How do I give Enter and Run my SEARCH button

Asked

Viewed 7,923 times

0

I created a program that has the Client Search field that loads the Clients in listview.. I want to search by filling in the Textbox and Pressing the Enter Key

  • Adding an action(or Listener, I don’t know what it’s called in c#) on the component to monitor when the button is pressed?

  • 1

    EventHandler, @diegofm. But the common term is Istener himself.

  • Like a google search... type in the field and hit Enter.. No need to click with the mouse or Tab to select the button

  • solved your problem ?

4 answers

4


Mode 1

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

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

Mode 2

Alternative - make use of the property 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 by the interface:

AcceptButton

0

private void SetDefault(Button myDefaultBtn)
{
   this.AcceptButton = myDefaultBtn;
}

0

use the textbox Keypress event

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

another option, which I use, is Textchanged next to a timer, which the search is done automatically when filling 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
}

-2

private void textBox1_KeyPress(Object Sender, Keypresseventargs and) { if (e.Keychar == 13) { btnPsearch.Performclick(); //it will simulate a click on the button. } }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.