Click here to Skip to main content
15,913,773 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hi All,

I'm looking for code for a KeyPress event.

Can any help me or give me the code for KeyPress event for textbox?
Posted
Updated 12-Apr-11 21:19pm
v2
Comments
Michel [mjbohn] 13-Apr-11 2:38am    
Well, keyPress is an event. And it's triggered by pressing keys. That's it. It's up to you to use this event. What's your prob? What do you want to achieve?
girish sp 13-Apr-11 2:42am    
wat you actually want to do in keypress event??
IndrajitDasgupat 13-Apr-11 3:04am    
after enter data to the text box and press enter key it should retrieve record for that particular data from table
Dalek Dave 13-Apr-11 3:19am    
Edited for Grammar and Syntax.
Sandeep Mewara 13-Apr-11 6:36am    
No effort!

Textbox is having KeyPress event.
So you can write your logic inside that event.
 
Share this answer
 
Comments
Dalek Dave 13-Apr-11 3:19am    
Simple and Right.
public textbox1_KeyPress(object sender,KeyPressEventArgs e)
{
e.KeyChar=char.ToUpper(e.KeyChar);//converts the entered char to upper
}



if you want to do any validations of the pressed key,you can do it in keydown event and pass a boolean value to keypress event,based on the bool value you can perform the operation you need



Hope it helps!!
 
Share this answer
 
v2
Comments
Dalek Dave 13-Apr-11 3:19am    
Good Call.
Sergey Alexandrovich Kryukov 13-Apr-11 3:35am    
Girish, here is my advice: always show how the handler is added to the event. I come across few questions when the users write some code like in your sample but fail to understand it won't ever called by itself unless added to the event instance...
--SA
girish sp 13-Apr-11 6:13am    
sure thing..
Sergey Alexandrovich Kryukov 13-Apr-11 6:25am    
Thank you for understanding...
--SA
myEditBox.KeyPress += (sender, eventArgs) => {
  MessageBox.Show(
    "You can examime eventArgs to see what key is pressed and act accordingly (even cancel the press)");
};


—SA
 
Share this answer
 
v2

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