none
Add item to listview auto select new item

    Question

  • Good morning, here's my problem. I have a listview and labels on my form. When the user clicks on the label the labels text displays in the listview. And the first item in the listview is selected. But when I click on another label the text is displayed as the 2nd item in the listview but it is not selected. Attached is the code for a couple of labels.
    Private Sub Label49_Click(sender As System.Object, e As System.EventArgs) Handles Label49.Click
            Dim intIndex As Integer
            For intIndex = 0 To ListView1.Items.Count + 1
            Next intIndex
            ListView1.Focus()
            Dim lSingleItem As ListViewItem = New ListViewItem()
            lSingleItem = ListView1.Items.Add("Shirt(s)", 0)
            lSingleItem.SubItems.Add("")     'The first sub item for the first line
            lSingleItem.SubItems.Add("2.99")
            lSingleItem.SubItems.Add("")
            ListView1.Items(0).Selected = True
        End Sub
    
        Private Sub Label50_Click(sender As System.Object, e As System.EventArgs) Handles Label50.Click
            Dim intIndex As Integer
            For intIndex = 0 To ListView1.Items.Count + 1
            Next intIndex
                ListView1.Focus()
                Dim lSingleItem As ListViewItem '= New ListViewItem()
                lSingleItem = ListView1.Items.Add("Coat(s)", 0)
                lSingleItem.SubItems.Add("")     'The first sub item for the first line
                lSingleItem.SubItems.Add("2.99")
            lSingleItem.SubItems.Add("")
            ListView1.Items(0).Selected = True
            'Me.ListBox1.SelectedIndex = Me.ListBox1.SelectedIndex + 1
        End Sub
    Thanks for your help.
    Wednesday, April 18, 2012 3:22 PM

Answers

  • You are using ListView1.Items.Add() which adds the new item to the end of the collection, but then you always set the first item as selected with ListView1.Items(0).Selected.

    Either Insert the new item instead of Add so that the new item is always the first item, or change to ListView1.Items(ListView1.Items.Count - 1).Selected = True to always select the last item in the collection.

    Also, there seems to be no purpose to your loops (For intIndex = 0 To ListView1.Items.Count + 1).  You have no code inside the loop so there is no purpose to that part of your logic.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by bwells59 Wednesday, April 18, 2012 4:26 PM
    Wednesday, April 18, 2012 3:50 PM

All replies

  • You are using ListView1.Items.Add() which adds the new item to the end of the collection, but then you always set the first item as selected with ListView1.Items(0).Selected.

    Either Insert the new item instead of Add so that the new item is always the first item, or change to ListView1.Items(ListView1.Items.Count - 1).Selected = True to always select the last item in the collection.

    Also, there seems to be no purpose to your loops (For intIndex = 0 To ListView1.Items.Count + 1).  You have no code inside the loop so there is no purpose to that part of your logic.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    • Marked as answer by bwells59 Wednesday, April 18, 2012 4:26 PM
    Wednesday, April 18, 2012 3:50 PM
  • If you want to select the last item that was added to the ListView then you need to specify the correct index. If I understand your problem correctly, you probably want something like the following:

    ListView1.Items(ListView1.Items.Count - 1).Selected = True


    Paul ~~~~ Microsoft MVP (Visual Basic)

    Wednesday, April 18, 2012 3:57 PM
  • Thank you that is just what I needed
    Wednesday, April 18, 2012 4:28 PM
  • Thank you that is just what I needed
    Wednesday, April 18, 2012 4:28 PM