[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Autosizing Controls

Posted on 2004-11-18
5
Medium Priority
?
282 Views
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...

Thanks
Jaz
0
Comment
Question by:jazduck
5 Comments
 
LVL 19

Accepted Solution

by:
Fahad Mukhtar earned 500 total points
ID: 12622922
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
 
LVL 10

Expert Comment

by:anv
ID: 12622987
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
 
LVL 14

Expert Comment

by:ptakja
ID: 12624483
Anchor & Dock properties of the controls are the way to go. NO CODE!
0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 500 total points
ID: 12625530
0
 
LVL 3

Author Comment

by:jazduck
ID: 12662045
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Loops Section Overview
Suggested Courses

829 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