Back to Devexpress

TreeList.CalcNodeDragImageIndex Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-cbed0139.md

latest7.1 KB
Original Source

TreeList.CalcNodeDragImageIndex Event

Enables you to specify a custom image to be displayed in front of nodes during node drag-and-drop.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

csharp
[DXCategory("DragDrop")]
public event CalcNodeDragImageIndexEventHandler CalcNodeDragImageIndex
vb
<DXCategory("DragDrop")>
Public Event CalcNodeDragImageIndex As CalcNodeDragImageIndexEventHandler

Event Data

The CalcNodeDragImageIndex event's data class is CalcNodeDragImageIndexEventArgs. The following properties provide information specific to this event:

PropertyDescription
DragArgsGets an object which provides information for drag-and-drop events.
ImageIndexGets or sets the index of the image to be displayed in front of nodes when dragging.
NodeGets the current Tree List node. Inherited from NodeEventArgs.
PtClientGets the current mouse pointer’s coordinates relative to the top-left corner of the control.

Remarks

The Tree List control enables end users to perform drag-and-drop operations on nodes if this feature is enabled with the TreeListOptionsDragAndDrop.DragNodesMode setting.

When a user drops a node, it is inserted before, after, or as a child of the hovered node. This behavior depends on the SHIFT key status and the mouse cursor position within the node region.

Handle the CalcNodeDragImageIndex event to display custom images in front of nodes during drag-and-drop operations. Assign the desired image index to the event’s CalcNodeDragImageIndexEventArgs.ImageIndex parameter. This index specifies the image’s position in the TreeList.Painter.NodeDragSvgImages image list. Add custom images to this list beforehand (for instance, on the form load) when required.

Note

The Tree List control does not guarantee that CalcNodeDragImageIndex and other Drag events are always raised in the same predefined order. Do not rely on the e.DragArgs.Effect parameter in the CalcNodeDragImageIndex event handler when you handle other Drag events to change the e.Effect parameter.

In such usage scenarios, check the same condition in Drag and CalcNodeDragImageIndex event handlers. Use Drag event handlers to set the required drag effect and the CalcNodeDragImageIndex event handler to specify the required drag image index based on your condition.

Example

The following sample code handles the TreeList.CalcNodeDragImageIndex event to display a custom image in front of parent nodes during drag-and-drop operations. The code adds the image to the painter’s image list to make the image available in the event handler.

csharp
using DevExpress.XtraTreeList;

// Add a custom image to the painter's image list
treeList1.Painter.NodeDragSvgImages.Add(SvgImage.FromFile("d:\\arrow.svg"));
// ...

private void treeList1_CalcNodeDragImageIndex(object sender, CalcNodeDragImageIndexEventArgs e) {
   // Display the custom image if the target node has children
   if (e.Node.HasChildren)
      e.ImageIndex = 3;
}
vb
Imports DevExpress.XtraTreeList

' Add a custom image to the painter's image list
TreeList1.Painter.NodeDragSvgImages.Add(SvgImage.FromFile("d:\\arrow.svg"))
' ...

Private Sub TreeList1_CalcNodeDragImageIndex(ByVal sender As Object, _
ByVal e As CalcNodeDragImageIndexEventArgs) Handles TreeList1.CalcNodeDragImageIndex
   ' Display the custom image if the target node has children
   If e.Node.HasChildren Then
      e.ImageIndex = 3
   End If
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CalcNodeDragImageIndex event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

winforms-treelist-create-file-manager-drag-drop-files-folders/CS/FileList/FileListHelper.cs#L31

csharp
Tree.AfterCollapse += new DevExpress.XtraTreeList.NodeEventHandler(this.treeList1_AfterCollapse);
Tree.CalcNodeDragImageIndex += new DevExpress.XtraTreeList.CalcNodeDragImageIndexEventHandler(this.treeList1_CalcNodeDragImageIndex);
Tree.DragDrop += new System.Windows.Forms.DragEventHandler(this.treeList1_DragDrop);

winforms-treelist-create-file-manager-drag-drop-files-folders/VB/FileList/FileListHelper.vb#L33

vb
AddHandler Me.Tree.AfterCollapse, New NodeEventHandler(AddressOf treeList1_AfterCollapse)
AddHandler Me.Tree.CalcNodeDragImageIndex, New CalcNodeDragImageIndexEventHandler(AddressOf treeList1_CalcNodeDragImageIndex)
AddHandler Me.Tree.DragDrop, New DragEventHandler(AddressOf treeList1_DragDrop)

See Also

DragNodesMode

DragCancelNode

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace