uncategorized

.NET 2.0 WinForms Controls

I’ve been working on this side project using .NET 2.0 for the last 6 months and some of the features are really starting to grow on me.  The project is a WinForms application which provides a nice contrast to my day-to-day web development.  Most of my previous forms development was done in VB6 which, although a decent tool, did leave a lot of things up to the developer.

In the last few months I’ve really started to like the new WinForm controls that are bundled with Visual Studio 2005.  If you use Panels, Splitters and Flow & Table layouts in combination with the Dock, Anchor, Margin and Padding properties on controls, it is possible to completely eliminate the need to write any resizing code.

The example that I’ve worked with is a dynamic database driven form that will load up a number of control pairings (a label and another data entry control).  In my previous, VB6, life I designed a form like this that had thousands of lines of code that managed the location of the controls, the spacing between them, their sizings and finally the location of the Save and Cancel buttons on the bottom of the form.  Oh, and don’t forget to write some code to resize the form itself so you don’t end up with a massive block of dead grey space, or, worse yet, controls being cut off by the edges of the form.

I was able, in a weekend instead of a month, create a dynamic, sizable form that has absolutely no resizing code.  Imagine you have 100 such forms in your application and each one has, on average, 250 lines of code to handle resizing.  That’s 25,000 lines of code that you need to maintain, and resizing code is notoriously difficult to maintain.  Say you can take 50% of those forms and implement the resizing without any code.  Not only have you increased the stability of the software, decreased it’s brittleness and saved some poor developer a tonne of unenviable work, but you’ve also reduced the amount of system maintenance that you will have to perform.

Not such a bad thing eh?  Check out the MSDN articles on the following topics for some more information:
How to: Anchor and Dock Child Controls in a TableLayoutPanel Control
How to: Anchor and Dock Child Controls in a FlowLayoutPanel Control.aspx)

If you want a good read on WinForms development check out Programming Microsoft Windows Forms 2005 Edition and read chapter 3 on Panels and Dynamic Layout.

I’m the Igloo Coder and my ship has Docked, Anchored and we’re calculating the Margins.