简体   繁体   中英

How to make WinForms UserControl fill the size of its container

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 (eg 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

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 ...

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);

In the resize event user control .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM