View Single Post
Old Jul 1st, 2005, 4:46 PM   #6
hoffmandirt
Hobbyist Programmer
 
hoffmandirt's Avatar
 
Join Date: Jul 2005
Location: PA
Posts: 125
Rep Power: 4 hoffmandirt is on a distinguished road
Send a message via AIM to hoffmandirt
One more way.

Here is one more way to handle the enter key and any other key.

For this example I have one button on a form. The onclick event of the button is as follows:

private void btnClickMe_Click(object sender, System.EventArgs e)
{
	MessageBox.Show("Hello World!");
}

This just displays a message box when the user clicks the button.

Now select the form and add this to the KeyUp event:

private void Form1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
{
	if(e.KeyCode == Keys.Enter)
	{
		this.btnClickMe_Click(new System.Object(), System.EventArgs.Empty);
	}
}

Simple right? You can call any key that you would like here. Just replace Keys.Enter with whatever key you would like. Basically what happens is when the user presses the enter key, it calls btnClickMe's click event and passes a blank object and empty args. You can use this to fire an event from anywhere.
hoffmandirt is offline   Reply With Quote