ContextMenu splitters

  • Thread starter Thread starter David Johnston
  • Start date Start date
D

David Johnston

I notice that if I put a splitter (MenuItem.Text = "-") into a ContextMenu
using SP2, my application crashes on PPC2002. However, normal menus work
fine, and I've seen other applications (non-.NET) that have successfully
used splitters in ContextMenu's.

Is it intentional (ie, a design decision) that splitters in ContextMenu's do
not work?

- Dave
 
Add the splitter first to the context menu, then associate the context menu
to the control later. Associating the context menu to the control makes the
program crash.
 
Hi David,

I too noticed some peculiarities with putting menu seperators into
ContextMenus, but I have found out that it is possible to get them
working. (If you do a Google Groups search for my name, you should
find the post fairly easily.)

You say that the app "crashes" on the PPC2002. Do you mean an
exception gets thrown? I was seeing a "not supported" exception, and
someone kindly pointed out that the problem was to do with setting the
MenuItem's Text property *after* the MenuItem had been added to the
Controls collection--that just doesn't work.

So, you need to set the MenuItem's Text to "-" *before* it gets added
to its parent's Controls collection: i.e.

//
// mnuSeperator1
//
this.mnuSeperator1.Text = "-";
//
// Add menu items to context menu
//
this.mnuContext.MenuItems.Add(this.mnuSeperator1);

Now, you may also find problems arise because the Windows Forms
Designer doesn't seem to be smart enough to work these things out for
itself. If the InitializeComponent() method has these bits in the
wrong order, you'll get stymied, and the "right" way to fixing this is
to move the code out of the InitializeComponent() method. (You can't
really leave it in there, as the method might get re-written by the
Forms Desiger, and the problem would then come back... Wouldn't it be
good if MS fixed this for us?)

I hope that helps you.

Cheers,

Jon
 
Back
Top