取消Windows窗体事件

时间:2009-11-19 19:15:47

标签: winforms events

我有一个Windows窗体。它有ComboBoxDataGrid

我的Leave上有ComboBox个活动,我的DoubleClick行有DataGrid个活动

我们的想法是,在离开ComboBox时,如果ComboBox的值发生了变化,那么请使用新值重新加载DataGrid

假设ComboBox显示的值为1,对于该值,DataGrid中会显示5条记录。 现在,用户在2和标签中输入ComboBox。在我的Leave事件中,我看到值已更改,我将DataGrid重新加载该值的所有记录。

但是,如果用户输入2并双击现有记录中的值1,则 离开事件重新加载DataGridDoubleClick事件FIRES。

如果已重新加载DataGrid,如何查找待处理事件列表并取消其中的每一项?

2 个答案:

答案 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...
}