Thanks for the input Pete but the issue has nothing to do with drawing
the menu. I think you are confusing this with context menu strip. I am
dealing with a context menu so it acts modal and does not return from
the show function until it is done. The menu gets drawn, I click on an
item, the menu goes away, but no message box appears.
There are a number of good reasons to stay away from DoEvents() and its
relatives generally. But it seems to me that the best reason in this
situation is simply that it doesn't really accomplish what you seem to
hope it would.
Specifically, calling DoEvents() may allow the menu to be drawn, but you
then subsequently dispose the menu right away, before the user gets a
chance to select one of the items in the menu. Even if .NET allows you to
get away with this now, there's certainly no reason it should be
guaranteed to in the long-term. You're just asking for your code to
break, even if for some reason it happens to work now.
Pete
Hi Mike,
I think, Pete is right... Avoid DoEvents wherever it's possible. Yes, it's
easy to use - but as easy it is to use, as easy it is to make things screw
up with DoEvents. It's like the "goto"-Statement. goto really isn't evil
from scratch - quite the opposite. But chances to make things ugly are
really high. Using DoEvents is a valid approach, but you (might) pay much
and get only little.
The bad about DoEvents is, that your UI is kept responsive, what is - in
most cases - also the goal of using it
http://blogs.msdn.com/jfoscoding/archive/2005/08/06/448560.aspx
You also have no guarantee that the disposed object will handle the event
correctly (in future releases of .net). If (on a brand new somewhat-core
processor) the GC is fast enough so you might get an invalid referenced
object in the "sender"-parameter of the event because the MenuItem objects
are disposed with the related ContextMenu object (maybe this is impossible -
I don't know, but I don't want my customers to find out *g*).
It's also a problem/matter of platform independence. Are you really sure,
your program is never ever needed to run on a mono/linux-platform? Have you
tested your solution on all platform constellations (there are many linuxes
out there *g*)? If ever needed, do you remember this "trick" and will you
test this behavior?
My suggestion is: write really clean code, and write it as it should be the
last time you'll do that (what I'm trying to say is: if possible, avoid any
hacks) *g*.
The reason, why ContextMenu can't be dragged to your form is that it's
recommend to replace it with ContextMenuStrip. If you want (I don't
recommandate that), you can get it work as expected. Right click on
Toolbox -> Choose Items... . Then go to tab ".NET Framework Components" (if
not selected) and search "ContextMenu (System.Windows.Forms)" in the list.
My second suggestion is: use ContextMenuStrip
It's a replacement (and
enhancement) for ContextMenu.
Why don't you want it? OK, it's not modal (one of the enhancements *g*). But
isn't it better to place the code after ShowContextMenu in MenuItemClicked?
Or listen to the event "menu.Collapse" and place the code there!?
At least - if you've tested it well and it works for you, choose DoEvents.
Yes, it's valid (it's not for nothing in the BCL). Nothing to be ashamed of
Sorry for my bad English (I have to write it every time *g*)
Greets, Wolfgang Kluge
http://gehirnwindung.de/
http://klugesoftware.de/