Enter a key not used by MessageBox, C #, Windows Forms

I have a form with a custom control. Suppose user control has focus. If I show a message box from this form, when the message box is closed by pressing Enter, either OK or Cancel, the message box closes, and then the user control receives a keyboard event (OnKeyUp) with the key entered.

This does not happen if the space bar is used to β€œpress” either the OK or Cancel button.

As MessageBox does not use the Enter key for some reason. I tried this with the Key KeyReview property but there was no difference.

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

+3
source share
2 answers

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

    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
+3
source

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

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

Source: https://habr.com/ru/post/1714570/


All Articles