How to make WinForms UserControl fill the size of its container

C#.NetWinformsMdiMultipage

C# Problem Overview


I am trying to create a multilayout main screen application. I have some buttons at the top that link to the main section of the application (e.g. management window for each entity in the Model)

Clicking any of these button displays the associated UserControl in a Panel. The Panel holds the UserControls that in turn holds the UI.

The WinForms UserControl does not have the Anchor or Dock property.

I have tried setting property of UserControl

AutoSize=True

And

private void ManageUsersControl_Load(object sender, EventArgs e)
{
        this.Width = this.Parent.Width;
        this.Height = this.Parent.Height;
}

But these did not work.
Note: I load this control dynamically at runtime

C# Solutions


Solution 1 - C#

Try setting the Dock property to Fill:

private void ManageUsersControl_Load(object sender, EventArgs e)
{
        this.Dock = DockStyle.Fill;
}

I would also set AutoSize to the default, I believe is False. See how that works ...

Solution 2 - C#

UserControl1 myusercontrol = new UserControl1();
            myusercontrol.Dock = DockStyle.Fill;//Dock Prope. Fill user Control Contrainer
            TabPage myTabPage = new TabPage();//New Tab Create
            myTabPage.Text = "Wel-Come Page";//Tab Header Txt
            myTabPage.Controls.Add(myusercontrol);
            tabControl1.TabPages.Add(myTabPage);

Solution 3 - C#

In the resize event user control .

 private void MyTextBox_Resize(object sender, EventArgs e)
        {
            this.Width = textBox1.Width;
            this.Height = textBox1.Height;
        }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioncodingbizView Question on Stackoverflow
Solution 1 - C#IAbstractView Answer on Stackoverflow
Solution 2 - C#rahuldmView Answer on Stackoverflow
Solution 3 - C#user3754071View Answer on Stackoverflow