2

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!

IKavanagh
  • 6,089
  • 11
  • 42
  • 47
Kushal
  • 3,112
  • 10
  • 50
  • 79

1 Answers1

5

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;
    }
Sayse
  • 42,633
  • 14
  • 77
  • 146
  • Kudos!! that worked, ya `KeyUp` was culprit here, I needed to move my entire `KeyUp` event code to `KeyDown` and it worked out of the box! Lesson learned. Thanks a lot! – Kushal Jul 20 '13 at 12:42
  • 1
    No worries :) I prefer using keyup still as keydown can fire off multiple times as I'm sure you're aware – Sayse Jul 20 '13 at 12:43