maazza maazza - 4 months ago 35
C# Question

Disable Drag & Drop based on a condition

I am trying to allow/disallow drag&drop on a treeview based on flag in a Tag object.
But I can't find the proper event for this (something like BeforeDrag).

I am using C# and winforms, thanks.

Answer

Ralf was right all I had to do was to add a check In the ItemDrag callback.

private void tree_ItemDrag(object sender, ItemDragEventArgs e)
{
    var node = (e.Item as TreeNode).Tag as DataObject;
    if(!node.IsFrozen)
       DoDragDrop(e.Item, DragDropEffects.Move);
    else
        MessageBox.Show("Frozen nodes cannot be moved", "Drag & Drop error", 
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
}