A little while ago I had an idea for a neat little component that could be used in applications that need to have their main user surface accessible even when the window is resized to something quite small. Essentially there are times that you have applications that you want to behave like OneNote when it is resized.

OneNoteFull

When I shrink the above window down a few notches OneNote detects it and adjusts its layout accordingly – we essentially end up with a slim-line version of OneNote that can easily tuck away into a corner to take notes on.

OneNoteSlim

So while I am at home coughing and spluttering today I thought I might implement a little prototype component that allows developers to achieve the same effect in their Windows Forms applications without having to write extra code. To test the component out I wrote a simple little driver application which has a number of panels docked to demonstrate the code.

ResizatronDriverFull

So in this example I set up, I set the four top and left most panels to disappear when the parent control’s (in this case the Form itself) size reaches either three hundred (300) pixels across or three hundred (300) pixels down. I thought about making an AND operation but OR worked so much better.

ResizatronPropertyGrid

When I run it up after setting the properties in the property grids (Resizatron is an extender provider component) and shrink the window down the four top and left most panels disappear – it worked!

ResizatronDriverSlim

The component itself is quite simple as far as extender providers go, but there are some interesting implementation details.

The first is that I wrote it in Visual Studio 2005 which has some nice little improvements for component developers like components automatically becoming available on the toolbox when they are successfully compiled (I really like that feature).

The second implementation detail is that I implemented the ISupportInitialize interface to ensure that the code was run after all the UI components had been created – this is important otherwise all the parent’s would have been null. And finally I used an anonymous method to hook up the resize event.

ResizatronCode

Why did I do that? Well, this is a really good example of where anonymous methods can reduce the amount of code that you have to write. In this case I am capturing an outer variable (extendee) to correlate parent resize events to child controls (the extendee).

This way I don’t need to walk the list of child controls in the parent and check if  they have any minimum parent size set. I guess its a trade off between having a lot of child controls in any given container and the cost of dispatching lots of short running events.

One drawback with my approach is that you can’t call SetMinimumParentSize at runtime and expect it to dynamically adjust behaviour, because the event wire-up is done in EndInit – but its a prototype!

You can download the code for this from my TechEd 2005 group up on Project Distributor.