Solved

C# winform press any key

Posted on 2011-02-22
5
1,269 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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
About delegates in c sharp 3 47
Capture logon name 13 70
Winform Module - What is the ASP.Net equiv 2 19
RLDC Reporting in Visual studio 11 13
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

12 Experts available now in Live!

Get 1:1 Help Now