Virtual Developer Workshop: Containerized Development with Docker

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


Click here for a larger image.


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()
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.


Download source - 23Kb


  • Excellent

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

    Thank you Richard

  • Thank you!

    Posted by mikey2nicey on 05/16/2007 06:16pm

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

  • Creating Flexi-Forms in Visual Basic

    Posted by dilionyi on 10/20/2005 02:06pm

    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

  • Just what I was looking for...

    Posted by Legacy on 02/11/2004 08: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!

  • Fabulous

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

    Originally posted by: Lee

  • Pain relief!!

    Posted by Legacy on 06/27/2003 07: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!!

  • Anchors

    Posted by Legacy on 01/29/2003 08: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.

  • Wicked!

    Posted by Legacy on 01/27/2003 08: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!


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

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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