How to intercept adding my custom control to a form?

Krzysztof Żelechowski 21 Reputation points
2021-09-16T09:23:02.207+00:00

I have a custom container control that must add a radio button to its container when added (due to the way radio buttons work in Windows Forms, the radio button must not be contained in the custom container or it will be independent of all other radio buttons on the form). However, ControlCollection .Add does not notify the added component that it is being added and handling Form .ControlAdded would have to be applied to every form the custom control is on, which seems like a nightmare. What can I do?

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,828 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Krzysztof Żelechowski 21 Reputation points
    2021-09-16T09:42:02.03+00:00
    0 comments No comments

  2. Jack J Jun 24,286 Reputation points Microsoft Vendor
    2021-09-17T06:04:53.14+00:00

    @Krzysztof Żelechowski , You could try to use Control.ControlAdded to intercept adding your custom control to a form.

    Here is a code example you could refer to.

    Custom Form:

    public partial class CustomForm : Form  
        {  
            public CustomForm()  
            {  
                InitializeComponent();  
                this.ControlAdded += CustomForm_ControlAdded;  
                 
            }  
      
            private void CustomForm_ControlAdded(object sender, ControlEventArgs e)  
            {  
                if (e.Control is MyRadioButton)  
                {  
                    this.Controls.Remove(e.Control);  
                    MessageBox.Show("You can not add your custom control called myRadionButton");  
                }  
            }  
      
            private void CustomForm_Shown(object sender, EventArgs e)  
            {  
                Console.WriteLine("est");  
            }  
        }  
    

    Form1:

    public partial class Form1 : WindowsFormsApp3.CustomForm  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form2 form = new Form2();  
                form.Show();  
            }  
      
            private void button2_Click(object sender, EventArgs e)  
            {  
                MyRadioButton radioButton = new MyRadioButton();  
                radioButton.Text = "test";  
                this.Controls.Add(radioButton);  
            }  
        }  
    

    Form2:

    public partial class Form2 : WindowsFormsApp3.CustomForm  
        {  
            public Form2()  
            {  
                InitializeComponent();  
            }  
      
            private void button2_Click(object sender, EventArgs e)  
            {  
                MyRadioButton radioButton = new MyRadioButton();  
                radioButton.Text = "test";  
                this.Controls.Add(radioButton);  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                Form3 form = new Form3();  
                form.Show();  
            }  
        }  
    

    Note: MyRadionButton is a custom control. I create a custom form. All the forms inherited from the Custom form. Then we can force all the forms not to add forms.

    Result:

    134130-7777.gif


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.