Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need something like Accept Button in User Control

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 the Accept Button of a Form works.

I cannot make the button to be focused, since I need other controls to be focused with tab change.

Any help is really appreciated :)

like image 633
Yalda Avatar asked Oct 22 '13 08:10

Yalda


1 Answers

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);
    }
like image 159
Jaycee Avatar answered Oct 13 '22 00:10

Jaycee