?
Solved

Trapping the KeyPress event at the form level

Posted on 2005-03-17
6
Medium Priority
?
1,904 Views
Last Modified: 2012-05-05
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;
         }
      }
   }
}

0
Comment
Question by:wenyonk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 

Author Comment

by:wenyonk
ID: 13567839
The line below is in the Component Initialize section for the form:

this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.frmPOSAddMultipleItems_KeyPress);
0
 
LVL 5

Accepted Solution

by:
RoninThe earned 2000 total points
ID: 13568169
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
 
LVL 2

Expert Comment

by:larker1
ID: 13568180
Does it get down past the initial if statement?

-L
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 18

Expert Comment

by:testn
ID: 13569417
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
 
LVL 1

Expert Comment

by:shankar_ms
ID: 13570607
can just use

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

at the keypress event.
0
 
LVL 13

Expert Comment

by:crazyman
ID: 13573243
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question