Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 6, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

How to Automatically Resize Forms

The ElasticForm subroutine, part of CodeBank's standard procedure library, automatically repositions and sizes all controls on an SDI form when the user or code resizes the form. To use the routine, simply lay out the form as you normally would in design mode. Then call the subroutine once in the Form_Load event, with the Init parameter set to true, so that it can record the initial positions of the controls. Call the sub in the Form_Resize event, with Init set to False, to automatically resize and reposition the controls. The procedure accommodates any number of nested containers, any ScaleMode, and all types of controls. Note, however, that the procedure relies on the Tag property to store position information for each control, and so cannot be used if Tag is being used for another purpose (see ElasticFormArray). Also, MDI child forms are resized by VB before the Form_Load or Form_Initialize events, so the proportions of each form will be distorted. With MDI children, you must use ElasticFormArray (available in CodeBank's standard library) and specify the optional DesignWidth and DesignHeight parameters:

Public Sub ElasticForm(frm As Form, _
        Init As Integer)
On Error Resume Next
Dim ctl As Object

If Init = True Then
        For Each ctl In frm.Controls
                ctl.Tag = Format$(ctl.Left _
                        / frm.ScaleWidth, ".0000") _
                        & Format$(ctl.Top / frm.Scale_
                        Height, ".0000") & Format$_
                        (ctl.Width / frm.ScaleWidth, _
                        ".0000") & Format$(ctl._
                        Height / frm.ScaleHeight, _
        Next ctl
        For Each ctl In frm.Controls
                ctl.Move Val(Mid$(ctl.Tag, 1, _
                        5)) * frm.ScaleWidth, _
                        Val(Mid$(ctl.Tag, 6, 5)) * _
                        frm.ScaleHeight, Val(Mid$_
                        (ctl.Tag, 11, 5)) * frm._
                        ScaleWidth, Val(Mid$(ctl.Tag, 16, _
                        5)) * frm.ScaleHeight
        Next ctl
End If

End Sub

Ward Hitt
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date