Click here to Skip to main content
15,887,992 members
Please Sign up or sign in to vote.
2.00/5 (1 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. :-)

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:

VB
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!
 
Share this answer
 
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.
 
Share this answer
 
I have a one gud Simplest way for you..!! you will love this.
.
VB
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?
 
Share this answer
 
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)
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900