Click here to Skip to main content
12,295,294 members (58,533 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

41.5K views
3.3K downloads
6 bookmarked
Posted

Resizing Controls When Resizing Form

, 1 Jun 2013 CPOL
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)

Share

About the Author

BBGq
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

 
QuestionBest Commercial Solution Pin
arocca6524-Feb-16 20:22
memberarocca6524-Feb-16 20:22 
QuestionDoes not resize controls inside Tab Pin
Jack SunCodeProject7-Feb-16 2:05
memberJack 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
Member 1159939911-Apr-15 9:20
memberMember 1159939911-Apr-15 9:20 
BugIt's Working, Sort Of!?!?!?! Pin
James Wilkins2-Jul-14 7:03
memberJames Wilkins2-Jul-14 7:03 
QuestionIW & IH are never used Pin
Member 108445048-Jun-14 23:18
memberMember 108445048-Jun-14 23:18 
QuestionRichTextbox in a TabControl does not resize Pin
Kaimaster104523-Apr-14 21:51
memberKaimaster104523-Apr-14 21:51 
QuestionScale Pin
Duke.Nukem22-Apr-14 21:01
memberDuke.Nukem22-Apr-14 21:01 
QuestionDoes it work with FlowLayoutPanel? Pin
Member 1029015924-Dec-13 10:09
memberMember 1029015924-Dec-13 10:09 
Generalthanks Pin
moorypc20-Oct-13 18:40
membermoorypc20-Oct-13 18:40 
GeneralRe: thanks Pin
BBGq19-Nov-13 10:24
memberBBGq19-Nov-13 10:24 
GeneralRe: thanks Pin
abhishek123456723-Nov-13 21:36
memberabhishek123456723-Nov-13 21:36 
Suggestion?? Pin
paragpatel314-Jun-13 19:25
memberparagpatel314-Jun-13 19:25 
GeneralRe: ?? Pin
BBGq6-Jun-13 9:06
memberBBGq6-Jun-13 9:06 
GeneralRe: ?? Pin
paragpatel3114-Jun-13 21:58
memberparagpatel3114-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.

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