tags:

views:

467

answers:

2

I have a form with a custom control on it. Assume the custom control has the focus. If I show a message box from that form, when the message box is closed by pressing Enter on either the OK or Cancel button, the message box is closed and then the custom control gets a keyboard event (OnKeyUp) with the enter key.

This doesn't happen if the space key is used to "press" either the OK or Cancel button.

It's like the MessageBox doesn't consume the Enter Key for some reason. I tried this with the Form's KeyPreview property turned on, but there was no difference.

Does anyone know how to stop that enter message after it is used to press the MessageBox button?

+3  A: 

Can't you ignore it in code? This is VB syntax:

    Private Sub frmEdit_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
 If e.KeyCode = Keys.Enter Then
  e.Handled = True
 End If
End Sub
Beth
+2  A: 

I have done a rough conversion from VB.NET to C# for you, I hope this helps.

private void frmEdit_KeyUp(ByVal object sender, KeyEventArgs e)
{
   if( e.KeyCode == Keys.Enter )
      {
         e.Handled = true;
      }
   else Application.DoEvents();
}
baeltazor