On my main Form I have a panel with dynamically generated UserControl in it. I want to clear all the UserControls on button click on another UserControl.
Here's what I've tried:
private Form1 mainForm = null;
public ProductCategoryUC(Form1 callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
public ProductCategoryUC(string Text, Image imagee)
{
InitializeComponent();
categorybtn.LabelText = Text;
categorybtn.Image = imagee;
}
private void categorybtn_Click(object sender, EventArgs e)
{
foreach (Control item in mainForm.productsContainer1.Controls.OfType<UserControl>())
{
item.Controls.Clear();
}
}
But getting this error:
System.NullReferenceException'