question

Krzysztofelechowski-8949 avatar image
0 Votes"
Krzysztofelechowski-8949 asked Krzysztofelechowski-8949 commented

How to intercept adding my custom control to a form?

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
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Krzysztofelechowski-8949 avatar image
0 Votes"
Krzysztofelechowski-8949 answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

JackJJun-MSFT avatar image
0 Votes"
JackJJun-MSFT answered Krzysztofelechowski-8949 commented

@Krzysztofelechowski-8949, 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.







image.png (9.5 KiB)
666.gif (214.6 KiB)
7777.gif (829.4 KiB)
· 9
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I have explained why this is not a good idea in my question.

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT Krzysztofelechowski-8949 ·

@Krzysztofelechowski-8949 ,

it is being added and handling Form .ControlAdded would have to be applied to every form the custom control is on.

Do you mean that this sentence explained why Form.ControlAdded is not a good idea?

I am not full understand this sentence. What do you feel dissatisfied?


·


0 Votes 0 ·

Because that means I would have to modify every form that my control is used on.

0 Votes 0 ·
Show more comments