TextBox autocomplete and default buttons

前端 未结 4 511
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:58

    Another option is to derive your own custom TextBox class that performs validation when Enture/Return is pressed:

    public class MyTextBox : TextBox
    {
        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (keyData == Keys.Enter || keyData == Keys.Return)
            {
                // Perform validation here
                return true;
            }
            else
                return false;
        }
    }
    

提交回复
热议问题