WPF TreeView Drag and Drop - Drop event always fires when clicking element - c#

I'm trying to put together a drag & drop solution in WPF TreeView control, using these techniques:
Dragging and dropping to a TreeView, finding the index where to insert the dropped item
When the user clicks on a TreeViewItem, first the treeViewItem_MouseLeftButtonDown gets executed, then the treeViewItem_Drop also. At every single click.

It sounds like you're calling DragDrop.DoDragDrop() from the treeViewItem_MouseLeftButton handler. The treeViewItem_Drop even is raised when the mouse button is released, so you're getting a drop event on every mouse click. Try calling DoDragDrop from a treeViewItem_MouseMove handler instead. Just make sure that the left mouse button is pressed before calling DroDragDrop. You may also want to make sure the mouse has moved a minimum distance before starting the DragDrop operation as well, such as
if(e.LeftButton == MouseButtonState.Pressed
&& horizontal_move > SystemParameters.MinimumHorizontalDragDistance)
{
DragDrop.DoDragDrop();
}

Related

Prevent Click events on child view while swiping(flicking) parent. WinPhone8.1

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.

Change happens after 2 mouse clicks when it should happen after a single click

I have a c# windows form application where I have a treeView inside a tabPage of a tabControl which is a part of the main form.
For the tree view, I click on the items of the treeView which I want to select then some change happens based on my selected Items.
I am using the AfterSelect event for item selection and and the mouseUp event for undoing the selection.
The item selection and deselection happens right away with a single click (no problem). The other change with should happen based on the selected items happens after two clicks! A single click either on the item node or outside the node's area do not trigger this change. I have to click again in order to see the change. That is wired. I am not using mouse double click events for this or something similar, I am only using the events I described above.
How can this be happening? and How to resolve it? Thanks.
EDIT: I am using my own multi-selection version of the treeView and I found (using debug) that when I get the selected nodes of the tree in the AfterSelect event after the first click is zero, then it is the number of selected nodes with the second click. How come this is happening when selected nodes are added and to the current selectedNodes list with every click in the overrided OnAfterSelect event of the treeView?
here is part of tree view code:
public List<TreeNode> SelectedNodes
{
get
{
return selectedNodes;
}
set
{
removeSelectionFromNodes();
selectedNodes = value;
selectNodes();
}
}
protected override void OnAfterSelect(TreeViewEventArgs e)
{
base.OnAfterSelect(e);
base.SelectedNode = null;
List<MSTreeNode> nodes = new List<MSTreeNode>();
.
.
.
removeSelectionFromNodes();
selectedNodes.Clear();
selectedNodes.AddRange(nodes);
selectNodes();
}
Maybe Treeview is losing focus in between clicks (?). You could try setting Treeview HideSelection property to False to keep the currently selected item highlighted when the control loses focus.
I tried to use the MouseDown event instead of the AfterSelect event. I override it in the my own multi-selection version of the treeView and used in the c# application I am developing but still it did not work. I am not sure how mouse events really work. If not used carefully, you may see wired behaviors.
Well, I ended up overriding the MouseUp and MouseUp events in my treeView subclass then I created an event which listens for changes in the selectedNodes list. If a change to the selectedNodes happnes in any of the mouse events this event is triggered. Then, I used the ChangedSelectedNodes event handler of the treeview instance in my application to do the other changes when there is a change in the node selection. This time it worked as expected.
I posted this in hope that it would be beneficial to anyone else who ran into the same problem like me.
P.S. Sometimes things do not work as you expect them to be and you just have fight and go through every other possibility until you find the solution.
Disable the hide selection option and use afterSelect option
in my project that works well

C# WinForms dragging controls with mouse

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.

How to tell when the user has clicked outside of the bounds your control?

I have a custom UserControl. I want to use it in a few different products, so I want something that can be implemented inside of the UserControl itself. I want to know when the user has clicked outside of the bounds of the UserControl so that I can hide it, similar to a ComboBox. How can I do that?
I tried handling the click event, but it only seems to fire if the click occured within the bounds of the control.
That's what the Capture property is designed to do. Set it to true and all mouse messages are routed to your control, even if it moves out of the window bounds. Check the e.Location property in the MouseDown event.
Hm, you may be able to accomplish what you want by listening to the GotFocus/LostFocus events. ComboBoxes give the drop downs focus when they open and close them when they lose focus.
do this
Select all controls on your form including form
In Property Window select MouseClick event
Now enter below Code in Common_MouseClick
Code:
if (!sender.Equals(yourControl))
{
yourControl.Visible=false;
}

Explicitly Prevent ContextMenuStrip from Loading in C#

I have a requirement to hide the contextmenustrip when a particular flag is not set. As i don't think we can explicitly control the show/hide of the context menu strip, i decided to trap the right mouse button click on the control with which the contextmenustrip is associated. It is a UserControl, so i tried handling it's MouseClick event inside which i check if the flag is set and if the button is a right button. However to my amazement, the event doesn't get fired upon Mouse Right Click, but fires only for Left Click.
Is there any thing wrong with me or is there any workaround?
RIGHT CLICK IS GETTING DETECTED, Question TITLE and Description Changed
After Doing some more research, i got the rightclick to fire, when i Handled the Mouse_Down Event on the Control. However Am still clueless, as how to explicitly prevent the ContextMenuStrip From Loading. Another question being, why MouseClick didn't detect Right Button Click?
Current WorkAround
Registering the event handler
userControl1.Control1.MouseDown += new MouseEventHandler(Control1_MouseDown);
void Control1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right && flag == false)
{
userControl1.Control1.ContextMenuStrip = null;
}
else
{
userControl1.Control1.ContextMenuStrip = contextMenuStrip1;
}
}
this is the current workaround i'm doing.
But how can i change it in the Opening Event of the ContextMenuStrip
Your solution will fail anyway when the context menu is invoked with the context menu key (or what it's called) on the keyboard. You can use the Opening event to cancel the opening of a context menu.
There is a work around.
Lets say Menu Item A sets the flag that controls the context menu on control B.
In the click event for A, you set b.ContextMenu = nothing to switch it off, and set b.ContextMenu back to the context menu control to switch it back on.
In WinForms there's also the Click event - which does get fired for a right mouse click.
If you are using WPF you should have MouseRightButtonDown and MouseRightButtonUp events.
Check out the table on this MSDN page which lists which controls raise which click events. Significantly, Button, CheckBox, RichTextBox and RadioButton (and a few others) don't raise and event on right click.

Categories

Resources