Click here to Skip to main content
16,020,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have created windows application. Form having 10 textbox and three buttons.
My requirement is that, on Up and Down keys, cursor should focus to textox according to their
tabindex(0 to 9) , also i want to close application on escape key and on enter key should save the record.

I have never worked on keys events.
How do i do?
Please help me.

Thanks
Posted
Updated 19-Sep-14 1:19am
v3
Comments
[no name] 19-Sep-14 9:06am    
You do this like everything else. Handle the keypress events and do what you want to do with it.
Sergey Alexandrovich Kryukov 19-Sep-14 10:50am    
Wrong event, by the way. I answered the question.
—SA
[no name] 19-Sep-14 11:09am    
And how, exactly, are OnKeyUp and OnKeyDown not key press events?
Sergey Alexandrovich Kryukov 19-Sep-14 11:42am    
First of all, names you mentioned are not event names at all.
Your notion of "press" is wider than that used in .NET, where there are low-level events KeyUp and KeyDown, and higher-level event KeyPress. For arrows, the event KeyPress is not suitable. Usually, if some one says "key press event" (not "keyboard event"), "KeyPress" event us assumed. This is what I mean.
—SA
Ganesh11021423 19-Sep-14 9:15am    
Thanks Wes Aday

Do you have any sample or reference ?

Thanks

This is not KeyPress event; these are lower-level keyboard events KeyDown, KeyUp, where you work not with characters (they are not yet calculated), but the key events. The information on the key is passed to your handler in a event argument parameter. Alternatively, you can override virtual methods OnKeyDown, OnKeyUp. Further detail depend on the application type. For container controls or UI elements, you may need to handle respective Preview*** events.

—SA
 
Share this answer
 
try this sample code
C#
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
       {
           if (keyData == (Keys.Escape))
           {
               this.Close();
               return true;
           }
           if (keyData == (Keys.Enter))
           {
             ////save code here
               return true;
           }
           if (keyData == (Keys.Down))
           {
               SendKeys.Send("{TAB}");
               return true;
           }
           if (keyData == (Keys.Up))
           {
               SendKeys.Send("+{TAB 1}");
               return true;
           }
           return base.ProcessCmdKey(ref msg, keyData);
       }


credits:http://www.autohotkey.com/docs/commands/Send.htm[^]

happy coding
good luck ;-)
 
Share this answer
 
Comments
Ganesh11021423 22-Sep-14 1:08am    
Thanks a lot George4986

Its working good.

Thanks
george4986 22-Sep-14 1:20am    
u r always welcome ;-)
Ganesh11021423 22-Sep-14 5:15am    
Thanks to all also..

Wes Aday, -SA

Thanks

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