Home All Groups Group Topic Archive Search About

Get the selected value from a listbox

Author
31 Mar 2006 2:28 PM
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

Author
31 Mar 2006 3:15 PM
Claes Bergefall
Use ListBox1.SelectedValue

   /claes

Show quoteHide quote
"Ben" <B**@Newsgroups.microsoft.com> wrote in message
news:uk4I09MVGHA.2156@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
>
>
Author
31 Mar 2006 3:21 PM
Ben
Hi Claes

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

Thanks B

Show quoteHide quote
"Claes Bergefall" <louplou@nospam.nospam> wrote in message
news:O9qTAXNVGHA.4340@TK2MSFTNGP10.phx.gbl...

> Use ListBox1.SelectedValue
>
>    /claes
>
> "Ben" <B**@Newsgroups.microsoft.com> wrote in message
> news:uk4I09MVGHA.2156@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
> >
> >
>
>
Author
31 Mar 2006 3:46 PM
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

Show quoteHide quote
"Ben" <B**@Newsgroups.microsoft.com> wrote in message
news:ecO1MbNVGHA.5808@TK2MSFTNGP12.phx.gbl...
> Hi Claes
>
> Thanks for your post: when i use it i get:
> txtEntity.Text = ListBox1.SelectedValue
>
> Thanks B
>
> "Claes Bergefall" <louplou@nospam.nospam> wrote in message
> news:O9qTAXNVGHA.4340@TK2MSFTNGP10.phx.gbl...
>
>> Use ListBox1.SelectedValue
>>
>>    /claes
>>
>> "Ben" <B**@Newsgroups.microsoft.com> wrote in message
>> news:uk4I09MVGHA.2156@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
>> >
>> >
>>
>>
>
>
Author
31 Mar 2006 3:54 PM
james
How about using:     txtEntity.Text = ListBox1.SelectedItem.ToString

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

Show quoteHide quote
"Claes Bergefall" <louplou@nospam.nospam> wrote in message
news:uJDUSoNVGHA.5332@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" <B**@Newsgroups.microsoft.com> wrote in message
> news:ecO1MbNVGHA.5808@TK2MSFTNGP12.phx.gbl...
>> Hi Claes
>>
>> Thanks for your post: when i use it i get:
>> txtEntity.Text = ListBox1.SelectedValue
>>
>> Thanks B
>>
>> "Claes Bergefall" <louplou@nospam.nospam> wrote in message
>> news:O9qTAXNVGHA.4340@TK2MSFTNGP10.phx.gbl...
>>
>>> Use ListBox1.SelectedValue
>>>
>>>    /claes
>>>
>>> "Ben" <B**@Newsgroups.microsoft.com> wrote in message
>>> news:uk4I09MVGHA.2156@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
>>> >
>>> >
>>>
>>>
>>
>>
>
>
Author
31 Mar 2006 4:23 PM
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

Show quoteHide quote
"Claes Bergefall" <louplou@nospam.nospam> wrote in message
news:uJDUSoNVGHA.5332@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" <B**@Newsgroups.microsoft.com> wrote in message
> news:ecO1MbNVGHA.5808@TK2MSFTNGP12.phx.gbl...
> > Hi Claes
> >
> > Thanks for your post: when i use it i get:
> > txtEntity.Text = ListBox1.SelectedValue
> >
> > Thanks B
> >
> > "Claes Bergefall" <louplou@nospam.nospam> wrote in message
> > news:O9qTAXNVGHA.4340@TK2MSFTNGP10.phx.gbl...
> >
> >> Use ListBox1.SelectedValue
> >>
> >>    /claes
> >>
> >> "Ben" <B**@Newsgroups.microsoft.com> wrote in message
> >> news:uk4I09MVGHA.2156@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
> >> >
> >> >
> >>
> >>
> >
> >
>
>
Author
31 Mar 2006 4:57 PM
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


Show quoteHide quote
"Ben" <B**@Newsgroups.microsoft.com> wrote in message
news:%23Wrxt9NVGHA.5288@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" <louplou@nospam.nospam> wrote in message
> news:uJDUSoNVGHA.5332@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" <B**@Newsgroups.microsoft.com> wrote in message
>> news:ecO1MbNVGHA.5808@TK2MSFTNGP12.phx.gbl...
>> > Hi Claes
>> >
>> > Thanks for your post: when i use it i get:
>> > txtEntity.Text = ListBox1.SelectedValue
>> >
>> > Thanks B
>> >
>> > "Claes Bergefall" <louplou@nospam.nospam> wrote in message
>> > news:O9qTAXNVGHA.4340@TK2MSFTNGP10.phx.gbl...
>> >
>> >> Use ListBox1.SelectedValue
>> >>
>> >>    /claes
>> >>
>> >> "Ben" <B**@Newsgroups.microsoft.com> wrote in message
>> >> news:uk4I09MVGHA.2156@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
>> >> >
>> >> >
>> >>
>> >>
>> >
>> >
>>
>>
>
>
Author
31 Mar 2006 5:05 PM
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)
Author
31 Mar 2006 5:38 PM
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

Show quoteHide quote
"james" <jjames700REMOV***@earthlink.net> wrote in message
news:e6kSNVOVGHA.4976@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)
>
>
>
>
>
>
Author
31 Mar 2006 6:50 PM
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


Show quoteHide quote
"Ben" <B**@Newsgroups.microsoft.com> wrote in message
news:OPnfxnOVGHA.4952@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" <jjames700REMOV***@earthlink.net> wrote in message
> news:e6kSNVOVGHA.4976@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)
>>
>>
>>
>>
>>
>>
>
>
Author
31 Mar 2006 9:41 PM
Kerry Moorman
Ben,

Use the listbox's SelectedValue property.

Kerry Moorman


Show quoteHide quote
"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
>
>
>