Creating Flexible Forms in Visual Basic (Flexi-Forms)

Making controls dynamically reposition, resize, and align in accordance with the Form size.

.



Click here for a larger image.

Environment:VB6

Ever designed a Form with List or TreeView controls that looked okay, but turned out to be OTT if there wasn't much data to be displayed, or felt cramped if there was plenty to be displayed? Thought about making the Form resizeable? Then the pain really starts... when dynamically resizing and repositioning the controls.

Well, here's a class that'll take the pain out of this. It can be applied to ANY form, by adding just a couple of extra lines of code, and making use of the 'Tag' control property.

Add the clsLayout and clsControl class files to your project, and then add these few lines to the code for the Form.

Private myLayout As New clsLayout
...
Private Sub Form1_Load()
   myLayout.SetLayout Me
End Sub
...
Private Sub Form1_Resize()
   myLayout.RedrawLayout
End Sub

The last step is to specify how you want the controls on the Form to align—simply specify the layout in the 'Tag' property of the controls. You can use a mixture of fixed and variable layout properties:

Property Description
None No alignment
fixLeft Control alignment fixed towards left of Form
fixTop Control alignment fixed towards top of Form
fixRight Control alignment fixed towards right of Form
fixBottom Control alignment fixed towards bottom of Form
varX Control x position adjusted proportionally to form width
varY Control y position adjusted proportionally to form height
varWidth Control width adjusted proportionally to form width
varDepth Control depth adjusted proportionally to form depth

The following are combinations of layout styles:

Style Description
varHorizontal Left + Right (control width proportional proportional to Form width)
varVertical Top + Bottom (control height proportional proportional to Form height)
varFull Left + Right + Top + Bottom (control size dependent on Form size)

Here's a useful example (see screen shots):

Label1.Tag     ""
List1.Tag      "varWidth varVertical"
Label2.Tag     "varWidth fixRight"
List2.Tag      "varWidth fixRight varVertical"
cmdOk.Tag      "fixRight fixBottom"
cmdCancel.Tag  "fixRight fixBottom"

Additional Notes on Usage

The layout names are case sensitive (actually, the code scans for single uppercase letters). You may also embed controls within controls (for example, put a List control in a Frame). However, you should not use the Bottom or Right settings for a control within a frame that itself has the Bottom or Right setting. It doesn't make sense to use the Horizontal setting on two side-by-side controls, nor use the Vertical setting on two vertically stacked controls. The controls will overlap when the form is resized.

How It Works

The SetLayout function stores the minimum width and height of the form, and creates a collection of clsControl objects. A clsControl contains a reference to a control on the Form, together with layout information—indentation from top, bottom, left, and right sides of the Form, and the layout style (obtained from the 'Tag' property).

The RedrawLayout function is then invoked from the Form_Resize() event, which repositions and/or resizes the controls in this collection. It does this using the control's indentation information and layout style, in relation to its parent's current width and height.

Downloads

Download source - 23Kb


Comments

  • Excellent

    Posted by Ashok on 03/23/2013 12:09am

    Thank you Richard

    Reply
  • Thank you!

    Posted by mikey2nicey on 05/16/2007 11:16am

    Thank you Richard this code is excellent - just what I was looking for!

    Reply
  • Creating Flexi-Forms in Visual Basic

    Posted by dilionyi on 10/20/2005 07:06am

    This particular piece is quite good. Apart from the fact that whatever control I have I can adjust to full size and viceversa, I found the use of the tag property quite intersting. Its been useful especially the class. Even though I will try to see how the classes work. thanks

    Reply
  • Just what I was looking for...

    Posted by Legacy on 02/11/2004 12:00am

    Originally posted by: Michael

    This is great! I was looking for just this for a project I'm working for that uses multiple lists... Thanks!

    Reply
  • Fabulous

    Posted by Legacy on 11/19/2003 12:00am

    Originally posted by: Lee

    Reply
  • Pain relief!!

    Posted by Legacy on 06/27/2003 12:00am

    Originally posted by: Dawn

    Totally awesome, I've been trying to find a solution to resizing for quite a while. This is so clean and easy to use. I am in your debt kind sir!!

    Reply
  • Anchors

    Posted by Legacy on 01/29/2003 12:00am

    Originally posted by: Itay

    That's why Anchors are for.
    to easily and dynamically allow the user to resize the dialog, without breaking the view and with extreme simplicity (in the implemetation) for the developer.


    Reply
  • Wicked!

    Posted by Legacy on 01/27/2003 12:00am

    Originally posted by: Vitaly

    Why the distance between two list panes changes as well? It probably shouldn't.

    It would be interesting to see how the whole thing will be blinking with actual items in those two lists.

    Keep it real!

    Respect!

    http://www.tooltips.net - is what i do,...hm

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: December 18, 2014 The Internet of Things (IoT) incorporates physical devices into business processes using predictive analytics. While it relies heavily on existing Internet technologies, it differs by including physical devices, specialized protocols, physical analytics, and a unique partner network. To capture the real business value of IoT, the industry must move beyond customized projects to general patterns and platforms. Check out this webcast and join industry experts as …

  • Today's agile organizations pose operations teams with a tremendous challenge: to deploy new releases to production immediately after development and testing is completed. To ensure that applications are deployed successfully, an automatic and transparent process is required. We refer to this process as Zero Touch Deployment™. This white paper reviews two approaches to Zero Touch Deployment--a script-based solution and a release automation platform. The article discusses how each can solve the key …

Most Popular Programming Stories

More for Developers

RSS Feeds