Toolstrips and Application Settings

I’ve not really spent any serious time writing a Windows Forms application in .net so I decided to see what’s changed.

The first thing that struck me, and subsequently consumed all of my time was the seemingly simple concept of a menu and tool-bar, or in .net terms MenuStrip and ToolStrip. Back in the day creating a menu was a basic affair, you said you wanted a menu and one would appear at the top of the window that you could change using editors of varying capabilities. This time I double clicked the menu control and the menu embedded itself within a container half way down the page. Now I’d also been playing with docking and anchoring so try as I might I couldn’t persuade the menu to appear at the top, it was always below another container. At this point I realized that I needed to get my head around the ToolStripbusiness, and so follows my tiny guide to using menus and toolbars.

1. The Tool Strip Container

Start off dropping a Tool Strip Container on the form, this allows the user to move any menus or toolbar to whatever edge of the screen they so desire.

2. The Status bar

Drop a status bar control inside the Tool Strip Container and dock it to the bottom of the container

3. Menu & Toolbar Strips

Drop a Menu Strip inside the Tool Strip Container followed by a Tool bar strip. You may want to use the smart icon to automatically insert the standard menus and buttons. I’d also recommend setting the “GripStyle” of the menu to visible.

4. Dock the Tool Strip Container

Set the docking mode of the container to “Full”.

You should now have the basis for a standard form where the user can drag the menu or tool strip to any of the edges of the window. The next trick is getting the application to remember where the user has left their toolstrips, this brings into to play the concept of Application Settings. In Visual Basic 6.0 saving user preferences was done via the SaveSetting and GetSetting API. This vanished with .net so I wrote a little wrapper for Isolated Storage to do pretty much the same thing. However, .net 2.0 introduces the ideas of Application Settings and User Settings. It’s a good concept, not only can you ask a form to save the settings you can also ask it to reload the last set or even reset to the default values, powerful stuff. So I can finally throw my Isolated Storage wrapper away, but I was still faced with the tedious job of storing the location of these ToolStrips. Fortunately before I dived headlong into grabbings lots of location details I discovered a static class called the ToolStripManager. This exposes LoadSettings\SaveSettings that does exactly what I wanted, remembers all the toolstrip location details on a per user basis. So just add;




To your Form constructor and FormClosing functions respectively and the user preferences will be adhered to. However, ToolStripManager doesn’t support the Form’s Application Settings concept of Reload or Default. Reloading isn’t too difficult, since the save takes place only when the Form is closed you can simply recall LoadSettings and it resets to the last known settings. “Default” is a bit trickier, you need to put the ToolStrips back to they way they were when the user first opened the application. As the application author I knew where they should go, but how do you tell that to the application? I guessed that the trick was to add the controls to the ToolStrips Container.Controls property, or in my case;

This worked fine, the TabStrips would all appear in the top container where they started life, but not quite. The order of the TabStrips was seemingly random, no amount of reordering or setting of indexes would provide a constant result. Reading a bit more I discovered that I should be using Join and not Add…it’s so obvious(?). Join allows you to specify the row in the container you wish the ToolStrip to appear, therefore allowing me to consitantly display the menu before toolbar.
toolStripContainer1.TopToolStripPanel.Join(toolStrip, row);


NB. Development Gotcha

During development of the the menu my SaveSettings kicked in and saved my new menu in the wrong position. Therefore whenever it loaded the settings back my menu would be incorrectly layed out. To fix this you have to navigate into your own document store and alter the settings XML by hand, usually located in something looking like:

<drive>:\Documents and Settings\<account>\Local Settings\Application Data\<application name>


This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Toolstrips and Application Settings

  1. rob says:

    Wow, I wasted an entire afternoon writing a class that would do this and here it is – all neat and tidy in one line of code.Thanks!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s