I am creating an application using WinForms. I have panel
in which I show a user control
. Inside this user control
I have a button
. When I click the button, I want to clear the panel
and show a different user control
. I am trying to do that using the following code:
private void btnCreateOffer_Click(object sender, EventArgs e)
{
var myControl = new WindowsFormsDemo.View.CreateOffer();
MockUpForm.panMain.Controls.Clear();
MockUpForm.panMain.Controls.Add(myControl);
}
This works from the buttons placed directly in the parrent form, but when I use in inside the user control
, it says:
'MockUpForm.panMain' is inaccessible due to its protection level
I suppose it has something to do with private/public classes. But I would rather have the "correct" solution, as opposed to just changing everything to public.
Any suggestions on how this is usually done?
panMain.Dispose();
destroys the panel, which is not what you want. You should dispose each control in the panel, so try a while-loop:while (panMain.Controls.Count > 0) panMain.Controls[0].Dispose();
– LarsTech