Results 1 to 11 of 11

Thread: Getting the selected item in a ListView control

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Getting the selected item in a ListView control

    I am trying to get the selected item in a ListView control, the multi select is set to FALSE.

    Is the SelectedIndexChanged event the best place to respond to a change in selection?

    How do I get the current selected index, assuming only one item is selected, dealing with multi select will come later

    I tried CurrentSelection = ListView_DirectorList.SelectedItems(0).Index

    but it keeps throwing exceptions :

    System.ArgumentOutOfRangeException: 'InvalidArgument=Value of '0' is not valid for 'index'.
    Parameter name: index'



    Rick

  2. #2
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Getting the selected item in a ListView control

    see if this helps

    Code:
     Private Sub ListView1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ListView1.SelectedIndexChanged
            'get Listview Data to Textbox
            'if you click on a Line in the Listview
            'the Items will be put in the Textboxes
            For Each lvItem As ListViewItem In ListView1.SelectedItems
                MessageBox.Show(lvItem.Index)
                TextBox1.Text = lvItem.SubItems(0).Text
                TextBox2.Text = lvItem.SubItems(1).Text
                TextBox3.Text = lvItem.SubItems(2).Text
            Next
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Getting the selected item in a ListView control

    I have multiselect set to false so I only need the index for the one item... I see how this could be made to work (looping through the selected items collection), but there must be a simpler way to just determine the currently selected item ???

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Getting the selected item in a ListView control

    This is an issue that a lot of people encounter because the ListView works differently to other controls in this regard. If an item is selected and then you select a different item, the SelectedIndexChanged event is raised twice. The first event occurs when the previous item is unselected. At that point, no items are selected, hence your exception. The second event occurs when the new item is selected, at which point 0 will be a valid index. You need to check that the SelectedItems.Count is greater than zero before trying to get the selected item. That will effectively ignore the first event and only act on the second.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Getting the selected item in a ListView control

    That is subtle, I realized that something wonky was happening because of the behavior, but did not necessarily connect it to the event firing twice. But I guess by strict definition of 'CHANGE' it should.

    thanks
    Rick

  6. #6
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,048

    Re: Getting the selected item in a ListView control

    Quote Originally Posted by Since VB_DOS View Post
    I have multiselect set to false so I only need the index for the one item... I see how this could be made to work (looping through the selected items collection), but there must be a simpler way to just determine the currently selected item ???
    well when I use a Listview I add a Checkbox
    Code:
     With ListView1
                .Items.Clear()
                .Columns.Clear()
                .View = View.Details
                .CheckBoxes = True '<-----
    if I need the Data to process I use Clone
    in Form2 I see the Data selected from Form1 in this example
    this can be 1 Item checked or n Items
    Code:
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            'first clear the previous selected Items in Form2.Listview1
            Form2.ListView1.Clear()
            'show the Form
            Form2.Show()
            'now update the Listview in Form2 with Checked items from Form1
            For Each Checkeditem As ListViewItem In ListView1.CheckedItems
                Form2.ListView1.Items.Add(Checkeditem.Clone)
                Debug.Print(Checkeditem.Index)
            Next
    
        End Sub
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Getting the selected item in a ListView control

    Another quick question, I cannot seem to figure out how to prevent the Listview control from showing multiple columns.... I really just want a single column list? The view is set to LIST.

    Rick

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,350

    Re: Getting the selected item in a ListView control

    Quote Originally Posted by Since VB_DOS View Post
    Another quick question, I cannot seem to figure out how to prevent the Listview control from showing multiple columns
    Look at the title of this thread. What does that question have to do with that topic? Nothing. Please keep each thread to a single topic and each topic to a single thread. If you have a question on a different topic, just start another thread.

    Anyway, you could try Details view with a single column but then you have to have a column header. If you want to see how a ListView works, spend some time with File Explorer in Windows.

  9. #9
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,686

    Re: Getting the selected item in a ListView control

    If you want to learn about using a ListView including obtaining the selected single item see

    TechNet Wiki article Windows Forms ListView: Examining Detail view with SQL-Server (VB.NET)

    Full source

    What is shown can help if you take time to study the code along get past it's not an exact match to your code.

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Dec 2020
    Posts
    97

    Re: Getting the selected item in a ListView control

    Thanks.....

    Rick

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Getting the selected item in a ListView control

    I'd use Details View. If you just want one column, don't add SubItems...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width