I have a tried my best to create a dummy project as an example for this question. In my Case i have multiple UserControls That i load into my main Form. What I am Trying to do is I have defined a Method ShowControl()
in main form that adds usercontrols to the panel.
public void Showcontrol(UserControl ctrl)
{
if (!pnl_ucContainer.Controls.Contains(ctrl))
{
pnl_ucContainer.Controls.Add(ctrl);
ctrl.Dock = DockStyle.Fill;
ctrl.BringToFront();
}
else
{
ctrl.BringToFront();
}
}
Let's Leave this code for a while and have a look at my UserControl Structure. What I have done is I have Used a Traditional Practice to implement Singleton Design Pattern. Like The Following for each UserControl in My Project.
namespace DemoApp
{
public partial class uc_HomePage : UserControl
{
static uc_HomePage _obj;
public static uc_HomePage GetInstance
{
get
{
if (_obj == null)
{
_obj = new uc_HomePage();
}
return _obj;
}
}
public uc_HomePage()
{
InitializeComponent();
}
}
}
To Call It From a Form1 Control Event I have this Simple Code.
private void Helper_Click(object sender, EventArgs e)
{
Showcontrol(uc_HomePage.GetInstance);
}
WHich Works For Me! As You Can see in the Following Image
Meanwhile When I want to add Another UserControl to the Same Panel and Bring it to Front I want to trigger the ShowControl()
Method From the uc_HomePage
UserControl.
Like Following.
private void uc_Button_Click(object sender, EventArgs e)
{
// Since Form1 is a Singleton Class
// I will call the method like this.
Form1.GetInstance.Showcontrol(uc_SettingsPage.GetInstance);
}
Well Nothing Happens When I call it from a usercontrol Button.
Please Help Me.....
EDIT: WHy The Down Votes Can Anyone Explain so that i can improve