How can I select only the parent node of a treeview treenode?

J

jmDesktop

I have searched everywhere and tried several things. I have a
treeview with and want to be able to only select a parent node. For
example:

root //don't want to drag this
-parent1 //yes, drag this an only this because it is a parent
--childOfParent1 //cannot drag this, only the parent
--childOfParent1 //can only drag parent
-parent2 //yes, parent, can select and drag
--childOfParent2 //no, cannot drag
....

I have tried various, but this was the last:

private void trvPapers_ItemDrag(object sender, ItemDragEventArgs
e)
{
TreeNode sourceNode = (TreeNode)e.Item;
if (sourceNode.Parent != null &&
sourceNode.Parent.IsSelected)
DoDragDrop(e.Item, DragDropEffects.Move);

}

fails. I don't get an error, just strange behavior and it doesn't do
what it's supposed to. Any help is appreciated. How can I only drag
parents along with their children? I don't want the user to drag the
children anywhere alone. I only want the parent and children to go
together. FWIW, my drag and drop works, I just can't limit it. Thank
you.
 
P

Pavel Minaev

I have searched everywhere and tried several things.  I have a
treeview with and want to be able to only select a parent node. For
example:

root //don't want to drag this
-parent1 //yes, drag this an only this because it is a parent
--childOfParent1 //cannot drag this, only the parent
--childOfParent1 //can only drag parent
-parent2 //yes, parent, can select and drag
--childOfParent2 //no, cannot drag
...

I have tried various, but this was the last:

     private void trvPapers_ItemDrag(object sender, ItemDragEventArgs
e)
        {
            TreeNode sourceNode = (TreeNode)e.Item;
            if (sourceNode.Parent != null &&
sourceNode.Parent.IsSelected)
                DoDragDrop(e.Item, DragDropEffects.Move);

        }

If you want to do what I think you do, then it won't work. Consider:
root node has Parent==null. All parent* nodes have Parent==root. All
child* nodes have Parent=parent*. So if you want to only drag parent*
nodes, you can simply check that node.Parent==root (and root would be
TreeView.Nodes[0] in your case).
 
J

jmDesktop

I have searched everywhere and tried several things.  I have a
treeview with and want to be able to only select a parent node. For
example:
root //don't want to drag this
-parent1 //yes, drag this an only this because it is a parent
--childOfParent1 //cannot drag this, only the parent
--childOfParent1 //can only drag parent
-parent2 //yes, parent, can select and drag
--childOfParent2 //no, cannot drag
...
I have tried various, but this was the last:
     private void trvPapers_ItemDrag(object sender, ItemDragEventArgs
e)
        {
            TreeNode sourceNode = (TreeNode)e.Item;
            if (sourceNode.Parent != null &&
sourceNode.Parent.IsSelected)
                DoDragDrop(e.Item, DragDropEffects.Move);
        }

If you want to do what I think you do, then it won't work. Consider:
root node has Parent==null. All parent* nodes have Parent==root. All
child* nodes have Parent=parent*. So if you want to only drag parent*
nodes, you can simply check that node.Parent==root (and root would be
TreeView.Nodes[0] in your case).- Hide quoted text -

- Show quoted text -

I ended up with what I think is a hack, but maybe not:

TreeNode sourceNode = (TreeNode)e.Item;
if (sourceNode.Parent != null) //check for root
{
if (sourceNode.Parent.Text == "myRootNodeText")
{
DoDragDrop(sourceNode, DragDropEffects.Move);
}
}
 
P

Pavel Minaev

I ended up with what I think is a hack, but maybe not:

TreeNode sourceNode = (TreeNode)e.Item;
            if (sourceNode.Parent != null) //check for root
            {
                if (sourceNode.Parent.Text == "myRootNodeText")
                {
                    DoDragDrop(sourceNode, DragDropEffects.Move);
                }
            }

It is a hack if you want to consider localization. Why not Nodes[0],
anyway?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top