Im new here in Coding and im Trying to activate my button from a Textbox by pressing enter. I tried to google for the Answer but Visual Studios say it doesnt work.
The most common Thing i read while searching for an Answer was the code down below. Now the problem is Visual Studios shows me the Keycode
line as error. Can somebody help me figure out why its an Error?
if (e.Keycode == Keys.Enter)
{
button1_Click(this, new EventArgs());
}
CodePudding user response:
- You should register to the
KeyDown
event, in case you haven't. You might already have something like that in your code. It is done by either using the GUI options in the designer, or programatically using something like:
this.textBox1.KeyDown = new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
In the event handler,
Keycode
should be spelled with capital C, i.e.:KeyCode
.An example for a complete event handler method:
private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { button1_Click(this, new EventArgs()); } }