There is some control:

public partial class UserControl1 : UserControl { public Button buttonExit; public UserControl1() { InitializeComponent(); Controls.Add(buttonExit = new Button() { Text = "Exit" }); buttonExit.Click += (s, e) => { (s as Control).FindForm().DialogResult = DialogResult.Cancel; }; } protected override void OnParentChanged(EventArgs e) { base.OnParentChanged(e); Form f = FindForm(); if (f!= null) f.CancelButton = buttonExit; } } 

Those. the form must receive buttonExit as a close button by Esc

In the designer I throw UserControl1 on the form. CancelButton of this form still remains (none)

Tell me where to dig

  • Take a look at debugging in which instance you are changing (see Name). I understand that you are changing the property of the instance and not the class. - Alexsandr Ter
  • @AlexsandrTer did not quite understand your comment. How to start debugging if I work in the VisualStudio designer? Yes, the property should change in the form instance - the parent form of this UserControl - kvvk

0