Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I'm working on c# windows form. If i filled all the text boxes then i'll press F4 key to save the data in db . Now i want to know the coding of key press event. Pls suggest me some ideas.



Thank You.

What I have tried:

I had no idea about key press event.
Posted
Updated 12-Sep-16 1:34am

 
Share this answer
 
Comments
OriginalGriff 12-Sep-16 6:12am    
KeyPress is only fired for "printable" characters plus backspace.
Richard MacCutchan 12-Sep-16 6:18am    
I obviously need to study those two links.
OriginalGriff 12-Sep-16 6:23am    
:laugh: It's caught me out before!
GKRISH04 12-Sep-16 6:19am    
Then ? There is no way to make a shortcut in my form ?
The problem with the KeyPress event is that it happens for every key, and it is directed at the control which has the "input focus" - which means that whichever textbox current accepts typing, that's the textbox for which the KeyPress event is fired - you wouldn't normally get it at the Form, which is really what you want. And KeyPress isn't fired at all for F4, although KeyDown and KeyUp are, KeyPress is only fired for printable characters anyway.

That's an unusable way to do it - the normal action in Windows is for ENTER to accept form data, and that's easy to do, it's all built in to WinForms anyway. F4 is unusual, and likely to confuse users so I strongly suggest that you don't go with it.
You can do it - but you'll have to override the ProcessCmdKey function for the form, and you will probably make your users hate the software!
 
Share this answer
 
Comments
GKRISH04 12-Sep-16 6:08am    
I used it for shortcut keys. then how can i make to submit the data while pressing cntl + S ?
OriginalGriff 12-Sep-16 6:11am    
Add another shortcut key, or override ProcessCmdKey and trap it out there.
GKRISH04 12-Sep-16 6:20am    
how ? Pls xplain it
OriginalGriff 12-Sep-16 6:24am    
https://msdn.microsoft.com/en-us/library/system.windows.forms.form.processcmdkey(v=vs.110).aspx
GKRISH04 12-Sep-16 6:33am    
Thank you brother :-)
There's another way to do this in WinForms without your having to override 'ProcessCommandKey, set Form 'KeyPreView to 'true, etc.; It's a little "hackish," but I hope it will round-out the discussions here.

1. Put a MenuStrip on the Form: if you don't want to use the MenuStrip to show some Menu Items, set its 'Visible Property to 'false.

2. Add a ToolStripMenuItem to the MenuStrip in the usual way. Set its ShortCutKeys to the key combination you want to trigger an event. Set its 'Visible Property to 'false.

3. Add a Click EventHandler to the ToolStripMenuItem, and write the code you wish executed when the Click happens.

Example:

I add a MenuStrip, 'menuStrip11. I add a ToolStripMenuItem, 'AltF4ShortCut with the key-combination set to 'Alt and 'F4. In the Designer view of the Form, you'll see:
C#
// AltF4ShortCut
// 
this.AltF4ShortCut.Name = "AltF4ShortCut";
this.AltF4ShortCut.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.F4)));
this.AltF4ShortCut.Size = new System.Drawing.Size(52, 24);
this.AltF4ShortCut.Text = "Save";
this.AltF4ShortCut.Visible = false;
this.AltF4ShortCut.Click += new System.EventHandler(this.AltF4ShortCut_Click);
Now, the Click EventHandler will fire no matter what Control has focus in the run-time Form; I could have the current focus set to a TextBox in a Panel in a UserControl, and it will still fire.

Here's what the Click EventHandler might look like:
C#
private void AltF4ShortCut_Click(object sender, EventArgs e)
{
    if (MessageBox.Show(
            "Save now ?",
            "Save Data",
            MessageBoxButtons.OKCancel,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button1,
            MessageBoxOptions.DefaultDesktopOnly
        )
        == DialogResult.OK)
    {
        //  code to save goes here
    }
}
I think it's safe to assume that a WinForm app with menu item with shortcut key-combinations defined installs its own lower-lever handler for Keyboard events, perhaps even using whatever the ProcessCmdKey over-ride uses under-the-hood (maybe WndProc ?).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900