C# – Enter Key Not Consumed by MessageBox, C#, Windows Forms

cwinforms

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?

Best Answer

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
Related Topic