How to make WinForms UserControl fill the size of its container
Asked Answered
O

3

53

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

Oxpecker answered 3/6, 2012 at 15:27 Comment(5)
UserControl does have a Dock property - it inherits that property from Control. See the Properties section of msdn.microsoft.com/en-us/library/…Misdo
the above statement "The winforms usercontrol does not have the "Anchor" or "Dock" property" was wrong. I was looking for "Dock" while on the UserControl. It should only appear for controls(children) added to the UserControl.Oxpecker
@Misdo yes it makes sense, but why it's not present in the designer property grid?Linson
because at this point, the UserControl was on its own, not added to any parent that would require the fill type - it should have shown if I was on the Panel control and I added the UserControlOxpecker
I think there is some confusion here between System.Windows.Forms.UserControl (which does have a Dock property) and System.Windows.Controls.UserControl (which doesn't).Therapy
M
105

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

Massasauga answered 3/6, 2012 at 15:30 Comment(9)
From the OP: The winforms usercontrol does not have the "Anchor" or "Dock" property.Hunter
@PaulSasik: The OP was probably mistaken; otherwise, it would not be a UserControl.Scrub
OK this really surprise me. Do you know why this property is not in the UserControl designer property grid? By the way, it works perfectly in runtime.Linson
this worked though i have a flicker - but it's the answer to my question. Thanks All.Oxpecker
@tunmisefasipe: glad to help ...you might try setting DoubleBuffered = true ...IIRC. Yeah, it's kind of frustrating to not have the Dock property on the designer. But, it makes sense - when in the Designer working on the UserControl itself, what container would it use to maximize itself? There is no container to Fill to.Massasauga
System.Windows.Controls.UserControl (used for WPF) indeed does not have a Dock property, so this cannot be the correct answer.Therapy
@yoyo: the OP is using WinForms.Massasauga
Thanks @IAbstract, you are correct. I found myself here while investigating a similar problem. My application contains both WPF and WinForms elements, and I had confused myself with the two different UserControl classes, only one of which has the Dock property.Therapy
To be clear, it works. With winforms forms.UserControl. You need to create Load behavior and add the "this.Dock = DockStyle.Fill" into.Vickyvico
A
0
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);
Allegorist answered 9/7, 2014 at 7:35 Comment(0)
A
-4

In the resize event user control .

 private void MyTextBox_Resize(object sender, EventArgs e)
        {
            this.Width = textBox1.Width;
            this.Height = textBox1.Height;
        }
Antiserum answered 6/7, 2016 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.