Search code examples
c#winformsbarcode-scannermotorola-emdk

want to stop user input by messagebox but bypassed by a Enter keydown


I'm coding a windows form application running on a barcode scanner.
The plantform is .Net2.0CF/C#.

What i want is whenever user input wrong, the app will pop a messagebox and block the next input(actually,a scan action) until user click the OK on the screen.

But normally the user will continuously scan the next stuff as they didn't find anything went wrong, this will insert a Enter keydown so the messagebox will be closed, in one word, the messagebox does not stop the user.

How can i code this? Below is a very simple code snippet

private void tb_KeyDown(object sender, KeyEventArgs e)
{
  if (e.KeyCode.ToString() == "Return")
  {
    if(!ValidateInput(tb.Text))
    MessageBox.Show("Error");
  }
}

Solution

  • You can create your own window (Form) that displays the error message, but does not react on the enter key.

    It should contain a button which the user can click (as you wrote), however you need to make sure the button does not have focus when the window is displayed. (Because if it had focus, pressing the return key will "click" the button.)

    A simple way for doing this is adding another control which has TabStop set to true (e.g. a textbox, another button) and which has a lower TabIndex property than the button.

    Additionally, maybe you might want to do a

    System.Media.SystemSounds.Beep.Play();
    

    when showing the window to draw the user's attention to the window.