Solved

C# winform press any key

Posted on 2011-02-22
5
1,251 Views
Last Modified: 2013-12-17
Hi,

I  have a winform app, how can write code to handle any key press to exit my program?

Thanks,
JT
0
Comment
Question by:jtran007
5 Comments
 
LVL 6

Expert Comment

by:ViceroyFizzlebottom
ID: 34953572
Have you tried handling the KeyPress event?

private void Form1_KeyPress(object sender, KeyPressEventArgs e) {
      // Do stuff here
    }
0
 
LVL 9

Accepted Solution

by:
rawinnlnx9 earned 250 total points
ID: 34953619
MSDN has a good discussion on this and below is a code excerpt. URL: http://msdn.microsoft.com/en-us/library/system.windows.forms.control.keypress(v=vs.71).aspx


// Boolean flag used to determine when a character other than a number is entered.
private bool nonNumberEntered = false;

// Handle the KeyDown event to determine the type of character entered into the control.
private void textBox1_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;
            }
        }
    }
}

// This event occurs after the KeyDown event and can be used to prevent
// characters from entering the control.
private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
    // Check for the flag being set in the KeyDown event.
    if (nonNumberEntered == true)
    {
        // Stop the character from being entered into the control since it is non-numerical.
        e.Handled = true;
    }
}

Open in new window



This discussion was also informative and showed a different way to approach things:

http://bytes.com/topic/c-sharp/answers/791541-traping-all-combinations-keypress-overriding-processcmdkey
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34954095
You may need to set the Forms KeyPreview() Property to True to get the KeyPress() event to work properly (depends on which controls you have on your form).

To close the app, from the main form, use "this.Close();".
0
 
LVL 13

Expert Comment

by:Naman Goel
ID: 34954128
set form's KeyPreview property to true.

this.KeyPreview = true;

and handle any of these event

KeyDown(object sender, KeyEventArgs e)

KeyPress(object sender, KeyPressEventArgs e)

KeyUp(object sender, KeyEventArgs e)

like this :

 private void TestForm_KeyDown(object sender, KeyEventArgs e)
        {
            Application.Exit();
        }
0
 

Author Closing Comment

by:jtran007
ID: 34966666
Thanks,
JT
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

743 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now