People often ask how can they make their form look the same in different sizes/resolutions, or change the resolution to suit their form.
Changing the resolution is a bad idea for several reasons:
Anything else on the users screen (the taskbar, or other programs) while your program is running will look wrong.
If your program doesn't set the resolution back properly (or crashes before it has the chance) then the user will be stuck at the 'wrong' resolution.
When the resolution is changed, any icons on the desktop are moved - but will not be moved back if you restore the previous resolution.
The resolution you want may not be available, or may look odd (which is often true for LCD monitors).
The person running the program may not have permission to change the resolution (if they are not an Admin user on the PC).
Ok, so how do I make my Form look the same at different sizes?
Every Form in a VB project has Resize event, which is called whenever the Height and/or Width of the Form are changed.
During the Form Resize event, you must also change the size (Height and Width) and position (Top and Left) of all the controls (command buttons, lines, text boxes etc) to make them look like they're in the right place.
NOTE: All the Codes posted here are to be placed under Form's Resize event
What is the difference between Height and ScaleHeight (or Width and ScaleWidth) properties?
1 - The ScaleHeight property is only applicable for a Form, and it is the height of the client portion of the form (not counting the height of the TitleBar, MenuBar or the Border of the form). So basically the ScaleHeight property should be used to position the controls within the form.
2 - All controls and Forms have a Height property and is for postioning/sizing the form/control relative to others. It is the Actual Size of Controls and the Form (including the Borders and the TitleBar) on the Screen.
The same applies to the Width and ScaleWidth properties.
Resizing Controls
The basic logic is to keep the Size_of_the_Control proportional to the ScaleSize_of_the_Form
Loop thru all of the controls within the Form, and set the ratio of the Previous_Size_of_Control to Previous_ScaleSize_of_Form.
Sometimes you will just want to relocate the control instead of resizing it, for example you cannot set the Height of a ComboBox at run-time, and making its width too large also makes the look of your form bad.
The basic logic is similar to Resizing form except that you need not to change .Width and .Height properties of the control.
Sometimes, it becomes necessary to fix the Form's Border so that user gets no liberty to fiddle with the beauty of the application. For this, just set the Form's MaxButton property to False.
But then you cannot change the WindowState to Maximized. So use this:
VB Code:
If Me.Height <> 7200 Or Me.Width <> 9600 Then
Me.Height = 7200
Me.Width = 9600
End If
Please see attached project for further Tips.
Resize Containers
Just for Information, that containers must be set or resized according to their properties set during Design Time. Resizing Containers (which I believe) must be avoided.
VB Code:
Private Sub Form_Load()
Frame1.Left = Me.ScaleLeft + 50
Frame1.Top = Me.ScaleTop + 50
Frame1.Height = Me.ScaleHeight / 2
Frame1.Width = Me.ScaleWidth / 2
End Sub
Private Sub Form_Resize()
Frame1.Left = Me.ScaleLeft + 50
Frame1.Top = Me.ScaleTop + 50
Frame1.Height = Me.ScaleHeight / 2
Frame1.Width = Me.ScaleWidth / 2
End Sub
Resizing Line Controls
Line Controls are not resized along with other controls because a Line control does not have .Left, .Top, .Height and .Width properties. Instead, it has .X1, .X2, .Y1 and .Y2 properties. So it is quite similar to the Resize fundamental used for controls, except:
The topic is quite understandable, isn't it!! Resizing a control does notresize the Font of it.
There are 2 samples attached for it, 1 downloaded and modified from PSCode.com and the other being made by me. Just use the Previous to Present ratio idea to resize the font.
I could not figure out what the problem is, though the UC I have attached is not made by me and is from another project.
Minimized Forms
When the form is minimized you do not need to re-position/re-size controls, as they cannot be seen anyway - so you are effectively wasting time. If you set values according to calculations based on the size of the form you are likely to get errors too.
In order to resolve this you should not process the resize event if the form is minimized, which you can do like this:
VB Code:
Private Sub Form_Resize()
If Me.WindowState = vbMinimized Then Exit Sub
'code to re-size/re-position controls
End Sub
Examples of the above methods (except Minimized Forms) can be found in separate projects in the attachment.
In case you come across any problems, please post a thread in the Classic Visual Basic Forum so that others members can participate in it (any replies to this thread will not be visible!).
Always use Option Explicit in your code. While working on this, I realised how important is using this.
Happy Programming!!
Last edited by si_the_geek; Feb 23rd, 2006 at 12:16 PM.
Reason: added "Minimized forms" section at H.G.'s request