Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
See more:
I want the Selected Items Value of a List box to a string. How can i do that. I want the value of the items not the text. Any one who will help will be most thankfull.
Posted
Updated 5-Sep-19 5:04am
Comments
CPallini 18-Aug-11 11:39am
   
What do you mean with 'the value of the items'? What have you filled the list box with?
Alan Burkhart 18-Aug-11 12:12pm
   
With a Winforms listbox, the text of an item is the value. I assume you're working with Winforms since the question is tagged for VB. Please specify a bit more what exactly what you're trying to do and I'll be glad to help. :-)
Rate this:
Please Sign up or sign in to vote.

Solution 1

I'm going to assume that you have the listbox bound to a datatable. If so, I've had trouble with the SelectedValue myself.

What I've found works is to use the following to get the selected value:

Listbox.SelectedItems(X)(Listbox.ValueMember)


Just replace "Listbox" with the name of your control, and "X" with the index of the selected item you're getting the value for.

If you're only allowing 1 selection at a time, then you can hardcode the "X" as 0.

Good luck!
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

If you have a string and you want to get the value out of that, you can have a look at this[^] and use the convert method.
   
Rate this:
Please Sign up or sign in to vote.

Solution 3

I have a one gud Simplest way for you..!! you will love this.
.
Dim i As Integer
        Dim item(ListBox1.SelectedItems.Count - 1) As String

        For Each item(i) In ListBox1.SelectedItems
            ListBox2.Items.Add(item(i).ToString())
            i += 1
        Next


does it help u?
   
Rate this:
Please Sign up or sign in to vote.

Solution 4

OK - so maybe it is silly to post to this old question, but I found an answer that worked well for me. So here it is:

Dim selectedObj As itemsObj = TryCast(myListBox.SelectedItem, itemsObj)
If Not IsNothing(selectedObj) Then
      MsgBox(selectedObj.selectedProp)
End If


The itemsObj is the object that you had added to the ListBox, I did it like this myListBox.Add(itemsObj)
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100