Ivan Gromov
Ivan Gromov

Reputation: 4415

"Global" WinForms events

I have a few instances of a custom control that raise a custom event on MouseMove. Here's the code: EventArgs class:

public class GroupMoveEventArgs
{
    public enum Action { CalcOffset, Move };
    Action action;
    int mouse_x;
    int mouse_y;

    // setters missed here

    public GroupMoveEventArgs(GroupMoveEventArgs.Action action,
        int mouse_x, int mouse_y)
    {
        this.action = action;
        this.mouse_x = mouse_x;
        this.mouse_y = mouse_y;
    }
}

Control class:

public delegate void GroupMoveEventHandler(object sender, GroupMoveEventArgs e);
public event GroupMoveEventHandler GroupMoveEvent;

protected virtual void figureMouseMove(object sender, MouseEventArgs e)
{
    if (mouseDown)
    {
        if (inGroup)
        {   // raising the event
            if (this.GroupMoveEvent != null)
                GroupMoveEvent(this, new GroupMoveEventArgs(
                               GroupMoveEventArgs.Action.Move,
                               Parent.PointToClient(Control.MousePosition).X,
                               Parent.PointToClient(Control.MousePosition).Y));
        }
    }
}

protected virtual void OnGroupMoveEvent(object sender, GroupMoveEventArgs e)
{
    Console.WriteLine("Got mouse move event " + this.num_of_points);
    if (inGroup)
    {
        if (e.EventAction == GroupMoveEventArgs.Action.Move)
        {
            Location = new Point(e.MouseX - offset.X, e.MouseY - offset.Y);
        }
        else
        if (e.EventAction == GroupMoveEventArgs.Action.CalcOffset)
        {
            Control c = sender as Control;
            Point pnt = Parent.PointToClient(Control.MousePosition);
            Point tmp = c.Location;
            offset.X = pnt.X - tmp.X;
            offset.Y = pnt.Y - tmp.Y;
        }
    }
}

The problem is that when I raise an event, it is handled only by the sender, when I need all the controls to handle it. How can I do that?

Thanks in advance, Ivan.

Upvotes: 0

Views: 1933

Answers (1)

Micah Armantrout
Micah Armantrout

Reputation: 6971

Option 1

I would use a static event on the control

 public static event GroupMoveEventHandler GroupMoveEvent;

then your event handler would need to be static as well

 protected static virtual void 
                  OnGroupMoveEvent(object sender, GroupMoveEventArgs e)
 {

Note As stated in the comments make sure you reference your static event like this

 GroupMoveEventHandler -= OnGroupMoveEvent;

here is an example of Static Events if you want more info

Option 2

put your event handler in the form that contains the control and after making all your instance controls assign the single event handler that is in the form to the event on from the control

In the form

 Control c = new control();
 c.GroupMoveEventHandler += figureMouseMove(object sender, MouseEventArgs e);

public delegate void GroupMoveEventHandler(object sender, GroupMoveEventArgs e); public event GroupMoveEventHandler GroupMoveEvent;

 protected virtual void figureMouseMove(object sender, MouseEventArgs e)
 {
     if (mouseDown)
     {
         if (inGroup)
         {   // raising the event
              if (this.GroupMoveEvent != null)
               GroupMoveEvent(this, new GroupMoveEventArgs(
                               GroupMoveEventArgs.Action.Move,
                               Parent.PointToClient(Control.MousePosition).X,
                               Parent.PointToClient(Control.MousePosition).Y));
         }
      }
    }  

Upvotes: 2

Related Questions