Dynamically Resize Form Controls

Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Almost 50 years of programming experience. Click '+ More' in my "Full Biography" to see links to some articles I've written.
Published:
Updated:
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most popular because when you choose a sizable border style the controls on the form don't resize when the user resizes the form and that's pretty ugly, and in my opinion makes sizable forms useless in most situations.

The CResizer class that is embedded here   CResizer.cls  solves that problem. Since CResizer is a class you can implement it in any of your applications in two simple steps:

1.    Add the CResizer class to your project
2.    Add  the following in any form where you want the form's controls to dynamically move and resize when the form is resized.

 
Option Explicit
                      Private clsResize As CResizer
                      
                      Private Sub Form_Resize()
                          clsResize.ResizeControls Me
                      End Sub
                      
                      Private Sub Form_Load()
                      
                          Set clsResize = New CResizer
                          clsResize.Initialize Me
                          
                       End Sub

Open in new window


Once you have done that, every control on the form will be both resized and moved when the form is resized. There may be occasions however where you don't want certain controls to be moved and/or you don't want them resized and/or you want the control's font to be adjusted, and so the class gives you control over how individual controls are handled by way of five parameters.
 
Parameter 1: The control's name

Parameter 2: Move? True/False, with True as the default

Parameter 3: Resize? True/False, with True as the default

Parameter 4: Adjust Font Size? True/False, with False as the default

Parameter 5: Truncate Command Button Caption? True/False, with False as the default
As you probably noticed, Parameter 5 is specific to Command Buttons. When you resize controls smaller you can use Parameter 4 to make the font smaller. That method is not totally effective however because in most cases you can't set a font size to be less that 8.25. After that most controls will automatically truncate the text. Command Buttons are different and they will wrap their Captions onto multiple lines. If you want them to truncate as other controls do then use Parameter 5. If there are other controls that behave like Command Buttons please let me know.

The use of those parameters is illustrated in the following code.

 
Private Sub Form_Load()
                      
                          Set clsResize = New CResizer
                          clsResize.Initialize Me
                          ' Move but don't resize or change the font size of the OK button
                          clsResize.SetActions "cmdOK", , False
                          ' Resize and change the fontsize, but don't move the Close button
                          clsResize.SetActions "cmdClose", False, True, True
                          ' Resize, move and change the font size of Text1
                          clsResize.SetActions "Text1", , , True
                          ' Truncate the text in Command1 
                          clsResize.SetActions "Command1", , , , True
                          
                       End Sub

Open in new window


Here are a couple of pictures showing the class in use. This one is the form immediately after it's loaded.
 After Load  

And this one shows the results of resizing the same form with the parameters above in place.
 After resizingIn the 'After' picture note that Text3 is resized and moved automatically as would all other controls, and the parameters I coded for cmdClose which said 'don't move it in relation to the new dimensions of the form' caused it to wind up in the middle of the form, and you probably wouldn't want to do that but you could if the situation called for it.

If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!
7
8,815 Views
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Almost 50 years of programming experience. Click '+ More' in my "Full Biography" to see links to some articles I've written.

Comments (7)

Brook BraswellApplication Development Manager
CERTIFIED EXPERT

Commented:
This works great for most cases but I have noticed that with some controls
such as a TreeView, the fonts do not size with the form.
Brook BraswellApplication Development Manager
CERTIFIED EXPERT

Commented:
However...
if you change the class from
   frm.Controls(lngCtl).FontSize = dblFontSize
to
   frm.Controls(lngCtl).Font.Size = dblFontSize

Then
  all controls will resize their fonts correctly
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2023

Author

Commented:
Nice work Brook. I've updated the class.
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2023

Author

Commented:
Added new functionality to the class. See Parameter 5.
Is it possible to do anything about the textboxes and labels going too far from each other horizontally when maximized?

Before:

After:


View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.