0

Image of Form

When a user enters text into one of the search boxes, I would like them to be able to hit Enter to perform the same function as clicking a button would.

For example if they were typing in the boxed labeled "Find Username" - they would have the option to either press Enter or click the "Show Users" button. If they were typing in the box labeled "Find First Group" then pressing Enter would then be the equivalent of selecting the "Show 1st Groups" button.

Does anyone know how I can achieve this?

Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
Oli Ur M80
  • 75
  • 2
  • 9

2 Answers2

1

You can use the KeyDown event of the TextBox and check which key has been pressed. Inside the event you can just call the event handler of the button that you which to be pressed.

private void findUserNameTextBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        Show_UsersButton_Click(this, new EventArgs());
    }
}
Mong Zhu
  • 23,309
  • 10
  • 44
  • 76
0

Put in a KeyEventHandler to the TextBox

$textBox1.add_KeyDown($textbox1_KeyDown)
$textbox1_KeyDown=[System.Windows.Forms.KeyEventHandler]{
    if($_.KeyCode -eq 'Enter')
    {
        ...
    }
}

EDIT Forgot to add the code for associating the event handler to the object.

AtomicFireball
  • 341
  • 2
  • 9