我有一个Windows窗体。它有ComboBox
和DataGrid
我的Leave
上有ComboBox
个活动,我的DoubleClick
行有DataGrid
个活动
我们的想法是,在离开ComboBox
时,如果ComboBox
的值发生了变化,那么请使用新值重新加载DataGrid
。
假设ComboBox
显示的值为1
,对于该值,DataGrid
中会显示5条记录。
现在,用户在2
和标签中输入ComboBox
。在我的Leave
事件中,我看到值已更改,我将DataGrid
重新加载该值的所有记录。
但是,如果用户输入2
并双击现有记录中的值1
,则
离开事件重新加载DataGrid
和DoubleClick
事件FIRES。
如果已重新加载DataGrid
,如何查找待处理事件列表并取消其中的每一项?
答案 0 :(得分:1)
不要使用Leave
事件,请尝试SelectedIndexChanged
Event,这会在DataGrid
s DoubleClick
事件之前触发。缺点是如果用户使用键盘滚动ComboBox
,如果用户在ComboBox
中向下滚动5步,则会触发5次。
另一种解决方案是在输入lComboEntered=true
时存储局部变量ComboBox
,并在false
事件触发时将值设置为Leave
。并在DataGrid
DoubleClick
事件中检查lComboEntered=false
之前是否做过任何事情。
答案 1 :(得分:1)
您遇到了事件订单问题,DataGridView.Enter事件过早触发。通过使用Control.BeginInvoke()方法延迟事件操作,可以彻底解决这个问题。一旦程序重新进入消息循环,其委托目标就会触发。换句话说,在所有挂起事件被触发后。使它看起来类似于:
private bool selectionDirty;
private void comboBox1_TextChanged(object sender, EventArgs e) {
selectionDirty = true;
}
private void dataGridView1_Enter(object sender, EventArgs e) {
this.BeginInvoke(new Action(() => selectionDirty = false));
}
private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e) {
if (selectionDirty) return;
// etc...
}