Click here to Skip to main content
Click here to Skip to main content

Control Resizing on a UserControl in WinForms

By , 21 May 2010
 
Let's say you have a UserControl, and on that user control, you have placed a control (any control), setting the Dock property to Fill. From hereon, we will refer to this control as the "filling control".
 
Let's further assume that you've placed a Panel (or other type of appropriate container) onto a Form, and set the Anchor property of the container to grow/shrink when you resize the form.
 
Finally, you would probably programatically add the user control (that contains the filling control) to the panel.
 
If you perform the steps outlined above, you will observe that the filling control will NOT resize itself to the container control, even though the container control will happily obey the Anchor property setting.
 
The problem is that the designer doesn't allow you to set either the Dock or the Anchor properties. You MUST manually set the appropriate property in the UserControl Load event handler (or someplace else equally appropriate).
 
I struggled with this for two hours (trying to capture Size events, and other things) before stumbling on the solution. I had completely missed the fact that there were no Dock/Anchor property settings in the designer for UserControl objects.



License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
Member
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionMissed something?memberPSU Steve2 Mar '10 - 3:01 
I'm a bit confused... the hierarchy is:
 
FORM contains PANEL (anchored) contains USER CONTROL contains FILLING CONTROL (dock Fill).
 
Of course the user control won't resize because even though the PANEL resizes due to its anchor settings, there has been nothing set to ask the UC to resize. Wouldn't you just want to set the UC to dock Fill too?
AnswerRe: Missed something?memberJohn Simmons / outlaw programmer3 Mar '10 - 3:43 
My point was that there is no way to do this *in the designer* and that you have to remember to go to the actual source code to establish the desired behavior.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

GeneralRe: Missed something?memberPSU Steve3 Mar '10 - 4:25 
You can set this in the designer. Let's see if I can post enough to show (I wish I could upload a ZIP with the solution...)
 
I have a UserControl (MyUserCtrl) with a FULL docked TextBox (multiline so it fills the control). The UserControl has a padding value so the TextBox doesn't go all the way to the edges, plus a blue background so you can see the UserControl when displayed on the main form. The main form (Form1) contains an anchored Panel control. The Panel control contains an instance of MyUserCtrl docked FULL. When you resize the form, the panel resizes due to the anchoring, the UserControl resizes due to it being docked, and the TextBox resizes due to it being docked too. I didn't have to do anything in the source -- everything is in the designer.
 
I think this is what you're trying to do. If I missed the boat, my apologies...
 
Partial Class Form1
    Inherits System.Windows.Forms.Form
 
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.MyUserCtrl1 = New ResizeTest.MyUserCtrl
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'Panel1
        '
        Me.Panel1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left) _
                    Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.Panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.Panel1.Controls.Add(Me.MyUserCtrl1)
        Me.Panel1.Location = New System.Drawing.Point(24, 22)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(374, 259)
        Me.Panel1.TabIndex = 0
        '
        'MyUserCtrl1
        '
        Me.MyUserCtrl1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.MyUserCtrl1.Location = New System.Drawing.Point(0, 0)
        Me.MyUserCtrl1.Name = "MyUserCtrl1"
        Me.MyUserCtrl1.Size = New System.Drawing.Size(372, 257)
        Me.MyUserCtrl1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(430, 307)
        Me.Controls.Add(Me.Panel1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)
 
    End Sub
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents MyUserCtrl1 As ResizeTest.MyUserCtrl
End Class
 
Partial Class MyUserCtrl
    Inherits System.Windows.Forms.UserControl
 
    'UserControl overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Dock = System.Windows.Forms.DockStyle.Fill
        Me.TextBox1.Location = New System.Drawing.Point(8, 8)
        Me.TextBox1.Multiline = True
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(468, 336)
        Me.TextBox1.TabIndex = 0
        Me.TextBox1.Text = "This textbox is docked FILL on the user control"
        '
        'MyUserCtrl
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.BackColor = System.Drawing.Color.Blue
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "MyUserCtrl"
        Me.Padding = New System.Windows.Forms.Padding(8)
        Me.Size = New System.Drawing.Size(484, 352)
        Me.ResumeLayout(False)
        Me.PerformLayout()
 
    End Sub
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class

GeneralRe: Missed something?memberJohn Simmons / outlaw programmer3 Mar '10 - 11:50 
I try not to do *anything* in VB...
 
Smile | :)
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130523.1 | Last Updated 21 May 2010
Article Copyright 2010 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid