• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 153
  • Last Modified:

firing event different ways for main application and extension

I have a mousemove event that fires on my application when I move over a particular control. Now I'm creating an extension for my application that needs to fire the same event code, but also perform other actions in the event if the extension is turned on. So what is the correct way to handle this? Create a whole new event that fires if the extension is enabled and duplicate the code of the original event into it, or do I somehow inherit from the original event the code that fires now, check to see if the extension is turned on, and then add the pieces I need for the extension to fire to the new event? or is there some other way that I don't know about? Hopefully this makes sense. thanks

thanks
guid
0
guidway
Asked:
guidway
  • 4
  • 2
2 Solutions
 
guidwayAuthor Commented:
p.s. I thought of overriding the event by creating a control in my extension that inherits from my control in the main application, however since I really don't need to change up anything else on this control (besides the mouse move event) I don't see a point of creating an entirely new control. There must be an easy way to override the event w/o creating a new control and inheriting from the application one.
0
 
Seraph_78Commented:
I think the correct approach would be to extend the MouseEventArgs class to add a new property called Extended.  

    public class ExtendedMouseEventArgs : MouseEventArgs
    {
        private bool extended;

        public ExtendedMouseEventArgs(MouseButtons button, int clicks, int x, int y, int delta, bool extended)
            : base(button, clicks, x, y, delta)
        {
            this.extended = extended;
        }

        public bool Extended
        {
           get { return this.extended; }
           set { this.extended = value; }
        }
    }


Then when you fire the event you can specify if it is extended or not.
Then in your handler you can check for the Extended property and perform what you want accordingly.
0
 
dstanley9Commented:
THe great thing about event sin .NET is that you can assign multiple handlers.

Can your extension just add another event handler to MouseMove?

control.MouseMove += new MouseEventHandler(this.custom_MouseMove);

THat way, the original MouseMove event will fire as well as the "extended" MouseMove event.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
guidwayAuthor Commented:
Seraph_78,
I am still new to how events work so it may take me some time to understand how to use your comments in my code.

dstanley9,
I attempted to do that and had trouble trying to keep track of how many event handlers were added. I use an extension manager that is built specifically for one of the libraries I'm using and it has a tendency to fire more than one time which in the end creates more event handlers than I want. I really need only two (the one that works now, and the new one that is to work with the extension). Is it possible to determine how many event handlers are associated with an event to make sure I only add the two I need?

In case it helps, the 3rd party library I use is called ArcObjects and is part of ESRI ArcGIS. I have posted the same question to their forums and did not receive a response yet.

thanks for your comments, I appreciate the help immensely.
0
 
Seraph_78Commented:
You should only be attaching the event handler(s) to the event once.  When the event is fired all the handlers in the events invocation list get called.  If you are getting multiple event handlers (more than 2) attached to the event then you are attaching the handlers in the incorrect place.

Following is an example of how you can add handlers to an event while making sure that that event handler has not already been added.


ComponentChangeHandler is the delegate type.
ComponentChange is the event

public void AddComponentChangeHandler(ComponentChangeHandler handler)
        {
            if (ComponentChange != null)
            {
                bool addHandler = true;
                // gets an array of delegates that are attached to the list
                Delegate[] d = ComponentChange.GetInvocationList();

                foreach (Delegate de in d)
                {
                    if (handler.Target == de.Target)
                    {
                        addHandler = false;
                        break;
                    }
                }
                if (addHandler)
                {
                    ComponentChange += handler;
                }
            }
            else
            {
                ComponentChange += handler;
            }
        }

0
 
guidwayAuthor Commented:
still haven't gotten around to trying this yet... been busy at work. I'll let you know once I get to it. sorry for the delay
0
 
guidwayAuthor Commented:
still haven't gotten to this yet, but before I forget and leave it open, thanks to everyone for your help.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now