Monday, April 24, 2006

FlowLayoutPanel Caveat emptor

My first WinForms app in .Net2 was a relatively simple data entry/maintenance program. I took the opportunity to take the new FlowLayoutPanel for a spin. It would automatically expand and shrink as its internal element's visibility changed depending on the state of the program. The panel at the bottom of the FlowLayoutPanel containing the Save and Cancel button would then automatically change its vertical position.

It worked well and saved about a dozen lines of awful UI jiggery-pokery.

Thinking that I was now an expert in all things to do with the FlowLayoutPanel I chose to use it again in my second WinForms 2.0 app. What a mistake!

Without going into details I had a user control with an fully docked group box internally containing a floating FlowLayoutPanel with a couple of different combinations of autosize, autogrow, etc thrown in what seemed to be a sensible combination. The FlowLayoutPanel also internally contained user controls loaded dynamically that were anchored to its edges.

Although it seemed right the IDE somehow got itself horribly confused. The various combinations of autosizing, docking and achoring made itself trip so that whenever I adjusted the size of the user control on the form it would magically automatically grow by about 100 pixels. Any attempt to pull it back into size would work but only for a moment.

What a mess.

Two hours of mucking around later I pulled out most of the flow layout smarts and ended up writing most of the UI jiggery-pokery myself - but it worked.

Watch out!