Capture “Enter” key in windows applicaiton

When we use keyboard in our daily uses, we do  approx 30% of our work by pressing  Enter key only .But sometimes when we need to capture these keys in application it creates a mess .. so here is a small and quick post to capture all these silly but very important key handling . 

Scenario 

I am assuming that on my windows for i have two controls a TextBox (id = txtSearchBox) and  a Button (id = btnSearch) control.If   type any thing in the search box and press enter key than it should show me a message saying “You have hit Enter key.”

Steps: 

(1) Create click event for Button 

private void btnSearch_KeyPress(object sender, KeyPressEventArgs e) 

 {  CallMessageBox();     } 

private void CallMessageBox() 

{       MessageBox.Show(“You have hit Enter key.”);} 

(2) Create and KeyPress Event of txtSearchBox from the property Window 

private void txtSearchBox_KeyPress(object sender, KeyPressEventArgs e) 

  

 {   if (e.KeyChar == Keys.Enter )   

   {CallMessageBox();   // Or do your work here } 

   }

 

 

That’s it .

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s