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
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
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();
}
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:
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
}
private void SetDefault(Button myDefaultBtn)
{
this.AcceptButton = myDefaultBtn;
}