sender in MouseEvents

Hello ppl,
I've posted this question on quite a few boards over the last week, so apologees if you are reading it again, but it really has me stumped.
I have developed two controls, one that acts as a container for the other at runtime. The container I'll call Pad and the control that can be added to this Pad is called Task.
The user can one or more Tasks to the Pad. I take the mouse position when the user adss the Task, set the tasks properties (location, size etc), and add it to Pad's Controls collection. Before I add the task, I register to handlers for MouseUp and MouseDown for the added Task.
I basically want the user to be able to relate any two tasks by MouseDown on one Task, drag the pointer to another Task and MouseUp.
When the user MousesDown on any Task, sure enough the delegate for MouseDown in Pad fires as expected, and when the user MousesUp on another Task, again the delegate for MouseUp fires.
The problem is that when I quiz the sender argument it is always the instance that raised the MouseDown, regardless of where the user releases the mouse button.
I was thinking that the instances were not unique but there are a number of argument against that. Firstly, I have output the hashcodes as the Tasks are created and they are different. When I start a drag from either Task the hashcode i correct for the first Task but as I have said earlier the hashcode for the target is the same, hence reenforcing that the sender is reporting the object under the mouse pointer incorrectly.
Any help with this will be greatly appreciated by me and my sanity.
Many Thanks
Simon Rigby
[1655 byte] By [Diggers] at [2008-1-27]
# 1

This seems to be the correct behaviour of a standard control. What you can use as a workaround is to set the focus on the Pad at the end of the ButtonDown. After that, the ButtonUp will be done on the other control.

Here is a demo with two buttons on a panel:

private void buttons_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine(((Button)sender).Text + "mouse down.");
panel1.Focus();
}

private void buttons_MouseUp(object sender, MouseEventArgs e)
{
Console.WriteLine(((Button)sender).Text + "mouse up.");
}

Vossekop at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...
# 2
Diggers wrote:
Hello ppl,

I've posted this question on quite a few boards over the last week, so apologees if you are reading it again, but it really has me stumped.

I have developed two controls, one that acts as a container for the other at runtime. The container I'll call Pad and the control that can be added to this Pad is called Task.

The user can one or more Tasks to the Pad. I take the mouse position when the user adss the Task, set the tasks properties (location, size etc), and add it to Pad's Controls collection. Before I add the task, I register to handlers for MouseUp and MouseDown for the added Task.

I basically want the user to be able to relate any two tasks by MouseDown on one Task, drag the pointer to another Task and MouseUp.

When the user MousesDown on any Task, sure enough the delegate for MouseDown in Pad fires as expected, and when the user MousesUp on another Task, again the delegate for MouseUp fires.

The problem is that when I quiz the sender argument it is always the instance that raised the MouseDown, regardless of where the user releases the mouse button.

I was thinking that the instances were not unique but there are a number of argument against that. Firstly, I have output the hashcodes as the Tasks are created and they are different. When I start a drag from either Task the hashcode i correct for the first Task but as I have said earlier the hashcode for the target is the same, hence reenforcing that the sender is reporting the object under the mouse pointer incorrectly.

Any help with this will be greatly appreciated by me and my sanity.

Many Thanks
Simon Rigby

Simon,

I think that a better way to handle this would be to use the drag and drop event handlers when you begin to drag your code. It will allow you to specify information in the operation which would allow you to identify the instance that is being dropped (if not anything else, you can specify an identifier in the data and then use that identifier to look up the instance from another resource) and then work with that instance in the dropped instance.

The behavior you are seeing is by design, since the original instance has the mouse capture during the lifetime between mousedown and mouseup.

Hope this helps.

- Nicholas Paldino [.NET/C# MVP]
- mvp@spam.guard.caspershouse.com

casperOne at 2007-8-21 > top of Msdn Tech,.NET Development,.NET Base Class Library...

.NET Development

Site Classified