@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:
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.