.NET Framework - After merging 2 ContextMenuStrip how to call Opening ?

Asked By fred06250
29-Jun-10 09:46 AM
Hello All

I ve got 2 ContextMenuStrip

menu1 :
_A
_B

menu2 :
_C
_D

I set Opening on both menu
menu1.Opening += new CancelEventHandler(MyOpening1);
menu2.Opening += new CancelEventHandler(MyOpening2);

now I want to merge these 2 menus into one :

ContextMenuStrip newMenu= new ContextMenuStrip();
ToolStripManager.Merge(menu1, newMenu);
ToolStripManager.Merge(menu2, newMenu);

newMenu.Opening += new CancelEventHandler(myNewOpening);

In fact I m stuck here :
when the newMenu is about to be displayed, OnOpening is fired but how
can I call the Opening of the other two menus (menu1 & menu2) ??

thanks in advance

fred
CancelEventHandler
(1)
Exception
(1)
Class
(1)
OnOpening
(1)
Merge
(1)
Subscription
(1)
  Peter Duniho replied to fred06250
29-Jun-10 10:58 AM
If you do not have control over the subscription on the other menus, you
have no way to get the event handlers for their events.  If you do, then
of course you can just take advantage of the knowledge of what methods
were used and use them in your own code.

If you do have control over the creation, initialization, and use of the
original menus then one approach to the latter is to sub-class the
ContextMenuStrip class and provide subscribe/unsubscribe methods for the
other code to use for the Opening event, so that your sub-class has
access to the handlers as they are added and removed.  Of course, this
only works if you can ensure that the code subscribing or unsubscribing
to or from the event calls your methods rather than accessing the event
directly.

Then maintain a private delegate field that matches the subscription
state that you can query later (via a property or method).

(You could create a whole new event in the sub-class, and provide an
explicit add/remove accessor implementation, but IMHO because of the
inability to actually override the base event, this could actually be a
little more confusing, maintenance-wise).

Another alternative to the above would be to provide a kind of proxy
class that the "MyOpening1" and "MyOpening2" handlers are subscribed to
instead of the menus themselves, and in which you also provide features
for maintaining and retrieving the subscription state.  This is not much
different from writing a sub-class, but it encapsulates the subscription
management in a non-control class, which you may or may not find to be a
simpler way to do it.

In general, the state of event subscription is private to the class
implementing the event.  Unless the class provides an exception to this
(and most do not, including ContextMenuStrip), you cannot get the event
subscription state after the fact.  The best you can do is intercept it
somehow as you are add/removing handlers.

Pete
Create New Account
help
protected internal" will allow you access of a member from outside the namespace the member class is found in, but you get the picture. Sorry for any line breaks, but I copied and pasted from Word. Program shows: how even private members of a enclosing class can be accessed by the nested class enclosed by the enclosing class; how protected / protected internal works; how you can instantiate either the nested class, the enclosing class, or both; how instantiation by enclosing class. Frankly I think the access modifiers in C# are more confusing and can lead to System.Text; namespace nestc001 { / / / <summary> / / / Summary Using Nested classes with access modifier "protected internal" / / / < / summary> / / / class NestedOuterClass1 { private int magic_no; public int magic_noPublic; public int[] arr_outerCl; public NestedInterClass0 mySeemingViolationInnerClass; public NestedInnerClassTWO
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream())) { _rawResult = reader.ReadToEnd(); reader.Close(); } webResponse.Close(); } catch (Exception e) { _message = e.Message; } Jonathan Actually, it is simple. You need webResponse to be in new StreamReader(webResponse.GetResponseStream())) { / / code here } It need not be messy. No. Yes. If the class implements IDisposable, then you need the "using" statement. If it doesn't, you don't using statements instead. Hopefully not a big deal. Exactly. Look at the documentation. Every .NET class has on the first doc page for that class a complete description of all classes it inherits and interfaces it implements. If the class implements IDisposable, it will say so there. Alternatively, you could always just look for the Dispose() or IDisposable.Dispose() interface in the class methods. Generally speaking it should. However, I can't rule out the possibility of a class that requires explicit closing for best results. A Close() method may have other semantics than simply disposing; I don't think that a class is required to perform a Close() per se in its Dispose() method, so for example
the following set of classes that originally had all the classes except for the DrawingObject class set to override. I understand how these all work. But when I changed the Circle class to new virtual it called Circle.Draw, it called the original Draw from the Drawing does this mean that the Circles method will not be called unless I have another Class that inherits from the Circle class? * ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** *88 using System; public class DrawingObject { public virtual void Draw() { Console.WriteLine("I'm just a generic drawing object."); } } public class Line : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Line."); } } public class Circle : DrawingObject { new virtual public void Draw() { Console.WriteLine("I'm a Circle."); } } public class Square : DrawingObject { public override void Draw() { Console.WriteLine("I'm a Square."); } } public class DrawDemo
hold to give a = short comment on them: Using static classes to purely use another class's fields and methods to = even pretend being a member of that other class without actually being a = member of that class is very hard to understand. It compromises any = refacturing effort. Who invents such things. . .? OOD pobox.com> http: / / www.pobox.com / ~skeet Blog: http: / / www.msmvps.com / jon.skeet World class .NET training in the UK: http: / / iterativetraining.co.uk On Sat, 29 Mar 2008 15 hold to give a short comment on them: Using static classes to purely use another class's fields and methods to even pretend being a member of that other class without actually being a member of that class is very hard to understand. It compromises any refacturing effort. Who invents such things. . .? OOD with colleagues about using them. If I tell them to keep = member functions within a class instead of creating some function = anywhere, providing an object as a parameter, doing nothing else