Auto Sizing Form Controls in .Net

Posted on Thursday 25 October 2007

UPDATE - Check out the comments for a better solution! - boy I sure am glad I finished the post the way i did!

In VB.Net (well any dot net language really) you can add a “SplitContainer” control – I like to call it a splitter control. It’s good, it’s great for getting your .Net controls to resize automatically. All you have to do is fill dock your splitter control to the form, then dock your other controls into the panes of the splitter control – simple!

One thing that has caused me a slight problem today however it that when you add a tool bar to the form, the docked splitter control does not run “up to” the bottom of the toolbar, but still goes all the way to the top of the window. The problem with this is that when the form changes size (is made larger) the splitter control does not “slide up” to the bottom of the toolbar, so you get a gap.

OK like this:
tbcs1.PNG

Not so good:
tbcs2.PNG

You can set the location of where the actual split will be placed at run time, so you could get round this by setting it to very low value. However by doing this you have now placed any controls on the lower part of the splitter behind the tool bar!

I added one line of code to over come this issue – it works nicely, notice that the –2 is to take account of the width of the splitter handle, which I have set at design time to 1, this seems to make it “hidden” at run time

Sub Panel1Paint(ByVal sender As Object, ByVal e As PaintEventArgs)

Me.splitContainer1.SplitterDistance = Me.toolStrip1.Height -2

End Sub

This is a bit of a bodge really as it will not work for all situations, but if you want a quick way to automatically resize form controls and you want a toolbar this might be a workaround.

Any better ideas?


4 Comments for 'Auto Sizing Form Controls in .Net'

  1.  
    25 October 2007 | 7:51 pm
     

    Why don't you use panels and set the Dock or Anchor properties? A toolbar strip would have the dock = top property set and the panel with the rest of the controls would have the dock = fill property set.

  2.  
    25 October 2007 | 10:49 pm
     

    Hi,
    I tired docking the toolbar to the top, but the text box went under the toolbar, hence the use of a splitter - but i will take another look along your lines of suggestions! - Thanks
    Ross

  3.  
    25 October 2007 | 10:55 pm
     

    Oh dear!!!!
    I tried out what you suggested and it worked perfectly - can believe i missed it! - What a fool!
    Thanks for the suggestion Johan, a much more sensible (and easier) was to do it!
    Cheers
    Ross

  4.  
    Yawar Amin
    12 April 2008 | 1:52 am
     

    I spent a lot of time trying to figure this layout stuff out. Finally I downloaded Visual C# and just designed the form in it. Then I looked at the designer-generated code and just lifted the techniques (but not the masses of code) off there. Works like a charm.

Leave a comment

(required)

(required)


.

Use [VBA] Your Code [/VBA], when posting code, cheers Ross x /


RSS feed for comments on this post | TrackBack URI