Keeping ID and Value in sync using ComboBox WinForms C#
I'm using ComboBox
to keep data in. Usually i create 2 ComboBox
one hidden to keep ID and one with the real data. I need id to know which id it is in database. I then use SelectedIndex and event for both checkboxes to keep them in sync. Sometimes I even use more comboboxes and keep them all in sync with ea开发者_如何学JAVAch other using SelectedIndex and lots of events. I have a feeling there's a better way.
private void czynnoscInstrumentyFinansoweComboID_SelectedIndexChanged(object sender, EventArgs e) {
czynnoscInstrumentyFinansoweCombo.SelectedIndex = czynnoscInstrumentyFinansoweComboID.SelectedIndex;
}
Also another thing that I would like to have is autocomplete/search inside ComboBox
. When user uses combobox and starts typing inside combobox it just reacts on first letter so when you type 'start' it will jump thru the list to s
then to t
then to a
etc. when i would like it to try and find the phrase "Start of something".
How do I achieve both of those?
EDIT:
I am not using DataSets, and i am adding values like that:
// get sql data and put it into strings/decimals then add it like that to comboBox
string var = "sds";
czynnoscInstrumentyFinansoweComboID.Add(var);
Well, if I understand your question correctly then I'd say your feeling is correct. You dont have to use multiple combos to keep track of data/value(that is, the identifier in your case).
Usually to do that we need to assign a datasource to the combo box, so that when you would get the selected index changed event, it would provide you with text as well as attached id. Set DisplayMember to your dataset column that contains text, and set ValueMember property to your dataset column that contains the ID.
DataSet yourDataSource = SomeGetDataSourceMethod(); //get your data source ready.
this.cbxLookup.DataSource = yourDataSource;
this.cbxLookup.DisplayMember ="EmployeeName";
this.cbxLookup.ValueMember = "EmployeeID";
Upon SelectedIndexChanged event, you can get SelectedItem, SelectedIndex, SelectedValue.
Alternatively you can do following as well:
int value=1;
cbxLookup.Items.Add(new ListViewItem("Your Name", value));
--EDIT 2-- Define a structure like following:
class KeyValueData
{
public KeyValueData(string Text)
{
text = Text;
itemData = 0;
}
public KeyValueData(string Text, int ItemData)
{
text = Text;
itemData = ItemData;
}
public int ItemData
{
get
{
return itemData;
}
set
{
itemData = value;
}
}
public override string ToString()
{
return text;
}
protected string text;
protected int itemData;
}
//and then add into combo like following:
comboBox1.Items.Add(new KeyValueData("New Yorkers", 21));
--EDIT 1--
For your second part, assuming that you are using .NET 2.0, you can get the KeyPress event and use ComboBox.FindString method. This example might help.
Dzien Dobry,
First.... Why dont you put an OBJECT into the combobox that has the string and the ID value? There is always Selecteditem to get it (not SelectedIndex)... saves you a second combobox.
Second... get proper tools. Infragistics, Devexpress - the Microsoft internal UI elements are SERIOUSLY limited in Winforms. Gets better with WPF, but Windowms it just is really primitive. And it wont change - MS relies on the ecosystem to provide proper tooling here.
I never touch any of the Winform integrated controls - totally on Infragistics, and that for a reason.
Dzien Dobry rowniez:) I'm not sure, but are you missing the fact that you can easily keep both id and value in combobox?
And total offtopic, but your naming convention for controls just provoked big eruption of laugh here, a moment ago. Thank you for that.
精彩评论