Need something like Accept Button in User Control

后端 未结 2 2101
旧时难觅i
旧时难觅i 2021-02-15 04:20

I need to have a Button on a UserControl (not Form) in a windows application to respond to \"Enter\" hit, the way a button which is set as

相关标签:
2条回答
  • 2021-02-15 04:25

    The AcceptButton is a property of Form and cannot be used for the UserControl. You can simply override ProcessCmdKey though and this will work so long as the user control has focus. Otherwise you will need to use the AcceptButton of the form separately or override the ProcessCmdKey in the form if you have multiple controls which could be active.

      protected override bool ProcessCmdKey(ref Message msg, Keys keyData) 
      {
          if (keyData == Keys.Enter) 
          {
            button.PerformClick();
            return true;
          }
          return base.ProcessCmdKey(ref msg, keyData);
        }
    
    0 讨论(0)
  • 2021-02-15 04:29

    If you set the Modifier property on the button to "Public", you can use the button as AcceptButton on the form. Unfortunately you cannot do it design-time with the properties window of Visual Studio, but you can do it in code.

        public Form1()
        {
            InitializeComponent();
            this.AcceptButton = userControl11.button1;
        }
    
    0 讨论(0)
提交回复
热议问题