Get the selected value from a listbox

B

Ben

Hi

Im trying to get the value from a listbox, i have the below code to get the
text:
txtEntity.Text = ListBox1.Text

But do not seem to be able to retrieve the data that i set as the value
member.

Thanks

B
 
B

Ben

Hi Claes

Thanks for your post: when i use it i get:
txtEntity.Text = ListBox1.SelectedValue

Thanks B
 
C

Claes Bergefall

Sorry, I don't understand. What do you get?
Do you get the same thing as ListBox1.Text?

How are you populating your listbox?

/claes
 
J

james

How about using: txtEntity.Text = ListBox1.SelectedItem.ToString

That should populate the textbox with the selected value ( Item) from your
listbox.
james
 
B

Ben

Hi Claes,

I am populating it like this:
Dim cn As New SqlConnection(strConnectionString)

cn.Open()

Dim ds As New DataSet

Dim da As New SqlDataAdapter(strSQL, cn)



da.Fill(ds, "viewOutlookIntegrationEmails")

ListBox1.DataSource = ds.Tables("viewOutlookIntegrationEmails")

ListBox1.DisplayMember = "Name"

ListBox1.ValueMember = "Code"

Code is the Reference from the database
Name is the name from the database

I get the name with:
txtEntity.Text = ListBox1.Text

But when i try to retreive the hidden code i get nothing with:
txtEntity.Text = ListBox1.SelectedItem

Or as James suggested:
txtEntity.Text = ListBox1.SelectedItem.ToString

Regards

B
 
J

james

Ben, in what event are you retreiving the value(s) from the Listbox to go in
your textbox (txtEntity) ? If you add the code I gave you in the ListBox's
"SelectedIndexChanged " event, then it should work when you click on an
item/value in the listbox, if your listbox is populated with data/values
from your database.
james
 
J

james

Ben, here's a quick example of what I'm talking about with a form containing
a textbox and a listbox:


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

For i As Integer = 1 To 10

ListBox1.Items.Add(i).ToString()

Next

End Sub

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles ListBox1.SelectedIndexChanged

TextBox1.Text = ListBox1.SelectedItem.ToString

End Sub


Now, when you click on any value in the listbox, the value is shown in the
textbox and the value changes as you select each one(replacing the previous
value).
james

(sorry if this is something you already knew)
 
B

Ben

Hi James

Thanks for your post.

It is very odd as this is exactly what I am doing.

Is the problem that the value data (ListBox1.ValueMember) cannot contain
text? As the data in here is eg 'EST00123'

Thanks
B
 
J

james

Ben, just doing a bit of testing, if I set a ListBoxes' ValueMember to a
String (or any value for that matter) it throws an error.
But, if I comment out : Listbox1.ValueMember ="EST00123" then the rest of my
code sample I posted previously works.
I think there is a misunderstanding of what ValueMember is/or does(on my
part). The example in Help for Listbox, gives a sample of populating a
Listbox from an Array and uses ValueMember. But, I cannot get the sample to
work properly.
Of course, it could be that I am misunderstanding how the code sample is
supposed to be used.
But, try commenting out the Listbox1.ValueMember and see if your code works
then.
james
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top