Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
How can I get the selected index of the selected item in a listview and also get the text that is selected. I.E. I have a listbox with a bunch of teams in it and I want to get the text that is selected in the listview. Like if Yankees is selected, I want a string set to "Yankees". Any ideas?

Thanks,

Tehon

Posted

Yes.. I could just give you the direct answer. No.. I'm not going to. :) Please do not think I'm trying to be rude, I'm trying to give you enough help that if you come across a similar problem, you'll know where to find the answer..

 

Go to View -> Other Windows -> Object Browser

 

There's a search icon, search for ListBox. Once you find the ListBox object, double click it and it'll show you all of the methods and properties the ListBox contains.. and then some.

 

The object browser contains detailed information on every single object that .NET has.

 

(Hint: Look for a property with the name Selected in it).

Gamer extraordinaire. Programmer wannabe.
Posted
I know I can use the SelectedItems method but that returns a ListViewItem correct? And I need to know how to convert that into something that I can use, like a string.

Thanks,

Tehon

Posted
Just attach .ToString() to the end of whatever property returns the object you're looking for.
Gamer extraordinaire. Programmer wannabe.
  • Leaders
Posted

This was the way bucky showed me to use listview a few months ago, hope this helps

Dim li As ListViewItem
   Dim sName As String

   For Each li In lvw.Items
     If li.Text = sName Then
       lvw.Items.Remove(li) 
       ' If you only want to remove one item with that Text 
       ' you can put an Exit For right here
     End If
   Next

you can get the index from the (li) and also do anything else, like adding additional text, icons etc...

Posted

I had the same problem, and found the solution with the following code

 

Dim StringVariable As String = ListView.SelectedItems( _
Listview.Items(0).Index).SubItems(1).Text

 

I hope this can help you

Fat kids are harder to kidnap
Posted
Why do I keep getting an ArgumentOutOfRange Exception? I am using:
                Dim StringVariable As String = lvwTeams.SelectedItems(0).ToString
       Label1.Text = StringVariable

Thanks,

Tehon

Posted (edited)

Still won't work for some reason.

Dim StringVariable As String = lvwTeams.SelectedItems(lvwTeams.Items(0).Index).Text

Still getting an argumentoutofrange exception. I'm selecting one of the items in the list and clicking a button that fires this method and I'm getting that exception. I am adding them to the listview this way:


       Dim str(6) As String
       Dim itm As ListViewItem
       While myReader.Read()
           str(0) = myReader.GetValue(2)
           str(1) = myReader.GetValue(3)
           str(2) = myReader.GetValue(4)
           str(3) = myReader.GetValue(5)
           str(4) = myReader.GetValue(6)
           str(5) = myReader.GetValue(7)
           itm = New ListViewItem(str)
           lvwTeams.Items.Add(itm)
       End While


Any other ideas?

Edited by tehon3299

Thanks,

Tehon

Posted

listBox1.SelectedItem.ToString() should work just fine. Or if you're expecting multiple selections, listBox1.SelectedItems[index].ToString() would also work fine (better yet you could foreach through it).

 

Unless the item you're trying to reference doesn't exist (ie; you have no items selected), I'm not sure why it'd be giving you that error. The line that you posted above, is that the line you're getting the error on?

 

Also, you could try adding each item as you go rather then throwing them into a string array...

lvwTeams.Items.Add(myReader.GetValue(2));

// etc..

Gamer extraordinaire. Programmer wannabe.
Posted

It's probably where you are declaring your string variable. Here's how I did it, note I am just adding dummy values to the list view

   Sub PopulateListView()
       Dim str(6) As String
       Dim itm As ListViewItem
       str(0) = "This"
       str(1) = "is"
       str(2) = "only"
       str(3) = "A"
       str(4) = "Test"
       str(5) = "Okay"
       itm = New ListViewItem(str)
       lvwTeams.Items.Add(itm)
   End Sub

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       PopulateListView()
   End Sub

   Private Sub lvwTeams_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwTeams.Click
       Dim StringVariable As String = lvwTeams.SelectedItems(lvwTeams.Items(0).Index).Text
       TextBox1.Text = StringVariable
   End Sub

Posted
It is still bombing on the:
Dim StringVariable As String = lvwTeams.SelectedItems(lvwTeams.Items(0).Index).Text
       TextBox1.Text = StringVariable

It says argument out of range exception. I can not figure this out.

Thanks,

Tehon

  • *Gurus*
Posted

Dim StringVariable As String = lvwTeams.SelectedItems(lvwTeams.Items(0).Index).Text

 

That code makes no sense. To get the text of the first selected item, do this:

 

Dim StringVariable As String = lvwTeams.SelectedItems(0).Text

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

  • 1 month later...
Posted

tehon3299 said....

"Why do I keep getting an ArgumentOutOfRange Exception? I am using: ...."

 

I kept getting the same so tried a few things and finally found that

 

ListView1_MouseUp

 

works better and always returns the selected item.

 

ListView1_SelectedIndexChanged

seems to error if you select one item then select another.

 

I guess when you click another item, it has changed but nothing is selected until your mouse click is up.

 

Drove me mad

 

I know this thread is old but this tip may save one person from wasting time because they were using the wrong code.

 

gs

  • 2 months later...
Posted

I just stumbled on this, I know it's old, but maybe it will help someone. When you select a new index in a listview the SelectedIndexChanged event fires twice. once when it changes to nothing, or -1, then again when it has the new index. I believe setting certain properties may affect this.

 

to get around it, use this code at beginning of sub:

 

If ListView1.SelectedItems.Count = 0 Then

Exit Sub

End If

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...