Introduction

In the 2.0 version of the .NET Framework the support for menus and toolbars has been upgraded. Unfortunately the structure of each new class has changed dramatically and conversion is not trivial. Also, the user interface exhibits some unexpected behavior. This article gives some tips for making the conversion from the old to the new. It also introduces simple extensions to the ToolStrip and MenuStrip classes that allow customization of mouseover highlighting and implementation of "Click Through" to improve the user interface.

Background

Like me, you probably have some legacy code written for the 1.x versions of the .NET Framework that you would like to upgrade to the fancier menus and toolbars that are available in the latest version. The MainMenu and ContextMenu classes have been replaced with entirely new classes, MenuStrip and ContextMenuStrip. Likewise, the ToolBar class has been replaced with ToolStrip. The new classes have a more up-to-date appearance and can be extended in new ways. For example, you can easily put ComboBoxes in menus or progress bars in a ToolStrip or StatusStrip. Since the structures of the new classes are quite different, it is not entirely straightforward to upgrade the old controls. I searched the Internet in vain for a simple process to follow. In the end I decided to take notes on what I had to do and share it with the Code Project community.

Having gone through the trouble of making the conversion, you may discover that the new controls behave in a fashion you did not expect, which might not be to your taste. There are two issues. First, unlike most Windows applications, the buttons and menus do not respond to a single click when their parent form does not have focus. Instead, you must click on the control twice before it responds, once to activate the form and the second time to activate the control. This is a perfectly fine model for the user interface and is the way Office 2003 and Visual Studio 2005 work. However, it would be better if the user could choose this model or the more standard approach in which a single click activates the control. Also, the new controls have a "feature" (I'm trying to be kind) that is very confusing to me. Unlike Office 2003, ToolStrip or MenuStrip will still show a highlight when the mouse moves over them on an inactive form. To me, this means they are ready for action, but unfortunately they still require a second click.

Borrowing code from Rick Brewster, I have extended the ToolStrip and MenuStrip classes to implement a feature that allows a single click to activate the control. For those who prefer the Office 2003 user interface, I have also implemented a "Suppress Highlighting" feature to enable mouseover highlighting only when the parent form (or one of its children) is active. I have included the code and a sample application to demonstrate the behavior of these new classes.

Upgrading by search and replace

If you are writing a new application, there should be no problem creating new MenuStrips and ToolStrips using the Visual Studio designer. However, if you are upgrading code, it is a large task to recreate all of your old menus and toolbars in the designer. If you use search and replace, it is not obvious how to proceed and mistakes are easily made. My goal is to save you some time by providing a step-by-step guide to the major changes. It won't cover every possibility, so you may have to correct some things after an initial compilation.

First, I strongly encourage you to save a backup of your entire project before you start editing. It is always dangerous to manually change the code created by the designer. If you make a mistake or if my tips lead you astray, you could easily end up with code that the designer cannot interpret, locking you out of the ability to modify your form until the bugs have been found. Second, the new class structures are complex, so my suggestions may only get you started. My suggestion is to charge forward with the simple changes listed here, then if the compiler complains, resolve any remaining issues.

I will assume you are using Visual Studio to edit your code. Open the code view and close the design view for the form you are upgrading. Follow these tips in order, using the bold highlighted text as your search and replace strings. Select the options to "Match Case", "Search Hidden Text", and search the "Current Document". Do the replacements one item at a time, checking to be sure each one makes sense in the context.

Tips for upgrading to MenuStrip and ContextMenuStrip

Replace MainMenuwith MenuStrip

Replace ContextMenuwith ContextMenuStrip

If the MainMenuconstructor or ContextMenuconstructor has an argument, remove it

Replace Menu.GetMainMenu() with ToolStripItem.GetCurrentParent(), the return value is a ToolStrip, which you may need to explicitly typecast to MenuStrip

Replace MenuStrip.MenuItemsand ContextMenuStrip.MenuItemswith MenuStrip.Items orContextMenuStrip.Items, but be sure you are only doing this on MenuStrip or ContextMenuStrip items and not on MenuItems

If mnMdiList is the name of the MenuItem that you want to display the list of MDI child forms, set this. MainMenuStrip.MdiWindowListItem = this.mnMdiList

Find the section of the InitializeComponent() method with lines containing this.Controls.Add. If the name of your MenuStrip is mainMenu, add the following line to the method: this.Controls.Add(this.mainMenu);

Now compile your code. You may find errors. If so, consult the class reference to resolve any remaining issues

View your form in the designer. If all goes well, you will see your new menus. Readjust the layout to accommodate the different size of the new menu.

Set any shortcut keys that were not edited in step 11.

All separators will appear as menu items containing a single hyphen. Right-click on these in the designer, select "Convert to" and replace with a real separator

Replace the ToolStrip.ButtonClickevent handler with separate handlers for each ToolStripButtonClick event

Eliminate lines that set ToolStripButton.Style

Replace ToolStripButton.Pushedwith ToolStripButton.Checked

Replace ToolStrip.Buttons.AddRange() with ToolStrip.Items.AddRange()

Use the designer to replace all the images for the buttons. The ImageList property still works, but it has been deprecated and cannot be used within the designer

Cross your fingers, compile, and run

Extending the ToolStrip and MenuStrip classes

As I mentioned above, I wanted to change the behavior of these controls to create a user interface that was more intuitive. I found most of the solution in Rick Brewster's blog on MSDN. Rick introduces a ClickThrough property and overrides the WndProc() method to watch for the WM_MOUSEACTIVATE message and change the return result from "Activate and Eat" to "Activate" when ClickThrough is true. In an MSDN forum, I found JasonD's suggestion to suppress the unwanted highlighting when click through is not occurring. He suggests overriding WndProc() to intercept the WM_MOUSEMOVE message and throw it away unless the parent form or one of its children has the focus.

I combined both of these ideas to create ToolStripEx and MenuStripEx classes. The sample project contains the code and an example of their use. Both classes contain two new properties that can be manipulated in code or set in the Visual Studio designer. They are:

// If true, activate control on a single click,
// even if the control's form does not have focus
publicbool ClickThrough=false;
// If true, do not show mouseover highlighting
// unless the control's form has focus
publicbool SuppressHighlighting=false;

Please refer to the sample project for the details and run the sample application to observe the effects. When you run it, try all four variations and see how the menus and toolbars respond both when the main form has the focus and when the smaller form has the focus. The code inside the ToolStripEx and MenuStripEx classes is identical, it simply defines the two properties above and then implements this simple WndProc() method:

Once you build the sample application, you can easily include the extended classes in your own projects, drop them onto your forms like any of the built-in classes, and edit them in the designer.

Final Thoughts

Many thanks to Rick Brewster and JasonD for providing code and suggestions for how to obtain the desired user interface behavior.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

Yes, that happened to me, which is part of the reason I decided to document my steps for others to follow. That's also why I included this in the article:

First, I strongly encourage you to save a backup of your entire project before you start editing. It is always dangerous to manually change the code created by the designer. If you make a mistake or if my tips lead you astray, you could easily end up with code that the designer cannot interpret, locking you out of the ability to modify your form until the bugs have been found.

Do you have any tips. I am using C++ and we were already using MenuStrip and ToolStrip. When I added the MenuStripEx code it compiles fine. However when I change the MenuStrip to MenuStripEx I get a CodeDOM parser error Unknown type "MenuStripEx"

I've never tried this in C++ so I can only describe what happens with C#. If you add the ToolStripEx class to a C# project in Visual Studio, you will not see the new controls in the ToolBox until you build the code. If you have a form that uses the controls, it will not display in the designer, but will show error messages until you build the code. Under these circumstances, you may get warnings when you try to run and debug the application, but it should still work and the warnings should be gone the next time you run the application.

Alright thanks, the code itself runs fine. The C++ Form Editor however has some sort of problem with the change. It says that the MenuStripEx is an Unknown Type. I had 2 or 3 other people look at it and we could not see the problem.

When I open a file from my recent file list and call this I get
"Unable to cast object of type 'SystemMenuItem' to type 'ToolStripMenuItem'"

Any idea why I would get this?

It works in unconverted app if MdiMenuItem = MenuItem and
m_menuMain = MainMenu

Also when I add a Separator to the menu it changes my code from
this.m_menuMain.Items.AddRange(new System.Windows.Forms.ToolStripMenuItem[] {
to
this.m_menuMain.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {

Thank you! It helped to have someone blaze a trail ahead . Here are a few additional things to look for that I ran into.

1) The old separators were a ToolBarButton with their Style set to ToolBarButtonStyle.Separator.
2) The following properties don't exist that existed for ToolBar.
ToolStrip.Appearance
ToolStrip.TextAlign
ToolStrip.ButtonDropDown
3) Had to convert ToolBarButton with Style set to ToolBarButtonStyle.DropDownButton to ToolStripSplitButton.
4) If you want dockable toolbars, you have to convert the code to use a ToolStripContainer and put the ToolStrips in its ToolStripPanels.

What am I doing wrong? If I add the MainMenu control then it indeed functions like a menu bar, but when I add the MenuStrip instead I always end up with a control at the top of the form that scrolls and takes up space in the form. (It also doesn't redraw correctly if you scroll it horizontally).