locked
Selected Item in listview RRS feed

  • Question

  • When I was using listbox was easy to retrive the selected index, but finally I decided to use viewbox because I can set a back color for every item.
    How can I retrive the selected index in a fast way, and maybe to set it?
    Wednesday, April 1, 2009 8:50 PM

Answers

  • Since the ListView supports multiple selection, you don't have one selected index, but rather SelectedIndices. If you disabled multiselection, you can access it by SelectedIndices[0]. To set it you need to first call SelectedIndices.Clear() and then SelectedIndices.Add(i).

    Regards,
    Fernando.
    /* No comments */
    Wednesday, April 1, 2009 8:56 PM
  • hi,

    Try this
    foreach(ListViewItem lview In ListView1.SelectedItems)
    {
    Int index=ListView1.Items.IndexOf(listviewItem)
    }

    You can use another if statement, if you set the selection to multiple, so that the above loop execute when selected item is 1

    if(Listview1.Selecteditem.count=1)
    {
    //then put the above loop inside this if statement
    }

    i hope this help you

    kaymaf
    Wednesday, April 1, 2009 9:21 PM

All replies

    • Proposed as answer by JohnGrove Wednesday, April 1, 2009 8:56 PM
    • Unproposed as answer by wyrda Wednesday, April 1, 2009 9:03 PM
    • Proposed as answer by Lucian BaciuMicrosoft employee Thursday, April 2, 2009 5:21 AM
    • Unproposed as answer by wyrda Thursday, April 2, 2009 2:47 PM
    Wednesday, April 1, 2009 8:55 PM
  • Since the ListView supports multiple selection, you don't have one selected index, but rather SelectedIndices. If you disabled multiselection, you can access it by SelectedIndices[0]. To set it you need to first call SelectedIndices.Clear() and then SelectedIndices.Add(i).

    Regards,
    Fernando.
    /* No comments */
    Wednesday, April 1, 2009 8:56 PM
  • Error    4    Non-invocable member 'System.Windows.Forms.ListView.SelectedItems' cannot be used like a method.    :(
    • Marked as answer by wyrda Thursday, April 2, 2009 2:44 PM
    • Unmarked as answer by wyrda Thursday, April 2, 2009 2:45 PM
    • Unmarked as answer by wyrda Thursday, April 2, 2009 2:45 PM
    Wednesday, April 1, 2009 9:00 PM
  • hi,

    Try this
    foreach(ListViewItem lview In ListView1.SelectedItems)
    {
    Int index=ListView1.Items.IndexOf(listviewItem)
    }

    You can use another if statement, if you set the selection to multiple, so that the above loop execute when selected item is 1

    if(Listview1.Selecteditem.count=1)
    {
    //then put the above loop inside this if statement
    }

    i hope this help you

    kaymaf
    Wednesday, April 1, 2009 9:21 PM
  • I tried your way but it didnt work..

    I am now Trying to make a media player. I already made a media Player with listbox but now i am trying to make it with ListView. When i am using

    ListView1.SelectedIndices(0)

    it is perfectly playing it..but when i am changing the media for second time..it says that "0" is not  valid for 'index'..

    So, what could be the reason.

    Thank You,

    ROHAN

    Wednesday, March 7, 2012 8:48 AM