Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

textbox and listbox 1

Status
Not open for further replies.

amberlynn

Programmer
Dec 18, 2003
502
0
0
CA
Hello,

I want to have a textbox that displays the selected item in a listbox, and have it change on the 'listbox SelectedIndexChanged event, so every time a new item is selected in the listbox, the textbox is updated.

What's the best way to go about this??

Thanks!

Amber
 
Add a textbox to your form. Add the listbox..In form load populate the list box

Listbox1.Items.Add("Pizza")
Listbox1.Items.add("Chicken")
Listbox1.Items.add("Beer")

In the ListBox1_SelectedValueChanged event add the following code

TextBox1.Text = ListBox1.SelectedItem

Whenever you select the item in the listbox the textbox will change
 
That doesn't seem to work properly.
I have to add .toString to the end to make it work, but it isn't returning the correct item.
It's returning items farther down the list.
And if I click the same item again, the textbox displays:

System.Data.DataRowView

Am I missing something?
My listbox is populated from a SQL table.

Thanks,
Amber
 
A list box's list contains objects. Depending on the object, just doing a .ToString on it won't net you the value that you see. Sometimes it works. Sometimes the list box contains string objects. Sometimes it is bound to a dataset. Sometimes it contains AcmeGenericObjectXWhichYouJustCreated.

That said, declare a DataRowView and set it to your selected item. Get the value out of the specific column in your DataRowView. Display that value.
 
Thanks for the reply. I really appreciate the explanations.

So my listbox is populated from a SQL database.
It contains strings.
When I do the "TextBox1.Text = ListBox1.SelectedItem.toString" it doesn't display the correct values...it's always off by a row or so.
If I do it this way:
Dim drv As DataRowView = lBoxCP.SelectedItem
txtCPMain.Text = drv("STR_CUTTING_PERMIT")
It works ALMOST perfectly...but after repeated times of selecting different items, it starts to disply the wrong item.
One thing I noticed, the first item in the list is selected automatically at first. Then when you click on other items, it works for a while, but the textbox is ALWAYS wrong when you select the very first item...
I'm baffled.
amber
 
I'm not sure. Do you have it on multi-select or something?
 
SelectionMode is set to One...is that what you mean?
amber
 
Yes. I'm not sure right off hand. I remember a similar problem with the web controls listbox. I think I resorted to added the items via .Items.Add() from a DataReader or DataTable some of the time.
 
Hmmm...I just don't know where to go from here.

Can you explain to me...the datarowview...how exactly does this work?
You declare it as a datarowview based on the item selected in a listboxl...so where is it pulling data from?

How does it know which dataset to use, if there are multiple datasets on a given form?

I'm unclear on this...
amber
 
I assume you set the .DataBinding() for the ListBox.

It's similar to a problem I used to have in VB6 with the ComboBox and the SelectedIndexChanged event. I would have to use the .Click or .ItemClick event instead. But the ListBox doesn't have such an event in .Net.

I'm sure the answer is simple and has been experienced by others. If someone else has an idea....?
 
The databinding is set in a different sub.
The reason I'm a bit confused is that the listbox is populated in one of two ways...either with a filter or without (depending on what a user enters before pressing the button that populates the listbox.
So when you declare a datarowview, it pulls from the dataset that the listbox is currently bound to?
It's becoming clearer...but still doesn't work :)
amber
 
Watch, try this. This may help:

Dim drv As DataRowView
Dim dv As DataView
Dim dt As DataTable
Dim ds As DataSet

dv = drv.DataView
dt = dv.Table
ds = dt.DataSet

Look at the tool-tips when you set each object equal to the other object's property.
 
try this:

TextBox1.Text = ListBox1.SelectedItem(0)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top