Hi All..
I want to get selected item on listview.
Does any one know how to solve this???
I'm using vb.net 2003

Dani AI

Generated

Short summary and reliable checklist for getting the selected ListView item (VB.NET 2003)

The safe pattern is: 1) respond to a selection event, 2) confirm something is selected, 3) confirm the requested subitem exists, then 4) read the text. The typical runtime error you saw after the first click happens because code tries to use SelectedItems(0) (or a subitem index) when nothing is selected or the subitem index is out of range. That explains why ’s guard fixed the problem — the event can fire with zero selected items.

Notes and troubleshooting tips

  • Always check SelectedItems.Count > 0 before touching SelectedItems(0). The SelectedIndexChanged event fires both when an item is selected and when selection clears.
  • Verify item.SubItems.Count > desiredIndex before reading SubItems(desiredIndex).Text to avoid ArgumentOutOfRange.
  • GetItemAt(x,y) returns Nothing when you click empty space; check for Nothing. ’s idea is useful only in mouse handlers and needs a null check.
  • Avoid On Error Resume Next (as suggested by ) — it hides the real problem and makes bugs harder to find.
  • If your project is really using an older control build where FocusedItem isn’t available (as noted by /), use SelectedItems or SelectedIndices instead.

Practical pattern (safe, works for single- or multi-select)

' In SelectedIndexChanged handler: build a comma-separated list of the second-column text for all selected rows
Dim sb As New System.Text.StringBuilder()
For Each it As ListViewItem In ListView1.SelectedItems
    If it.SubItems.Count > 1 Then
        If sb.Length > 0 Then sb.Append(", ")
        sb.Append(it.SubItems(1).Text)
    End If
Next
TextBox1.Text = sb.ToString()

Credit where due: showed the basic SelectedItems approach, demonstrated GetItemAt usage, and illustrated iterating items/subitems, and ’s count-check is the key fix that resolves the “works once, then errors” symptom.

Recommended Answers

All 10 Replies

For Top Level:

MsgBox(ListView1.SelectedItems(0).Text)

And Subitems:

MsgBox(ListView1.SelectedItems(0).SubItems(1).Text)

You may want to make sure you have subitems before trying that one ;)

dim x as integer
dim y as integer
dim str as string

private sub ON_BUTTON_CLICK(...sender as OBJECT,... e as MOUSE_EVENT_ARGS)
    x = e.X
    y = e.Y
    str = ListView1.GetItemAt(x, y).Text
end sub

Hi All..
I want to get selected item on listview.
Does any one know how to solve this???
I'm using vb.net 2003

Dunno if this will help - I'm using vb 2008 express
Textbox1.Text = Listview1.Items.Item(Listview1.FocusedItem.Index).SubItems(x).Text

-- where x is the subitem value you want to retrieve

bradleystephens
>>well FocusedItem not available on vb 2003.

Cikara21
>> i'll try your code. Thx

Comatose
>> thx for the codes. I want if i selected any row i can get item on it.
so, how i can know the sub item of selected item.
MsgBox(ListView1.SelectedItems(0).SubItems(This part i wanted).Text)

Please help...Thx

On Form Load,I m adding items to the ListView.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim f As System.Drawing.Font = New System.Drawing.Font("Arial", 12.0!, System.Drawing.FontStyle.Bold)
        Dim lvitem As ListViewItem
        lvitem = ListView2.Items.Add("1")
        lvitem.SubItems.Add("A", Color.Black, Color.White, f)
        lvitem.SubItems(1).Font = f
        lvitem.UseItemStyleForSubItems = False
        lvitem = ListView2.Items.Add("2")
        lvitem.SubItems.Add("B", Color.Black, Color.White, f)
        lvitem.SubItems(1).Font = f
        lvitem.UseItemStyleForSubItems = False
           End Sub

On Listview Click,I show the text of second Subitem as message.

Private Sub ListView2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView2.Click
        Dim lCount As Integer
        For lCount = 0 To ListView2.Items.Count - 1
            If ListView2.Items(lCount).Selected = True Then
                MsgBox(ListView2.Items(lCount).SubItems(1).Text)
                Exit For
            End If
        Next
End sub

Hi All..
I want to get selected item on listview.
Does any one know how to solve this???
I'm using vb.net 2003

On Error Resume Next
TextBox1.Text = lv1.SelectedItems(0).SubItems(0).Text

here lv1 is name of Listview..

Above coding should be written inside lv1's selected Index changed event..

This coding is used to store listitem value in textbox..similar u can store it inwhich u want to get..........

Hi dnk,
I have some problem with this thread. Did you have found the answer?

Thank you.

No, its not solved. I already know how to get it.
There are no problem in the first time click but error came up when next click.

Try This :

Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
Dim i As Integer
If ListView1.SelectedItems.Count = 0 Then Exit Sub 
i = Val(ListView1.SelectedIndices.Item(0).ToString)
MsgBox(ListView1.Items(i).SubItems(1).Text)
End Sub

You also can use this line of code to get selected index :

i = Val(ListView1.SelectedItems(0).Index)
'and this
i = ListView1.SelectedIndices(0)
commented: Your helping make this thread solved after 2 years :D +2

Wow,,finally this thread can be solved.
Thanks Jx,
I already do like your code use SelectedIndices, but it just working in first click but not the next click. But you solved it now.
Thanks to sharing sir.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.