Unlock For Us

[C#] How to Get the Selected Index and Selected Item Text in List View Control

List View class makes the life easier. It represents a Windows list view control, which displays a collection of items that can be displayed using one of four different views.

I know this is not related to Windows Tips and Tricks. But since this blog is dedicated for helping users…. I decided to write the guide.

winbubble

WinGuggle 2.1 System Summary tab is using a list view control written in VB.net.

 

 

 


wintinker


WinTinker Password Generator Web Applications Tab also uses a list view control




There is no SelectedIndex property in List View that’s why its very difficult to get the selected item text using its index.

Thankfully I found a workaround to get the selected index.

if (ListView1.SelectedItems[0].ToString().IndexOf("Audio") > 0)
{
    intindex = 1;  //set the index manually

}

Using the SelectedIndexChanged Event

private void ListViewWeb_SelectedIndexChanged(object sender, EventArgs e)
{
     if (ListViewWeb.SelectedIndices.Count <= 0)
     {
            return;
     }
     int intselectedindex = ListViewWeb.SelectedIndices[0];
     if (intselectedindex >= 0)
    { 
   //MessageBox.Show(ListViewWeb.Items[intselectedindex].Text);
    }
}

 

Hope this helps!

0 Comments:

 

© UnlockForUs 2007-2012| Blogger| Google.com | License Agreement