Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to fetch selected item from a listview

Status
Not open for further replies.

redshadow

Programmer
May 24, 2001
70
0
0
PH
Hi All,

How can I fetch the item selected by the user as well as the subitem/s from the listview.

 
Maybe this will help:

Code:
[COLOR=green]'Assume there is a ListView with default name[/color]

[COLOR=blue]Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load[/color]

        [COLOR=green]'Add headings / items to listview, view property set to Details[/color]
        ListView1.Columns.Add("C1", 200, HorizontalAlignment.Center)
        ListView1.Columns.Add("C2", 200, HorizontalAlignment.Center)

        ListView1.Items.Add("Item 1")
        ListView1.Items(0).SubItems.Add("Subitem 1")
        ListView1.Items.Add("Item 2")

    [COLOR=blue]End Sub[/color]

[COLOR=blue]Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged[/color]
        Dim i1 As String
        Dim i2 As String
  
        [COLOR=green]'get item and subitem values[/color]
        i1 = ListView1.SelectedItems(0).SubItems(0).Text
        i2 = ListView1.SelectedItems(0).SubItems(1).Text

        [COLOR=green]'show the values[/color]
        MessageBox.Show("1: " & i1 & " - 2:" & i2)

    [COLOR=blue]End Sub[/color]

you should also look into doing something like

Code:
Dim itmX as ListViewItem
For Each itmX in ListView1.SelectedItems
        [COLOR=green]'process each item[/color]
Next

Hope this points you in the right direction

 
Hi, Sorry for the late reply.

Thank you for the reply. I tried your code and it works, however, when I click again another item it generates an exception.

I tried to enclose it in the try catch block and it says that the index is out of bound but after catching the error, the item was also fetched.

My additional question now is how can I remove the exception issue. Thanks again.
 
You get an exception since when you select another item, the event SelectedIndexChanged is fired twice, one for the unselect and one for the select. In the unselect, SelectedItems.Count is zero so you get an exception.
A little fix:
Code:
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
        Dim i1 As String
        Dim i2 As String
  
        'get item and subitem values
        If ListView1.SelectedItems.Count > 0 Then
                i1 = ListView1.SelectedItems(0).SubItems(0).Text
                i2 = ListView1.SelectedItems(0).SubItems(1).Text
        'show the values
                MessageBox.Show("1: " & i1 & " - 2:" & i2)

        End If


    End Sub
You don't need the "If" if you implement the for each as NBartomeli suggested. You may think "why do I need for each, I don't have multiple selection", but imagine you will change your selection to multi someday and forget to change the event...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top