TextBox autocomplete and default buttons

前端 未结 4 520
Happy的楠姐
Happy的楠姐 2020-12-18 06:35

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

4条回答
  •  忘掉有多难
    2020-12-18 06:45

    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;
        }
    

提交回复
热议问题