|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Get the selected value from a listboxHi
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 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 > > 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 > > > > > > 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 >> > >> > >> >> > > 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 >>> > >>> > >>> >>> >> >> > > 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 > >> > > >> > > >> > >> > > > > > > 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 >> >> > >> >> > >> >> >> >> >> > >> > >> >> > > 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) 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) > > > > > > 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) >> >> >> >> >> >> > > 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 > > >
Help Needed WIth Printing BN.NET 2005
Patterns anyone? How do I detect a form being opened already From VB.NET to Word 2003 dll not reading .config ? Importing from Excel How can I get caption of all open windows?? Passing Vb equivilent of "structs" between forms settings.settings ?? LIstbox having multicolumn |
|||||||||||||||||||||||