Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextBox autocomplete and default buttons

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?

like image 260
Zhenya Avatar asked Oct 14 '22 15:10

Zhenya


2 Answers

Do not assign AcceptButton and CancelButton form properties. Set DialogResult in the buttons OnClick event.

like image 126
arbiter Avatar answered Oct 20 '22 00:10

arbiter


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;
    }
like image 32
TheVillageIdiot Avatar answered Oct 19 '22 23:10

TheVillageIdiot