Trapping the KeyPress event at the form level

I would like to preview all the key strokes on a form and take action only on numeric keys.  I have the following code which is not working.  Please help.

KeyPreview is True on the form

private static bool bolNonNumberEntered = false;

private static bool NonNumberEntered
{
   get
   {
      return bolNonNumberEntered;
   }
   set
   {
      bolNonNumberEntered = value;
   }
}

private void frmPOSAddMultipleItems_KeyPress(object sender, System.Windows.Forms.KeyEventArgs   e)
{
   // Initialize the flag to false.
   NonNumberEntered = false;

   // Determine whether the keystroke is a number from the top of the keyboard.
   if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
   {
      // Determine whether the keystroke is a number from the keypad.
      if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
      {
         // Determine whether the keystroke is a backspace.
         if(e.KeyCode != Keys.Back)
         {
            // A non-numerical keystroke was pressed.
            // Set the flag to true and evaluate in KeyPress event.
            NonNumberEntered = true;
         }
      }
   }
}

wenyonkAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
RoninTheConnect With a Mentor Commented:
Just change the event to be handled to "keyDown" instead of "keyPress". keyPressEventArgs doesn't have a definition for keyCode, but keyChar.

this.KeyDown += new KeyEventHandler(this.frmPOSAddMultipleItems_KeyDown);
private void frmPOSAddMultipleItems_KeyDown(object sender, System.Windows.Forms.KeyEventArgs   e)
{
   // Initialize the flag to false.
   NonNumberEntered = false;

   // Determine whether the keystroke is a number from the top of the keyboard.
   if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
   {
      // Determine whether the keystroke is a number from the keypad.
      if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
      {
         // Determine whether the keystroke is a backspace.
         if(e.KeyCode != Keys.Back)
         {
            // A non-numerical keystroke was pressed.
            // Set the flag to true and evaluate in KeyPress event.
            NonNumberEntered = true;
         }
      }
   }
}
0
 
wenyonkAuthor Commented:
The line below is in the Component Initialize section for the form:

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmPOSAddMultipleItems_KeyPress);
0
 
larker1Commented:
Does it get down past the initial if statement?

-L
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
testnCommented:
put this

public frmPOSAddMultipleItems()
{
       InitializeComponent();
       this.KeyPreview = true; //<---------- add this
}

protected override OnKeyPress(Sstem.Windows.Forms.KeyEventArgs   e) /// <--- change this
{
   // Initialize the flag to false.
   NonNumberEntered = false;

   // Determine whether the keystroke is a number from the top of the keyboard.
   if (e.KeyCode < Keys.D0 || e.KeyCode > Keys.D9)
   {
      // Determine whether the keystroke is a number from the keypad.
      if (e.KeyCode < Keys.NumPad0 || e.KeyCode > Keys.NumPad9)
      {
         // Determine whether the keystroke is a backspace.
         if(e.KeyCode != Keys.Back)
         {
            // A non-numerical keystroke was pressed.
            // Set the flag to true and evaluate in KeyPress event.
            NonNumberEntered = true;
         }
      }
      base.OnKeyPressed(e); //<--- call base method
   }
0
 
shankar_msCommented:
can just use

if (!char.IsDigit(e.KeyChar) && (e.KeyChar != (char)(int)Keys.Back))
                  {
                        e.Handled=true;
                  }

at the keypress event.
0
 
crazymanCommented:
This works, but not on the Number pad for some reason...


protected override void OnKeyDown(KeyEventArgs e)
            {
                  string _value = Convert.ToChar(e.KeyValue).ToString()  ;
                  if (char.IsNumber(_value,0))
                  {
                        //Do Something here
                        MessageBox.Show("Number " + _value + " Was Pressed");
                        e.Handled =true;
                  }
                  base.OnKeyDown (e);
            }
0
All Courses

From novice to tech pro — start learning today.