Is there an post Drag and Drop event? - c#

I'm trying to implement drag and drop capability into a custom data grid control. In the end, I'd like to be able to drag rows between 2 of these custom data grids. I believe I have everything working as expected. However, I need to do some cleanup in the drag and drop source control. Specifically, I need to refresh the grids after items are moved. How do I do this? If it make any difference, these grids are not bound to a data source (for other reasons I won't explain).

You supposed to perform post-drop operation on the drag source in the MouseMove event handler after the call to DoDragDrop() returned and on the drop target in the DragDrop event handler.

Related

How to send mousedown message to a component or control?

Is there a way to send message (like mousedown) to a control?
My purpose is that when you click GridView, prevent mousedown behavior itself and bypass mousedown message to RepositoryItem.
From your question: "My purpose is that when you click GridView, prevent mousedown behavior itself and bypass mousedown message to RepositoryItem."
It sounds like you want to control the behavior of the Mouse Down event on the control. Right?
If that's the case, on the Winforms designer, look at the properties panel. On there is also a selection for the form events for a control. You can scroll down and find the Mouse Down event and add an event handler. Then do whatever filtering you need including not even processing the message at all.
You also mentioned that it's a DevExpress control. I recall that some of their controls were wired up a little differently so the DevExpress website might have some additional clues.

How DragDrop features work in WPF? Deep understanding of DragDrop and related events

I'm trying to completely understand DragDrop features of WPF.
Background:
I'm trying to use DragDrop in WPF for a bit diffrent purposes that it was designed.
By "diffrent purposes" I mean that it will not carry data to be copied, moved or pasted. I'm working on diagram drawing application and DragDrop will be used to move "diagram elements", resize them or connect their "attachment points" with lines. All DragDrop operations will happen inside of some Canvas, UserControls representing "diagram elements" or "connector lines".
So far, I understand that:
DragDrop operation starts when I initialize it by calling this method:
public static DragDropEffects DoDragDrop(DependencyObject dragSource, object data, DragDropEffects allowedEffects)
for example in some MouseMove event handler (conditionally - if the mouse button is pressed)
My understanding of this is DoDragDrop method stores passed dragSource and data somewhere.
DragDrop operation ends when:
I have registered Drop event handler on some UIElement and that event fires
Esc key has been pressed
By operation end I mean that there will be no more Drop event handlers called when I release mouse key over UIElement that has AllowDrop set to true.
DragOver event has no impact on whole operation (it is still "in progress", but I can check what is currently being dragged and check DataObject being carried.
To do DragDrop operation properly all I have to do is:
call DragDrop.DoDragDrop()
use DragOver event handler to update interface if necessary (like moving "diagram elements" or redraw/modify "connector lines" on my Canvas if user needs to see what is going on
use Drop event handler to finish operation
Questions
Is my understanding correct?
Is it good idea to use DragDrop built in WPF for such application, or maybe I should write something based on MouseUp and MouseDown from scratch?
I'm asking because my first tries end with big Drop handler methods where I have to write few conditions to check context of drop operation (what was dropped? control that represents "diagram element", end of "connector line" or maybe something else). Also I have to "pack" data for DoDragDrop method in MouseMove handler methods, and then cast them to proper type in Drop (or DragOver) event. I'm thinking about writing some custom separate events for dragging "connector lines" over a "worksheet", something like CreatingConnectorDrop, MovingConnectorAttachmentPointDrop etc.
Can anyone recommend some source of information useful for writing this type of drawing application? Like some github repository, book...
The drag source calls DoDragDrop, e.g., from the MouseMove handler, to initialize a drag operation with the involved data as its argument.
The drop target must handle the DragEnter, DragLeave or DragOver attached events (and their corresponding tunneling versions) to manipulate the drag effect (by setting the DragEventArgs.Effects property).
Handle the GiveFeedback event and tunneling versions to provide a drag feedback e.g. to highlight illegal drop targets or potential drop targets in general.
The drop target handles the the Drop event to accept the dropped data.
Since all drag & drop events are routed events, the Drop event can always be handled by every element in the current UI element (sub)tree, unless the routed event is explicitly marked as handled by the tree's root element (tunneling) or leaf element (bubbling).
I recommend to read the Microsoft Docs: Drag and Drop Overview to learn more.
Using the existing Drag & Drop framework infrastructure is absolutely recommended. It makes the enabling of drop support for a particular control very convenient (simply set the UIElement.AllowDrop to true).
Additionally, it uses dedicated bubbling and tunneling events. Opposed to your suggested mouse events, this avoids the drag & drop logic from being broken due to the internal mouse event handling of the participating controls.
Another benefit of using the provided infrastructure is that WPF controls like text boxes already implement a specific drag and drop behavior, that reflects the UX of the Windows OS.

How to copy events associated with control when copying controls?

When I need to re arrange positions of buttons of controls I use copy/cut and paste but I lose the asociation of the events and need to re assign manually.
Is there a way to do that without losing them?
For example I cut a button and paste it elsewhere.
It had associated the bt_ClickEventFunction, but when pasting it, it loses this.
You can drag them on the Form in order to change their location, you don't need to cur&paste them.
Copy&Paste creates another control with new name and Text while Cut&Paste deletes everything from designer and then regenerates the control as it were but without lines for adding events. I presume that this is because the code for events handlers is in file other that the file created by designer and when you cut the control, event handler doesn't gets deleted from the code since it can be the event handler for some other control also. So when pasting, designer actually only creates the new control as it would when you drag it from the Toolbox but sets the properties from the control hat has been cut.
EDIT Edit based on your comments.
You can do drag&drop even with TabPages. You need to drag it for the small rectangle with arrows like shown on the image:
and you can drag it to the other TabControl, in that case, TabPage will retain all "associated" event handlers.
To restore all the event procedures to their respective controls, go to the VBA code window of the form and then cut, copy and paste the whole module. You may press Ctrl+A, Ctrl+X and Ctrl+V to do so.

ListView DrawSubItem Event

I have a ListView control in my windows form. I am adding images to ListView subItems using DrawSubItem event. It seems normal when I populate my ListView. But problem is every time I click ListView, DrawSubItem event fires. Therefore, all images and texts are loading every click and I can't select any item on my ListView. How can I solve this problem?
I have designed Better ThumbnailBrowser component that solves background loading of images and thread synchronization for you, so that you don't have to set images in draw events.
You can also try Better ListView Express, which is free for any kind if usage.
Unlike regular .NET ListView, you can set images of arbitrary sizes in the items.

Drag, Drop and Delete

I have a question about drag and drop in WinForms. I know how to enable the user to drag and drop controls around inside the form, but what I'm now trying to do is enable them to drag a LinkLabel ontop of a"Recycle Bin" icon inside my Form and when it detects that something has been dropped onto the Recycle Bin icon, that control will be removed from the Form.
How would I detect if something's been dropped on another control? Would I still use Control.DragEnter & Control.DragDrop?
Thank you
yes, DragEnter and DragDrop is the right way to go, also you need to handle DragOver.
Typically, in these handler you specify what kind of drag-drop is allowed, and in DragDrop do your staff of deleting.
Here is the helpful link for you which can explain you about the DragDrop.
http://www.codeproject.com/KB/combobox/LarryDragAndDrop.aspx
You require to work on following Events:
1. MouseDown 2.DragEnter 3. DragDrop

Categories