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

Resizing Controls When Resizing Form

, 1 Jun 2013
Rate this:
Please Sign up or sign in to vote.
Making the sizes of the controls change automatically when resizing the form.

Introduction

One of the problems that faces the programmers is the screen resolution and the sizes of the controls. Making the software changes screen resolution or obliges the user to do so are both annoying. Furthermore, the user may prefer sizes different of the ones chosen by the developer. Properties such as Anchor and Docking could be useful, but don't let the sizes of the controls preserve their initial ratios. 

The Code 

The following code allows the user to resize the form to any desired value while at the same time resizing the controls with the same ratio.  

Option Strict On

Public Class Form1

    Dim CW As Integer = Me.Width ' Current Width
    Dim CH As Integer = Me.Height ' Current Height
    Dim IW As Integer = Me.Width ' Initial Width
    Dim IH As Integer = Me.Height ' Initial Height

    Private Sub Form1_Resize(ByVal sender As Object, _
            ByVal e As System.EventArgs) Handles Me.Resize

        Dim RW As Double = (Me.Width - CW) / CW ' Ratio change of width
        Dim RH As Double = (Me.Height - CH) / CH ' Ratio change of height

        For Each Ctrl As Control In Controls
            Ctrl.Width += CInt(Ctrl.Width * RW)
            Ctrl.Height += CInt(Ctrl.Height * RH)
            Ctrl.Left += CInt(Ctrl.Left * RW)
            Ctrl.Top += CInt(Ctrl.Top * RH)
        Next

        CW = Me.Width
        CH = Me.Height

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles MyBase.Load

        IW = Me.Width
        IH = Me.Height

    End Sub

End Class

Now, try to add some controls on the form, and then run the program. Try to resize the form to any value (you can even maximize it), and see how the controls are resized accordingly. 

See the attached example.

Please  note that the attached example is not a well designed application; it is just a simple example that clarifies the technique.

License

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

About the Author

BBGq

United States United States
No Biography provided

Comments and Discussions

 
BugIt's Working, Sort Of!?!?!?! PinmemberJames Wilkins2-Jul-14 7:03 
QuestionIW & IH are never used PinmemberMember 108445048-Jun-14 23:18 
QuestionRichTextbox in a TabControl does not resize PinmemberKaimaster104523-Apr-14 21:51 
QuestionScale PinmemberDuke.Nukem22-Apr-14 21:01 
QuestionDoes it work with FlowLayoutPanel? PinmemberMember 1029015924-Dec-13 10:09 
Generalthanks Pinmembermoorypc20-Oct-13 18:40 
GeneralRe: thanks PinmemberBBGq19-Nov-13 10:24 
GeneralRe: thanks Pinmemberabhishek123456723-Nov-13 21:36 
Suggestion?? Pinmemberparagpatel314-Jun-13 19:25 
GeneralRe: ?? PinmemberBBGq6-Jun-13 9:06 
GeneralRe: ?? Pinmemberparagpatel3114-Jun-13 21:58 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 1 Jun 2013
Article Copyright 2013 by BBGq
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid