简体   繁体   English

阻止父窗体在MessageBox打开(或“关闭”)时接收KeyUp事件

[英]Prevent Parent Form from receiving KeyUp event while MessageBox is open (or “closing”)

My question somewhat relates to this question but the solution suggested is not working for me. 我的问题在某种程度上与问题有关,但是建议的解决方案对我不起作用。

So here's my case. 所以这是我的情况。

I have a child form within an MDI parent. 我在MDI家长中有一个孩子表格。 The form contains Tab control and a GridView in it. 该窗体中包含Tab控件和一个GridView。 I have added keyboard shortcuts within KeyUp event of the form itself. 我在窗体本身的KeyUp事件中添加了键盘快捷KeyUp Now when user has selected one of the rows in Grid and hits Delete , I do MessageBox.Show() with YESNO buttons to confirm user's action. 现在,当用户选择了Grid中的某一行并YESNO Delete时 ,我使用YESNO按钮执行MessageBox.Show()以确认用户的操作。

Also, Form supports Enter (or Ctrl + O ) key that if User hits it while record is selected from the Grid, it opens the Record in another child form for editing. 此外,Form支持Enter (或Ctrl + O )键,如果在从网格中选择记录时用户单击它,它将以另一个子窗体打开Record进行编辑。

Here, Enter key is causing conflicts, as when I have that delete confirmation MessageBox open, and I hit "Enter", it does the delete operation but the same record is also opened in the child form for editing (this can obviously lead to NullPointers but I guess delete from Database occurs after the record is cached for opening). 在这里, Enter键引起冲突,因为当我打开该删除确认MessageBox并单击“ Enter”时,它会执行删除操作,但也会在子窗体中打开同一记录以进行编辑(这显然会导致NullPointers但我想从数据库中删除是在将记录缓存为打开之后发生的。

As the solutions provided in the similar question I linked earlier, I tried setting a Form level flag which is set to true when MessageBox is opened and set to false when user clicks either of Yes or No keys, but I'm unsure if I'm setting flag at proper place in code or not. 正如我先前链接的类似问题中提供的解决方案一样,我尝试设置一个窗体级别标志,该窗体级别标志在打开MessageBox时设置为true,在用户单击“是”或“否”键时设置为false,但是我不确定是否是否在代码中的适当位置设置标志。

PSA: I have Delete and Open as buttons on the form as well and thus I'm using same methods on Shortcuts. PSA:我在窗体上也有Delete和Open作为按钮,因此我在Shortcuts上使用了相同的方法。

Here's my KeyUp Event of Form 这是我的KeyUp表单事件

private void FormAnalystOpenReport_KeyUp(object sender, KeyEventArgs e)
{
    if (((e.Control && e.KeyCode == Keys.O) || e.KeyCode == Keys.Enter) &&
          !this.DELETE_CONFIRM_OPEN)
    {
        rtBtnOpen_Click(sender, e);
    }
    else if (e.KeyCode == Keys.Delete)
    {
        rtBtnDelete_Click(sender, e);
    }
}

And following method to delete record 并按照以下方法删除记录

private void rtBtnDelete_Click(object sender, EventArgs e)
{
    DataGridViewRow row = (DataGridViewRow)rtDataGrid.SelectedRows[0];
    int delete_id = int.Parse(row.Cells[0].Value.ToString());
    this.DELETE_CONFIRM_OPEN = true;
    DialogResult feedback = MessageBox.Show(this,"Are you sure you want to delete selected record?", "Confirm Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if(feedback == DialogResult.Yes)
    {
        if (this.db.DeleteRecordById(delete_id)) //Would return true for successful delete of record, false otherwise.
        {
            //Code to reload Grid Data with updated Records list.
        }
        else
        {
            MessageBox.Show(this, "Failed to delete record!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }
    this.DELETE_CONFIRM_OPEN = false;
}

Thanks! 谢谢!

I think your problem is that messagebox works off a KeyDown event so when you return to your form the button is Down and you release it thus triggering your KeyUp. 我认为您的问题是消息框无法处理KeyDown事件,因此当您返回表单时,该按钮处于Down状态并释放它从而触发了KeyUp。

try adding a keydown event to your form to set the deleteconfirm. 尝试在您的表单中添加一个keydown事件以设置deleteconfirm。

 if ((e.Control && e.KeyCode == Keys.O) || e.KeyCode == Keys.Enter)
    {
        canDelete = true;
    }

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

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