Click here to Skip to main content
15,889,874 members
Articles / .NET
Tip/Trick

Control Resizing on a UserControl in WinForms

Rate me:
Please Sign up or sign in to vote.
4.95/5 (7 votes)
21 May 2010CPOL 78.7K   7   7
Control resizing is a bit more mysterious than it might appear at first glance.
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)


Written By
Software Developer (Senior) Paddedwall Software
United States United States
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.

Comments and Discussions

 
GeneralMy vote of 5 Pin
anon19319-May-13 16:54
anon19319-May-13 16:54 
GeneralReason for my vote of 1 Not a very clear example and respons... Pin
digger6927-Sep-10 6:15
digger6927-Sep-10 6:15 
JokeDifficult to find out what you mean Pin
johannesnestler25-May-10 5:57
johannesnestler25-May-10 5:57 
QuestionMissed something? Pin
PSU Steve2-Mar-10 3:01
professionalPSU Steve2-Mar-10 3:01 
AnswerRe: Missed something? Pin
#realJSOP3-Mar-10 3:43
mve#realJSOP3-Mar-10 3:43 
GeneralRe: Missed something? Pin
PSU Steve3-Mar-10 4:25
professionalPSU 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? Pin
#realJSOP3-Mar-10 11:50
mve#realJSOP3-Mar-10 11:50 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.