TextBox autocomplete and default buttons

前端 未结 4 512
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 07:08

    instead to Accept and Cancel buttons you can go for the following approach:

    1. Set KeyPreview property for the form to true.
    2. Handle the KeyDown event of the form, in the handler method you can have something similar to the below code

      switch (e.KeyCode)
      {
          case Keys.Enter:
          {
              if (!txtAuto.Focused)
              {
                  Save();
              }
              break;
          }
          case Keys.Escape:
          {
              if (!txtAuto.Focused)
              {
                  Close();
              }
              break;
          }
      }
      

提交回复
热议问题