<

Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x

Dynamically Resize Form Controls

Published on
16,496 Points
5,496 Views
5 Endorsements
Last Modified:
Martin Liss
Over 40 years of programming experience. Expand my "Full Biography" to see links to some articles I've written.
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!
5
Comment
Author:Martin Liss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 

Expert Comment

by:GarySB
I created a empty class so it would be in the right place and pasted the instrucitons in.
Red is showing for:
     VERSION 1.0 CLASS

AND all the Attributes lines.
0
 

Expert Comment

by:GarySB
I'm having to many problems trying to get this to work.
My smaller pictures normally display 200 pix apart from each other.
In Win7 they are more like 500 and since my background picture is the same my picture buttons are all off.
Can  you think of another technique?
0
 
LVL 14

Expert Comment

by:Brook Braswell
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.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Brook Braswell
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
0
 
LVL 49

Author Comment

by:Martin Liss
Nice work Brook. I've updated the class.
0
 
LVL 49

Author Comment

by:Martin Liss
Added new functionality to the class. See Parameter 5.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Join & Write a Comment

Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month