954,517 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Get Selected Item

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

dnk
Light Poster
49 posts since Dec 2007
Reputation Points: 35
Solved Threads: 0
 

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 ;)

Comatose
Taboo Programmer
Team Colleague
2,910 posts since Dec 2004
Reputation Points: 361
Solved Threads: 215
 
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
cikara21
Posting Whiz
340 posts since Jul 2008
Reputation Points: 47
Solved Threads: 69
 
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
Newbie Poster
1 post since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

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

dnk
Light Poster
49 posts since Dec 2007
Reputation Points: 35
Solved Threads: 0
 

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
sonia sardana
Posting Whiz
326 posts since Mar 2008
Reputation Points: 0
Solved Threads: 8
 
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..........

rajprabuit
Newbie Poster
5 posts since Dec 2009
Reputation Points: 10
Solved Threads: 1
 

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

Thank you.

Sturdy
Light Poster
34 posts since Mar 2011
Reputation Points: 28
Solved Threads: 1
 

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.

dnk
Light Poster
49 posts since Dec 2007
Reputation Points: 35
Solved Threads: 0
 

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)
Jx_Man
Nearly a Senior Poster
3,329 posts since Nov 2007
Reputation Points: 1,372
Solved Threads: 444
 

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.

dnk
Light Poster
49 posts since Dec 2007
Reputation Points: 35
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You