简体   繁体   English

C# combobox 选择馈送另一个 combobox

[英]C# combobox selection feeding another combobox

using mysql I feed combobox2 with selection of combobox1.It works OK.使用 mysql 我选择组合框 1 来馈送组合框 2。它工作正常。 but the problem is the first select seems does not trigger the eventhandler.但问题是第一个 select 似乎没有触发事件处理程序。 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.这是因为在 combobox 的 SelectedIndex 更改之前,您不会创建事件处理程序。 This is fired alongside SelectedValue changes.这与 SelectedValue 更改一起触发。 Create the event handler in the load method ensuring it is there when the first SelectedValue changes.在 load 方法中创建事件处理程序,确保它在第一个 SelectedValue 更改时存在。 If you put it in the Load make sure you clean it up with -= in the unload.如果将其放入 Load 中,请确保在卸载时使用 -= 对其进行清理。 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??????另外,我认为您要使用的事件是 ComboBox.SelectionChangeCommitted 因为如果用户使用键盘上的向上/向下箭头导航下拉列表,则 SelectedIndexChanged 将在提交选择之前触发 - 这就是您想要的吗???? ??

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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