Adding UserControl to SplitContainer not Filling panel

147 views Asked by At

I have two split containers. splitContainer_Top & splitContainer_Bottom - which is inside of the splitContainer_Top so that I have three panels to work with.

Here's a screen example of the splitcontainer panels:

enter image description here

I'm am trying to replace the UserControl in splitContainer_Bottom.Panel1 when a button is clicked.

Here is my code:

private void button_Settings_Click(object sender, EventArgs e)
{
  var settings2 = new SettingsControl();
  var frm = (MainMenu)Application.OpenForms["MainMenu"];
  frm.ChangeControl(settings2);
}


public void ChangeControl(Control newControl)
{
  if (newControl == null)
  {
    throw new ArgumentNullException("Control passed was not valid.");
  } 
  
  foreach (Control control in splitContainer_Bottom.Panel1.Controls)
  {
    splitContainer_Bottom.Panel1.Controls.Remove(control);
  }

  splitContainer_Bottom.Panel1.Controls.Add(newControl);
  newControl.Location = new Point(0, 0);
  newControl.Dock = DockStyle.Fill;
  newControl.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
  
}

I'm doing it this way since the button is on another usercontrol and not on the form directly.

I'm trying to fill the splitContainer_Bottom.Panel1 with the usercontrol MainControl but it's not filling the panel1 correctly. If I add the MainControl at design time, it works fine but at runtime, it's not filling.

Here's an example of what it is doing at runtime:

enter image description here

This is what I'd like it to do, which only works at design time:

enter image description here

I have this in the ChangeControl method but it seems to be ignoring it:

newControl.Dock = DockStyle.Fill;

Anyone have any suggestions?

0

There are 0 answers