Reputation: 23
It seems I came to a different way of designing structure. My project has 3 main forms (form parent, form menu, and form childs).
The Parent has a left panel to contain the menu form and another panel for containing the child forms.
Working like:
The parent form opens the menu form in the left panel. While the menu is shown on the left panel, clicking the menu should open a child in the main panel. (both panels are in the parent form).
public Home()
{
InitializeComponent();
panelmenus.Controls.Clear();
frmmenus menu = new frmmenus();
menu.TopLevel = false;
menu.Dock = DockStyle.Fill;
panelmenus.Controls.Add(menu);
menu.Show();
}
private void btnsetting_Click(object sender, EventArgs e)
{
Forms.frmEmployee employee = new Forms.frmEmployee()
{
Dock = DockStyle.Fill,
TopLevel = false,
};
Home main = new Home();
main.pncontainer.Controls.Add(employee);
employee.Show();
}
Is it possible to do this way or not? Because I came to this way already.
If there is any other way to do this problem, give me please.
I took few hours for this issue.
Much thank you to all.
Upvotes: 0
Views: 258
Reputation: 19641
If you only have one instance of the parent form (i.e., Home
), you may use Application.OpenForms
to find and access that instance. Then, you can use it (instead of creating a new instance) to display the employee form:
private void btnsetting_Click(object sender, EventArgs e)
{
Home main = Application.OpenForms.OfType<Home>().FirstOrDefault();
if (main != null)
{
Forms.frmEmployee employee = new Forms.frmEmployee()
{
TopLevel = false,
Dock = DockStyle.Fill
};
main.pncontainer.Controls.Add(employee);
employee.Show();
}
}
Note that you'll need to set the Modifiers
property of pncontainer
to either Public
or Internal
for this to work.
If you don't want to use Application.OpenForms
or you may have multiple instances of Home
, there are a few ways to still access the right instance of Home
. One way would be using the ParentForm
property like this:
private void btnsetting_Click(object sender, EventArgs e)
{
Home main = this.ParentForm as Home;
if (main != null)
{
Forms.frmEmployee employee = new Forms.frmEmployee()
{
TopLevel = false,
Dock = DockStyle.Fill
};
main.pncontainer.Controls.Add(employee);
employee.Show();
}
}
Upvotes: 2
Reputation: 39122
Here's one possible approach.
The Parent of your instance of frmmenus
will be a Panel. From that Panel you can call FindForm() to get a reference to the main Home
instance. Next you can use Controls.Find() to search for your Panel called "pncontainer". From there, simply clear the panel and add your desired form instance to it:
private void btnsetting_Click(object sender, EventArgs e)
{
frmEmployee employee = new frmEmployee()
{
Dock = DockStyle.Fill,
TopLevel = false,
};
Form frm = this.Parent.FindForm();
Control match = frm.Controls.Find("pncontainer", true).FirstOrDefault();
if (match != null && match is Panel)
{
Panel p = (Panel)match;
p.Controls.Clear();
p.Controls.Add(employee);
employee.Show();
}
}
Upvotes: 1