Reputation: 25
I'm trying to get my UserControl
that I have on the left side in my SplitPanel
to open up a new user control in the right side. The buttons above open a UserControl
in the left side; however, I can't get the buttons in the left side to open a new UserControl
in the right side of the SplitPanel
. Appreciate any help I can get.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
#region Buttons
private void button1_Click(object sender, EventArgs e)
{
if (!splitContainer1.Panel1.Controls.Contains(left_Settings.Instance))
{
splitContainer1.Panel1.Controls.Add(left_Settings.Instance);
left_Settings.Instance.Dock = DockStyle.Fill;
left_Settings.Instance.BringToFront();
}
else
{
left_Settings.Instance.BringToFront();
}
}
-
public partial class left_Settings : UserControl
{
private static left_Settings _instance;
public static left_Settings Instance
{
get
{
if (_instance == null)
{
_instance = new left_Settings();
}
return _instance;
}
}
public left_Settings()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (!splitContainer1.Panel2.Controls.Contains(right_settings_start.Instance))
{
splitContainer1.Panel2.Controls.Add(right_settings_start.Instance);
right_settings_start.Instance.Dock = DockStyle.Fill;
right_settings_start.Instance.BringToFront();
}
else
{
right_settings_start.Instance.BringToFront();
}
}
private void button2_Click(object sender, EventArgs e)
{
//Some code
}
}
-
public partial class right_settings_start : UserControl
{
private static right_settings_start _instance;
public static right_settings_start Instance
{
get
{
if (_instance == null)
{
_instance = new right_settings_start();
}
return _instance;
}
}
public right_settings_start()
{
InitializeComponent();
}
}
Upvotes: 0
Views: 2129
Reputation: 2683
My code does something similar, but using panels and directly instantiating the controls rather than using an internal "instance" property.
Control myControl = new Control();
//Do initialization stuff
RightPanel.Controls.Add(myControl);
//Do Docking and Layout stuff
myControl.Show();
From what I can see the only thing you are missing is the Control.Show()
command. Make certain that your control actually gets loaded, this is more than just adding it and bringing it to the front, you have to tell the code to Show it. Setting Breakpoints inside the Control_Load
event should let you make sure that its actually showing up.
update
The other reason this may not work is that the SplitContainer itself exists as an object on the form, but you are trying to access it from inside the left_settings object...which can't see it. Try referencing it as this.ParentForm.splitContainer1
instead and see if it can find it. You might need to make it a public object if left_settings can't see it, or provide some other accessor for it.
Upvotes: 1