Need something like the Accept button in User Control

I need to have a Button in UserControl (not Form ) in a Windows application to respond to an "Enter" hit, like a button that is set as Accept Button for Form works.

I can't get the button to focus, as I need the other controls to focus on changing the tabs.

Any help really appreciated :)

+6
source share
2 answers

AcceptButton is a form property and cannot be used for UserControl. You can simply override ProcessCmdKey, although this will work as 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 several controls that can 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); } 
+11
source

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

  public Form1() { InitializeComponent(); this.AcceptButton = userControl11.button1; } 
0
source

Source: https://habr.com/ru/post/956467/