Autosizing Controls

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

Thanks
Jaz
LVL 3
jazduckAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fahad MukhtarDistinguished EngineerCommented:
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.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
anvCommented:
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()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        '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
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    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.SuspendLayout()
        '
        'Label1
        '
        Me.Label1.Location = New System.Drawing.Point(24, 8)
        Me.Label1.Name = "Label1"
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "Label1"
        '
        'Button1
        '
        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"
        '
        'Form1
        '
        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"
        Me.ResumeLayout(False)

    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)
        Else
            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)
        Else
            Me.Label1.Width = OrgLabelWidth
            Me.Button1.Width = OrgButtonWidth
        End If

    End Sub

End Class
0
ptakjaCommented:
Anchor & Dock properties of the controls are the way to go. NO CODE!
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
0
jazduckAuthor Commented:
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.

Jaz
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.