By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,524 Members | 1,519 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,524 IT Pros & Developers. It's quick & easy.

Get the selected value from a listbox

P: n/a
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
Mar 31 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Use ListBox1.SelectedValue

/claes

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
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

Mar 31 '06 #2

P: n/a
Ben
Hi Claes

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

Thanks B

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
Use ListBox1.SelectedValue

/claes

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
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


Mar 31 '06 #3

P: n/a
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

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
Hi Claes

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

Thanks B

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
Use ListBox1.SelectedValue

/claes

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
> 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
>
>



Mar 31 '06 #4

P: n/a
How about using: txtEntity.Text = ListBox1.SelectedItem.ToString

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

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
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

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
Hi Claes

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

Thanks B

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
Use ListBox1.SelectedValue

/claes

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
> 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
>
>



Mar 31 '06 #5

P: n/a
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

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
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

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
Hi Claes

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

Thanks B

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:O9**************@TK2MSFTNGP10.phx.gbl...
Use ListBox1.SelectedValue

/claes

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:uk**************@tk2msftngp13.phx.gbl...
> 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
>
>



Mar 31 '06 #6

P: n/a
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
"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:%2****************@TK2MSFTNGP14.phx.gbl...
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

"Claes Bergefall" <lo*****@nospam.nospam> wrote in message
news:uJ**************@tk2msftngp13.phx.gbl...
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

"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:ec**************@TK2MSFTNGP12.phx.gbl...
> Hi Claes
>
> Thanks for your post: when i use it i get:
> txtEntity.Text = ListBox1.SelectedValue
>
> Thanks B
>
> "Claes Bergefall" <lo*****@nospam.nospam> wrote in message
> news:O9**************@TK2MSFTNGP10.phx.gbl...
>
>> Use ListBox1.SelectedValue
>>
>> /claes
>>
>> "Ben" <Be*@Newsgroups.microsoft.com> wrote in message
>> news:uk**************@tk2msftngp13.phx.gbl...
>> > 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
>> >
>> >
>>
>>
>
>



Mar 31 '06 #7

P: n/a
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)


Mar 31 '06 #8

P: n/a
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

"james" <jj***************@earthlink.net> wrote in message
news:e6**************@TK2MSFTNGP11.phx.gbl...
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)


Mar 31 '06 #9

P: n/a
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
"Ben" <Be*@Newsgroups.microsoft.com> wrote in message
news:OP**************@TK2MSFTNGP09.phx.gbl...
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

"james" <jj***************@earthlink.net> wrote in message
news:e6**************@TK2MSFTNGP11.phx.gbl...
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)



Mar 31 '06 #10

P: n/a
Ben,

Use the listbox's SelectedValue property.

Kerry Moorman
"Ben" wrote:
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

Mar 31 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.