简体   繁体   中英

C# combobox selection feeding another combobox

using mysql I feed combobox2 with selection of combobox1.It works OK. but the problem is the first select seems does not trigger the eventhandler. the second time I do it it will trigger.

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { comboBox1.SelectedValueChanged += new EventHandler(comboBox1_selectedvaluechanged); }

private void comboBox1_selectedvaluechanged(object sender, EventArgs e) { region = comboBox1.SelectedItem.ToString(); values_to_venue(); db.connection.Close(); }

It's because you're not creating your event handler until the SelectedIndex of the combobox changes. This is fired alongside SelectedValue changes. Create the event handler in the load method ensuring it is there when the first SelectedValue changes. If you put it in the Load make sure you clean it up with -= in the unload. Or, you can just create it in the constructor and then you won't need to remove it.

You do want to set the events in your constructor or load method; also, I think the event you want to use is ComboBox.SelectionChangeCommitted because if the user navigates the DropDown list using the up/down arrows on the keypad, SelectedIndexChanged will fire before the selection is committed - is that what you want??????

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM