Wednesday, March 10, 2010

Splitters and anchors

I am not sure if this issue comes back to the fact that I am using a splitter, because I can tell you that in the first instance, using a splitter in winforms is so counter-intuitive and convoluted that I had to look up an article online, and it gave me some code to use on the basis that they also felt that trying to add a splitter using the design view is an exercise in frustration and futility. The concept is simple, why is it so hard ?

So, I have my splitter. On one side is a listbox, on the other is a panel. On that panel I have two pictureboxes, which are supposed to be evenly spaced across the panel. No matter what I try with the anchor properties, I cannot get them to resize themselves so they keep their layout. If I make the first one anchor to the right and the left, it will fill the entire panel left to right, with the second one appearing on top of it. The second one does not resize at all, when I apply the same properties.

At least, I think that is true. If you click the property sheet for the Anchor property, a little box pops up. If you click what you want on that box and then hit build, when the app closes, you'll see an 'invalid property' message box and your changes will be lost, you need to explicity close that popup first. I believe I've done that and tested every combination I can think of, twice. I can't believe I'm writing a winforms app and resizing controls by hand.


It seems that if it's in a splitter, the panel never fires a size changed event. I can catch the event on the main form, and then manually resize my controls, but I can't get the framework to position them for me. For anyone reading this, ignore the SizeChanged and ClientSizedChanged events ( and also the SplitterMoving event if you draw any sort of image or video inside your splitter ), the SplitterMoved event is the way to resize your form after the splitter has been moved.


  1. Haven't tested this but it should work. (It's what I almost always use when I want to divide space on a from evenly)

    Put a tablelayoutpanel inside your panel with the pictureboxes. Dock it to fill.
    Alter the tablelayoutpanel to have only 2 columns (or rows depending on what you want).
    Alter the columns (or rows) property's to have a size type of percent 50%.
    Put your picture boxes inside the columns (or rows) (1 in each)
    They now should automaticly adjust to be even in size.

  2. As Tom said, use yet another container. Either a TableLayoutPanel (for fixed layout), or another Splitter (for user-selectable layout).

    I don't use them often, but when I do they work as I expect them to. Most of the time, I write my own Resize handler and set Bounds myself.

  3. OK - thanks. So, I need to use another control inside the splitter for the docking to work. Is there then any reason that that behaviour is not just the default inside a splitter ? When would I WANT the docking to not work ?