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
MainMenu
with MenuStrip
- Replace
ContextMenu
with ContextMenuStrip
- If the
MainMenu
constructor or ContextMenu
constructor 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.MenuItems
and ContextMenuStrip.MenuItems
with MenuStrip.Items
or ContextMenuStrip.Items
, but be sure you are only doing this on MenuStrip
or ContextMenuStrip
items and not on MenuItems
- Replace
MenuItem.MenuItems
with ToolStripMenuItem.DropDownItems
- Replace
this.Menu
with this.MainMenuStrip
- Replace
MenuItem
with ToolStripMenuItem
(and MenuItems
with ToolStripMenuItems
)
- Change all
ToolStripMenuItem.Index
properties to ToolStripMenuItem.MergeIndex
- Replace
ToolStripMenuItem.Popup
event with ToolStripDropDownItem.DropDownOpening
event and ContextMenuStrip.Popop
event with ContextMenuItem.Opened
event.
- Delete lines setting the
MenuItem.Shortcut
property and reset these later in the designer, or edit the line to set the ToolStripMenuItem.ShortcutKeys
property using the Form.Keys
enumeration
- Replace
ToolStripMenuItem.ShowShortcut
with ToolStripMenuItem.ShowShortcutKeys
- Delete lines setting the
ToolStripMenuItem.MergeOrder
property
- Delete lines setting the
ToolStripMenuItem.RadioCheck
property
- Delete lines setting the
ToolStripMenuItem.MdiList
property
- 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
- Compile your application and see if it runs!
Tips for upgrading to ToolStrip
- Replace
ToolBar
with ToolStrip
- Replace
ToolBarButton
with ToolStripButton
- Replace
ToolBarSeparator
with ToolStripSeparator
- Delete lines setting ToolStrip.
ButtonSize
, ToolStrip.
DropDownArrows
, or ToolStrip.ShowToolTips
, these properties don't exist for ToolStrip
- Replace the
ToolStrip.ButtonClick
event handler with separate handlers for each ToolStripButtonClick
event
- Eliminate lines that set
ToolStripButton.Style
- Replace
ToolStripButton.Pushed
with 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:
public bool ClickThrough=false;
public bool 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:
protected override void WndProc(ref Message m)
{
if(m.Msg == WinConst.WM_MOUSEMOVE && this.suppressHighlighting &&
!this.TopLevelControl.ContainsFocus)
return;
else
base.WndProc(ref m);
if(m.Msg == WinConst.WM_MOUSEACTIVATE && this.clickThrough &&
m.Result == (IntPtr)WinConst.MA_ACTIVATEANDEAT)
m.Result = (IntPtr)WinConst.MA_ACTIVATE;
}
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.