Click here to Skip to main content
15,883,921 members
Articles / Resize
Tip/Trick

Resizing Controls When Resizing Form

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
1 Jun 2013CPOL 89.2K   5.3K   6   14
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.  

VB
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)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionBest Commercial Solution Pin
arocca6524-Feb-16 20:22
arocca6524-Feb-16 20:22 
QuestionDoes not resize controls inside Tab Pin
Jack SunCodeProject7-Feb-16 2:05
Jack SunCodeProject7-Feb-16 2:05 
SuggestionI saw a lot of solutions about this....but finally i made a code for resizing objects on a from ,what u need to understand it is the following ( Form , Four Buttons located at the bottom of the form side by side and a text box located at the top of t Pin
M.M.I Programmer11-Apr-15 9:20
M.M.I Programmer11-Apr-15 9:20 
BugIt's Working, Sort Of!?!?!?! Pin
James Wilkins2-Jul-14 7:03
James Wilkins2-Jul-14 7:03 
QuestionIW & IH are never used Pin
veeRob8-Jun-14 23:18
veeRob8-Jun-14 23:18 
QuestionRichTextbox in a TabControl does not resize Pin
Kaimaster104523-Apr-14 21:51
Kaimaster104523-Apr-14 21:51 
QuestionScale Pin
Duke.Nukem22-Apr-14 21:01
Duke.Nukem22-Apr-14 21:01 
QuestionDoes it work with FlowLayoutPanel? Pin
School Projects24-Dec-13 10:09
School Projects24-Dec-13 10:09 
Generalthanks Pin
moorypc20-Oct-13 18:40
moorypc20-Oct-13 18:40 
GeneralRe: thanks Pin
BBGq19-Nov-13 10:24
BBGq19-Nov-13 10:24 
GeneralRe: thanks Pin
abhishek123456723-Nov-13 21:36
abhishek123456723-Nov-13 21:36 
Suggestion?? Pin
paragpatel314-Jun-13 19:25
paragpatel314-Jun-13 19:25 
GeneralRe: ?? Pin
BBGq6-Jun-13 9:06
BBGq6-Jun-13 9:06 
GeneralRe: ?? Pin
paragpatel3114-Jun-13 21:58
paragpatel3114-Jun-13 21:58 

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.