I’ve been trying to get my head around creating a custom event to handle mouse controls and such, but i keep running into certain errors that none of the tutorials seem to address. I’m pretty sure my logic of creation / assigning is correct, but maybe theres something fundamentally wrong that I’m missing here.
First off i create my delegate with the same signature as the method;
public delegate void MouseDown(object sender, MouseEventArgs e, Control control);
And then i assign it my event;
public event MouseDown OnMouseDown;
and then finally i try and subscribe to the event;
public static void Init(Control control, Control container, Direction direction)
{
control.MouseDown += OnMouseDown;
}
//ignore the parameters I'm passing, these are out of the scope of my problem.
However on this section I’m getting the error “an ohject reference is required for the non-static field, method, or propery “blah blah.OnMouseDown””
Finally heres my method I’m trying to subscribe to on mouseDown;
public void MouseDownEvent(object sender, MouseEventArgs e, Control control)
{
Dragging = true;
DragStart = new Point(e.X, e.Y);
control.Capture = true;
}
It probably doesn’t help that I’m trying to modify a helper class i found somewhere. If any further information is needed feel free to ask.
Note: The prime objective of this class is to allow controls to be moved at runtime.
Edit:
I believe the first two have worked, but to move i need to use the following method;
public void MouseMoveEvent(object sender, MouseEventArgs e, Control control, Control container, Direction direction)
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
}
}
so i need to send Direction direction, which i can’t send as sender. The reason I’m making these changes to the whole system is, i had it working before using anonymous delegates, but these proved tough to unsubscribe from when i wanted to re-lock a control in place.
Edit 2:
scratch that, the mouseDown and mouseUp won’t work if i don’t pass the correct control, at least subscribing the way i was doing it before. i could try your method but, the way i was doing it i was just calling one method which subscribed to all 3 MouseControls. it looks like either i can subscribe in sepearate methods as suggest, or i need to pass the right control correctly, i.e. not sender as control. any ideas?
Currently I’m subscribing by running this method from anywhere;
helper.Init(this.Controls["btn" + i]);
and then it runs through these methods before subscribing the button to my mouseup, down and move.
public static void Init(Control control)
{
Init(control, Direction.Any);
}
public static void Init(Control control, Direction direction)
{
Init(control, control, direction);
}
public static void Init(Control control, Control container, Direction direction)
{
control.MouseDown += new MouseEventHandler(FireOnMouseDown);
control.MouseUp += new MouseEventHandler(FireOnMouseUp);
control.MouseMove += delegate(object sender, MouseEventArgs e)
{
if (Dragging)
{
if (direction != Direction.Vertical)
container.Left = Math.Max(0, e.X + container.Left - DragStart.X);
if (direction != Direction.Horizontal)
container.Top = Math.Max(0, e.Y + container.Top - DragStart.Y);
}
};
}
Note: the third subscription is how they were before (anon delegates). I believe i need to pass the correct control in the events though. Does this give more clarity to my problem?
Answer to this question:
Initmethod is static which means that any non local variable used inside it must be static. In your code, thepublic event MouseDown OnMouseDown;must be static.Just do this and it will work fine (without the need for a delegate):
EDIT
Please see code below to see how to get the control that has been clicked.