How do I give Enter and Run my button SEARCH

0

I created a program that has the Client Search field that loads the Clients in the listView. I want to search by filling in the TextBox and pressing the Enter Key

    
asked by anonymous 14.04.2017 / 17:47

3 answers

2

Mode 1

You can use the KeyDown event. Instead of using the key code, make use of enumeration of keys .

private void textBox1_KeyUp(object sender, KeyEventArgs e) {
    if (e.KeyCode == Keys.Enter)
        ExecutarBusca();
}

Mode 2

Another alternative - make use of the AcceptButton . The button that is assigned to this property will be executed automatically by pressing the Enter key.

By code:

private void Form1_Load(object sender, EventArgs e) {
    this.AcceptButton = btnPesquisar;
}

Or through the interface:

    
19.04.2017 / 15:56
0

use the TextPress KeyPress event

  private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
  {
       if (e.KeyChar == 13)
       {
          //Executa  a pesquisa
       }
  }

Another option, which I use, is the TextChanged next to a timer, which searches automatically when filling in the textbox:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    timerPesquisa.Enabled = false;
    timerPesquisa.Enabled = true;
}

private void timerPesquisa_Tick(object sender, EventArgs e)
{
    timerPesquisa.Enabled = false;
    //Executa a pesquisa
}
    
14.04.2017 / 18:23
0
private void SetDefault(Button myDefaultBtn)
{
   this.AcceptButton = myDefaultBtn;
}
    
19.04.2017 / 16:01