winfrom中获取Listview选中项的值

这段代码是写在listview_MouseUp事件中的

private void listView1_MouseUp(object sender, MouseEventArgs e)  
{  
    if (e.Button == MouseButtons.Left)  
    {  
        WizardBase.IntermediateStep inter = (wizardControl1.WizardSteps[wizardControl1.CurrentStepIndex] as WizardBase.IntermediateStep);  
        if (listView1.SelectedItems.Count > 0)  
        {  
            //if (ListViewTmpIndex != -1)  
            //{  
            //    listView1.Items[ListViewTmpIndex].Selected = false;  
            //}  
            //ListViewTmpIndex = listView1.Items[listView1.SelectedIndices[0]].Index;  
            switch (listView1.Items[listView1.SelectedIndices[0]].Index)  
            {  
                case 0:  
                    //TODO:File                      
                    if (inter != null)  
                    {  
                        inter.Subtitle = "打开一个或多个文件";  
                        inter.Refresh();  
                    }  
                    break;  
                case 1:  
                    //TODO:Oracle  
                    if (inter != null)  
                    {  
                        inter.Subtitle = "连接一个OracleSpatial服务";  
                        inter.Refresh();  
                    }  
                    //wizardControl1.  
                    break;  
                case 2:  
                    //TODO:PostgreSql  
                    if (inter != null)  
                    {  
                        inter.Subtitle = "连接一个PostGIS服务";  
                        inter.Refresh();  
                    }  
                    break;  
                case 3:  
                    //TODO:ArcSDE  
                    if (inter != null)  
                    {  
                        inter.Subtitle = "连接一个Arc SDE服务";  
                        inter.Refresh();  
                    }  
                    break;  
                case 4:  
                    //TODO:SQLServer  
                    if (inter != null)  
                    {  
                        inter.Subtitle = "连接一个SQLServer服务";  
                        inter.Refresh();  
                    }  
                    break;  
                default:  
                    break;  
            }//switch 选中的行  
        }  
        else if (listView1.SelectedItems.Count <= 0)//点击空白区  
        {  
            if (this.listView1.FocusedItem != null)  
            {  
                ListViewItem item = this.listView1.GetItemAt(e.X, e.Y);  
                if (item == null)  
                {  
                    this.listView1.FocusedItem.Selected = true;  
                }  
            }       
        }  
    }//left_button_up  
}//listView1_MouseUp 

 

其实,写listview_Click事件中也行,根据自己的需要来处理就行。

转载于:https://www.cnblogs.com/canyan3073/articles/1753006.html

你可能感兴趣的:(winfrom中获取Listview选中项的值)