简体   繁体   中英

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. The form contains Tab control and a GridView in it. I have added keyboard shortcuts within KeyUp event of the form itself. 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.

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.

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).

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.

PSA: I have Delete and Open as buttons on the form as well and thus I'm using same methods on Shortcuts.

Here's my KeyUp Event of Form

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.

try adding a keydown event to your form to set the deleteconfirm.

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

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