Autosizing Controls

Posted on 2004-11-18
Last Modified: 2010-04-23
Am i missing an easy way to do this? I want to make my controls dynamically resize on a form-resize so that they fit the form nomatter what size it is.  I could work it out programatically based on each controls size and location relative to the forms, but its a lot of work if theres an easier inbuilt option to do so...

Question by:jazduck
    LVL 19

    Accepted Solution

    see the ANCHOR and DOCK properties..ANCHOR is more relavent... If you are designing a form that the user can resize at run time, the controls on your form should resize and reposition properly. To resize controls dynamically with the form, you can use the ANCHOR property of Windows Forms controls.
    LVL 10

    Expert Comment

    try this code...

    Public Class Form1
        Inherits System.Windows.Forms.Form
        Private OrgLabelHeight = 23, OrgLabelWidth = 100, OrgButtonWidth = 136, OrgButtonHeight As Integer = 24
        Private OrgFormHeight = 400, OrgFormWidth = 400

    'where these variables store the dimensions given in desgin mode
    'i.e. original dimensions

    #Region " Windows Form Designer generated code "

        Public Sub New()

            'This call is required by the Windows Form Designer.

            'Add any initialization after the InitializeComponent() call

        End Sub

        'Form overrides dispose to clean up the component list.
        Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
            If disposing Then
                If Not (components Is Nothing) Then
                End If
            End If
        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.
        Friend WithEvents Label1 As System.Windows.Forms.Label
        Friend WithEvents Button1 As System.Windows.Forms.Button
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
            Me.Label1 = New System.Windows.Forms.Label()
            Me.Button1 = New System.Windows.Forms.Button()
            Me.Label1.Location = New System.Drawing.Point(24, 8)
            Me.Label1.Name = "Label1"
            Me.Label1.TabIndex = 0
            Me.Label1.Text = "Label1"
            Me.Button1.Location = New System.Drawing.Point(56, 64)
            Me.Button1.Name = "Button1"
            Me.Button1.Size = New System.Drawing.Size(136, 24)
            Me.Button1.TabIndex = 1
            Me.Button1.Text = "Button1"
            Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
            Me.ClientSize = New System.Drawing.Size(248, 125)
            Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button1, Me.Label1})
            Me.Name = "Form1"
            Me.Text = "Form1"

        End Sub

    #End Region

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            OrgLabelHeight = Me.Label1.Height
            OrgLabelWidth = Me.Label1.Width
            OrgButtonHeight = Me.Button1.Height
            OrgButtonWidth = Me.Button1.Width
            OrgFormHeight = Me.Height
            OrgFormWidth = Me.Width
        End Sub
        Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize

            If Me.Height > OrgFormHeight And OrgFormHeight <> 0 Then
                'form's height increased
                Me.Label1.Height = Me.Label1.Height + (Me.Height - OrgFormHeight)
                Me.Button1.Height = Me.Button1.Height + (Me.Height - OrgFormHeight)
            ElseIf Me.Height < OrgFormHeight Then
                'form's height Decreased
                Me.Label1.Height = Me.Label1.Height - (OrgFormHeight - Me.Height)
                Me.Button1.Height = Me.Button1.Height - (OrgFormHeight - Me.Height)
                Me.Label1.Height = OrgLabelHeight
                Me.Button1.Height = OrgButtonHeight
            End If

            If Me.Width > OrgFormWidth And OrgFormWidth <> 0 Then
                'form's height increased
                Me.Label1.Width = Me.Label1.Width + (Me.Width - OrgFormWidth)
                Me.Button1.Width = Me.Button1.Width + (Me.Width - OrgFormWidth)
            ElseIf Me.Width < OrgFormWidth Then
                'form's height Decreased
                Me.Label1.Width = Me.Label1.Width - (OrgFormWidth - Me.Width)
                Me.Button1.Width = Me.Button1.Width - (OrgFormWidth - Me.Width)
                Me.Label1.Width = OrgLabelWidth
                Me.Button1.Width = OrgButtonWidth
            End If

        End Sub

    End Class
    LVL 14

    Expert Comment

    Anchor & Dock properties of the controls are the way to go. NO CODE!
    LVL 85

    Assisted Solution

    by:Mike Tomlinson
    LVL 3

    Author Comment

    Split between Desp & IdleMind as Desp has it right, but IdleMind gave some valuable help on how to use it effectively.

    Thanks Guys, was just what i was after.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now