Here’s a strange piece of behaviour – a puzzle (with a solution) for you.

Imagine that you’re working with Win.Forms, constructing a resizable layout using a TableLayoutForm as the host.

At Design time, your form looks like this:

Each label is configured as follows:

  • Anchors: Left (Left align, vertically centre)

  • Autosize: true

Each control is configured:

  • Anchors: Left Right (Stretch across cell, vertically centre)
  • Maximum Size: (240,0)

But, at run time it looks like this:

Notice how the controls are offset down from their proper location.

Why is this happening? Any ideas?

[Solution below]

Note that the top edge of each control is vertically centered within its cell – this is your clue.

Turns out that the TableLayoutPanel pays attention to the MaximumSize property of the controls – with the maximum size of these controls set to (240,0), the panel assumes that all the controls will be zero height and conveniently centered them vertically in their cells.

There are a number of possible fixes …

  • With the rows of the TableLayoutPanel set to AutoSize, we could set the Anchors of the controls to Left|Top|Right (Stretch across the top of the cell)

  • We could remove the MaximumSize property entirely by setting it to (0,0)

  • We could set the height of the MaximumSize property to the actual height of the control

I chose the later.

It’s an interesting example of how different features interact - if I had happened to set up these controls in any other way, the bug wouldn’t have surfaced.


blog comments powered by Disqus
Next Post
StyleCop for ReSharper  24 Feb 2009
Prior Post
Unit testing with LINQ  18 Feb 2009
Related Posts
Cryptic Error Codes  04 Apr 2015
Care about the User Experience  21 Jan 2013
Everything's broken but we can fix it  21 Sep 2012
First Impressions - Visual Studio 11  31 Mar 2012
Reinventing the Validation Wheel  16 Nov 2011
Custom Chrome  26 Oct 2011
Validation can be the enemy  27 Jan 2011
Who's Needs First?  14 Dec 2010
A User Interface Stack Exchange  22 Oct 2010
Typography  20 Sep 2010
More ux posts »
February 2009