I have a .NET TextBox with AutoComplete feature on the form. The form has also AcceptButton and CancelButton defined. If I try to commit a suggestion with Enter key or close drop down with Esc, my form closes. How can I prevent this behavior?
Do not assign AcceptButton and CancelButton form properties. Set DialogResult in the buttons OnClick event.
Simple way is to remove AcceptButton and CancelButton properties while you are in auto-complete textbox:
public Form1()
{
InitializeComponent();
txtAuto.Enter +=txtAuto_Enter;
txtAuto.Leave +=txtAuto_Leave;
}
private void txtAC_Enter(object sender, EventArgs e)
{
AcceptButton = null;
CancelButton = null;
}
private void txtAC_Leave(object sender, EventArgs e)
{
AcceptButton = btnOk;
CancelButton = btnCancel;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With