[RESOLVED] Data bound Combobox and Textbox. Help VS2005
Hello Community.
Situation : (var names simplified for ease of example)
1. I have a Combobox control bound to a table in SQL2005.
2. The Table is two columns "name" and "id"
Table example
id......Name
118...Jhon
321...Doe
ect.. (sorry for the basic description just trying to give as much info)
3. In the combobox properties i have the following setings.
DataSource = (my databound table source)
Displaymember = name
Valuemember = id
The Sql query retrives and integer that sets the item in the combobox to the value member accordingly. This all works perfect. The index of the combo box is not used because the values in the table are dynamic thus the use of an ID in the users table.
MY PROBLEM:
I wish to set a textbox to the displayed value of the combobox.
VB Code:
txtUserNon.Text = cboUserList.SelectedText
This seems not to work. I have tried all of the methods associated with comboboxes such as SelectedValue,Item, and text and they return the ValueMember?!..not the Shown text such as "Jhon" or "Doe"
For example the end result is to have the (databound)combobox displaying the username and the text box next to it displaying the same name.
I am pretty sure this problem does not exsist if i was using a dataset with a dataview. Thus the item value would be easy to copy, but the attempt here is to learn how to do what im asking to do with control bound data.
Probably be easiest to stick with data binding. Are you using the data sources window to drag and drop your properties? That seems to be much more reliable than doing it in code.
Anyway, here's a C# snippet that worked for me as a test.
You should read the help topic for the SelectedText property to see what it actually does. It works in exactly the same way as the SelectedText property of the TextBox and I'm sure that you can appreciate that there's no list to choose from in a TextBox so it cannot be that. Buoy is correct that data binding is the way to go but, if you were going to do it manually, you would need this code:
I guess im not sure of why this does not work. I read the Selected text and yes im using a dropdownlist. Thus the value it kicks back when I debug is a "" value and this is not what i want. Why does this feature not work when i set a datasource in the control like this and yet works if i actualy use Databinding the other way?? Is it not accomplishing the same task??
Take a look at the attached screen shot. The SelectedText property for that ComboBox would return "llo Wo". It has nothing to do with the list item that's been selected. That's returned by the SelectedItem property. If you've set the DropDownStyle of the ComboBox to DropDownList then SelectedText will ALWAYS return an empty string because it's not possible to select part of the text.
Re: [RESOLVED] Data bound Combobox and Textbox. Help VS2005
Do you want the user to be able to enter values into the text box that are not in the drop-down list? If not then you should simply set the DropDownStyle to DropDownList and forget SelectedText altogether because it's no relevant.
If you do want the user to be able to enter values that are not in the list then you can leave the DropDownStyle as DropDown and then you can do what you ask by handling the SelectedIndexChanged event and calling the SelectAll method. I would still guess that this is pointless and irrelevant though. If you don't specifically want to know if the user has selected only part of the text then SelectedText is COMPLETELY irrelevant. Just use the Text property to get the entire text in the text box.