I try to drag an item from an ItemList to my TreeList. For that I wrote listView1_MouseDown, listView1_MouseMove, treeView1_MouseMove and treeView1_MouseUp functions. When I move the mouse within treeView1 borders, MouseMove event is handled as it was supposed to. But MouseUp doesn't fire. Am I doing something wrong?
Talking about Winfroms.
As far as I remember, MouseUp event is sent to the same control as MouseDown was sent to. So, in your case, your listView1 will receive MouseUp.
You might want to look into some other events, like DragDrop.
Related
I have canvas with listbox inside it.
each child element of listbox sets eventhandler for Click event.
On canvas I set eventhandlers for
ManipulationStarted="canvas_ManipulationStarted"
ManipulationDelta="canvas_ManipulationDelta"
ManipulationCompleted="canvas_ManipulationCompleted"
My code for swiping works perfect accept one thing, it fires Click eventhandler before ManipulationCompleted eventhandler.
But for example listbox in the same time scrolls perfectly and do not fire Click event.
So basically what I need is to handle manipulation events in same way listbox do.
If this condition is true:
private void canvas_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
{
e.DeltaManipulation.Translation.X > [some value]
....
}
I need to disable firing Click event on any child element of canvas, doesn't matter if it is inside listbox or not.
Why are you setting click handlers if you don't want them to fire?
Click fires on pointer pressed, so there's no way to tell if the user wanted to Click or to start a manipulation. You'll need to either decide based on the location clicked or a later event if you want to differentiate between "clicks" and swipes.
Instead of Click you can handle the Tap gesture along with the manipulation events. Since Tap fires on the pointer released the manipulation system will fire it if the user tap and releases in one spot and it will trigger the manipulations if the user presses and moves the pointer.
See How to handle manipulation events for Windows Phone 8 for more details.
I have a Grid and inside the grid, I have Buttons. I need PointerEntered and PointerReleased events as I need to track which buttons are hovered. The problem is that, I need my Grid to handle the click events. Even though I don't have a click/tap/press handler for the button, my button captures the click and doesn't bubble it up to its parent (Grid). If I disable the button by settin IsEnabled to false, the click event is bubbled up to the Grid correctly, but then PointerEntered and PointerReleased events aren't fired, which I need to handle on the button. How can I handle enter/release events on the button and at the same time, pass the click event to its parent? I need my Grid to go onto "clicked" state as I also listen for the PointerReleased event on it. If click doesn't fire, released doesn't fire even if I do release the mouse button when on the Grid.
Thanks,
Can.
Try using AddHandler to attach a click event to grid and see if it works for you.
http://msdn.microsoft.com/en-us/library/windows/apps/windows.ui.xaml.uielement.addhandler.aspx
Remember to remove the event using RemoveHandler when done.
I'm making a calendar From in C# using WinForms.
I've put it together using a two-dimensional array of Panels, and inside them I have a List<> of custom controls which represent appointments.
The user needs to be able to drag appointment controls from one Panel to another (from day to day).
The custom control has a MouseDown and MouseUp event, which passes a message up from the control to the Parent.Parent (custom control -> day panel -> calendar form) and calls public methods StartDragging() and StopDragging() respectively.
Inside these methods, I make a clone of the custom control and add it to the Form, and store it in a global variable in the form which is called DraggedControl.
The Form has an event handler for MouseMove which goes like this:
void Calendar_MouseMove(object sender, MouseEventArgs e)
{
if (DraggedControl == null)
return;
DraggedControl.Location = PointToClient(MousePosition);
Refresh();
}
There are two problems, however:
First of all, the custom control is under everything else. I can see it being added and removed on MouseDown and MouseUp, but it is being added at 0,0 under the panels and day Labels.
Secondly, it does not appear to be moving with the MouseMove at all. I have a feeling this might be because I am moving the mouse with the button pressed, and this would represent a drag action rather than a basic MouseMove.
If I remove the MouseUp code, the control does drag with the mouse, however as soon as the mouse enters the panels (which the control is, sadly, underneath), the drag action stops.
What would you suggest I do?
I suspect there is probably a better way to do what I am trying to do.
custom control is under everything
else
bring it on top:
DraggedControl.BringToFront();
it does not appear to be moving with
the MouseMove at all
Control, which handled MouseDown event, captures mouse input and receives all following MouseMove events until it releases mouse input on MouseUp event, that's why Calendar_MouseMove() is not called. Handle MouseMove event for the same control, which generated MouseDown event.
After having started a Drag & Drop operation by DragDrop.DoDragDrop(...) no more MouseMove Events are fired. I even tried
AddHandler(Window.MouseMoveEvent, new MouseEventHandler(myControl_MouseMove), true);
where the last parameter means I even opt in for handled events. No chance, seems like the MouseMove Event is never fired at all! Any way to still get MouseMove Events while using Drag & Drop? I'd like to Drag & Drop a control, while dragging this control it shall follow the mouse pointer. Any idea how to do this in this case?
You need to handle the DragOver event.
EDIT: Try handling the GiveFeedback event on the control that you called DoDragDrop on; that might do what you're looking for.
What is the DragDrop.DoDragDrop construction? DoDragDrop is intended to be called in MouseDown/MouseMove method indeed, not in DragDrop. It is supposed to START handling the procedure, not to react for the drop (the desired effect of the drop you just implement directly in DragDrop). MouseMove never fires while already dragging, perhaps that's why it does not also fire with you, since you set the procedure. I think you handle this wrong way, here is one of examples http://msdn.microsoft.com/en-us/library/aa984430%28v=vs.71%29.aspx.
How to raise mouse button events from code behind when we have only mouse position?
I need to raise mouse events from code behind in different positions of screen.
Umm.. quite simply no you can't.
Edit
Now that you've describe what it is you actually need to do (a useful techinque in getting your problems solved). Here is the solution:-
Use the AddHandler method to attach a handler for the MouseLeftButtonDown and the MouseLeftButtonUp.
myPanel.AddHandler(UIElement.MouseLeftButtonDown, MyButtonDown_Handler, true);
Note the final true parameter is the enabler here, it indicates that your handler should be called even if another element has indicated that the event has been handled.
Whilst you cannot add a handler for MouseMove in this way that is not a problem since MouseMove cannot be marked as handled anyway, so you just attach a handler to the Panel for MouseMove in the normal manner.