Profile Picture

NTreeViewEx block item drop

Posted By Juan de Ituarte López 8 Years Ago
Author
Message
Juan de Ituarte López
Posted 8 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)

Group: Forum Members
Last Active: 8 Years Ago
Posts: 2, Visits: 44

In a NTreeViewEx control I want to be able to block dropping a node to other nodes.
I want to achieve the same result as the default implementation when it blocks me from dropping a node to itself or its parent, but to other nodes of my choice.
How can I achieve this?
Thanks



Nevron Support
Posted 8 Years Ago
View Quick Profile
Supreme Being

Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)Supreme Being (4,329 reputation)

Group: Forum Members
Last Active: Last Year
Posts: 3,039, Visits: 3,746
Hello Juan,

To do this you will need to subclass NTreeViewEx and override ApplyDragDrop method as follows:


public class MyNTreeViewEx : NTreeViewEx
{
    protected override void ApplyDragDrop(NLightUIItemDragDropEventArgs e)
    {
        NTreeNode over = e.DragOverItem as NTreeNode;
        // condition which determines that this is the node(s) that cannot
        // drop another node on it.
        // In this case you can set Tag property of the node to true,
        // or you can choose another approach.
        if ((bool)over.Tag == true)
        {
            return;
        }

        base.ApplyDragDrop(e);
    }
}


Also you can attach the instance of the new class to ItemDrag event and in the event handler you can change the cursor when the user drag over the node that should not accept dropped node:


myNTreeViewEx1.ItemDrag += myNTreeViewEx1_ItemDrag;
...
void myNTreeViewEx1_ItemDrag(object sender, NLightUIItemDragDropEventArgs e)
{
    NTreeViewEx treeView = sender as NTreeViewEx;

    // get the hovered node
    NTreeNode node = treeView.VisibleNodeFromY(e.MousePosition.Y);
    if (node == null)
    {
        return;
    }

    Cursor cur = e.Cursor;

    if ((bool)node.Tag == true && e.Cursor == null)
    {
        e.Cursor = Cursors.No;    
    }
    else
    {
        e.Cursor = cur;
    } 
}


I hope this helps.

Best Regards,
Nevron Support Team



Juan de Ituarte López
Posted 8 Years Ago
View Quick Profile
Forum Newbie

Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)Forum Newbie (0 reputation)

Group: Forum Members
Last Active: 8 Years Ago
Posts: 2, Visits: 44
Perfect answer, it works like a charm, thanks!!!!

To make the control generic I've subclassed NTreeViewEx and overrided ApplyDragDrop method as follow to add a new event ItemAllowDrag:

public class MyTreeViewEx : NTreeViewEx
{
    public delegate bool MyItemDragDropEventHandler(object sender, NLightUIItemDragDropEventArgs e);
    public event MyItemDragDropEventHandler ItemAllowDrag;

    protected override void ApplyDragDrop(NLightUIItemDragDropEventArgs e)
    {
        if (ItemAllowDrag != null)
        {
            if (!ItemAllowDrag(this, e))
                return;
        }
        base.ApplyDragDrop(e);
    }
}

And then I attached 2 events to my control:

private bool TreeList_ItemAllowDrag(object sender, NLightUIItemDragDropEventArgs e)
{
    if (e.Item.Name == "FOLDER")
        return false;
    if (e.DragOverItem.Name == "COMPONENT")
        return false;
    return true;
}

private void TreeList_ItemDrag(object sender, NLightUIItemDragDropEventArgs e)
{
    NLightUIItem DragOverItem = TreeList.ItemFromPoint(e.MousePosition);
    if (e.Item.Name != "COMPONENT")
        e.Cursor = Cursors.No;
    else if (DragOverItem != null && DragOverItem.Name == "COMPONENT")
        e.Cursor = Cursors.No;
    else
        e.Cursor = null;
}


Is it possible to also avoid the node hover effect?

Thanks,
Juan





Similar Topics


Reading This Topic