Reputation: 287
i have 2 groupBoxes, both with one TextBox and one Button.
When i am in groupBox1 and write something in textBox1 and i Press the Enter Button, the Button in groupBox1 should be pushed, same Thing when i am in groupBox2 and write something there in textBox2.
Something like
if (Focus is on groupbox1 == true)
this.AcceptButton = button1;
else if(Focus is on groupbox2 == true)
this.AcceptButton = button2;
Upvotes: 2
Views: 169
Reputation: 525
Use the enter event for toggling the focus
private void textBox1_Enter(object sender, EventArgs e)
{
AcceptButton = button1;
}
private void textBox2_Enter(object sender, EventArgs e)
{
AcceptButton = button2;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("First button clicked");
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show("Second button clicked ");
}
Upvotes: 1
Reputation: 1133
You can subscribe on TextBox
KeyDown
event:
TextBox tb = new TextBox();
tb.KeyDown += new KeyEventHandler(tb_KeyDown);
static void tb_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key is down
}
}
Upvotes: 0