tehon3299 Posted March 28, 2003 Posted March 28, 2003 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? Quote Thanks, Tehon
wyrd Posted March 28, 2003 Posted March 28, 2003 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). Quote Gamer extraordinaire. Programmer wannabe.
tehon3299 Posted March 28, 2003 Author Posted March 28, 2003 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. Quote Thanks, Tehon
wyrd Posted March 28, 2003 Posted March 28, 2003 Just attach .ToString() to the end of whatever property returns the object you're looking for. Quote Gamer extraordinaire. Programmer wannabe.
Leaders dynamic_sysop Posted March 28, 2003 Leaders Posted March 28, 2003 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... Quote
iebidan Posted March 28, 2003 Posted March 28, 2003 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 Quote Fat kids are harder to kidnap
tehon3299 Posted March 28, 2003 Author Posted March 28, 2003 Why do I keep getting an ArgumentOutOfRange Exception? I am using: Dim StringVariable As String = lvwTeams.SelectedItems(0).ToString Label1.Text = StringVariable Quote Thanks, Tehon
iebidan Posted March 28, 2003 Posted March 28, 2003 Try the following code Dim StringVariable As String = ListView.SelectedItems( _ ListView.Items(0).Index).Text Quote Fat kids are harder to kidnap
tehon3299 Posted March 28, 2003 Author Posted March 28, 2003 (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 March 28, 2003 by tehon3299 Quote Thanks, Tehon
wyrd Posted March 28, 2003 Posted March 28, 2003 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.. Quote Gamer extraordinaire. Programmer wannabe.
GMan_NC Posted March 28, 2003 Posted March 28, 2003 This works fine for me Dim StringVariable As String = lvwTeams.SelectedItems(0).Text Quote
tehon3299 Posted March 28, 2003 Author Posted March 28, 2003 It must be because I am adding them to the listview from an array. You think thats it? Quote Thanks, Tehon
GMan_NC Posted March 28, 2003 Posted March 28, 2003 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 Quote
tehon3299 Posted March 28, 2003 Author Posted March 28, 2003 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. Quote Thanks, Tehon
wyrd Posted March 28, 2003 Posted March 28, 2003 Do you actually have items selected in your ListBox? Quote Gamer extraordinaire. Programmer wannabe.
tehon3299 Posted March 28, 2003 Author Posted March 28, 2003 Yes, I am selecting one item in the box. Quote Thanks, Tehon
*Gurus* divil Posted March 29, 2003 *Gurus* Posted March 29, 2003 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 Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
gs_london Posted May 20, 2003 Posted May 20, 2003 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 Quote
monkeys smell Posted August 13, 2003 Posted August 13, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.