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

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

Post by jmDeskto » Wed, 16 Jul 2008 03:00:37


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.
 
 
 

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

Post by Peter Duni » Wed, 16 Jul 2008 05:38:40

On Mon, 14 Jul 2008 11:00:37 -0700, jmDesktop < XXXX@XXXXX.COM >



I'm having a hard time understanding what you're asking. So you may want
to try to be more explicit. However, the code you posted looks to me as
though it would only drag the specific item that generated the ItemDrag
event. Maybe instead you want to call DoDragDrop() with
"sourceNode.Parent" instead of "e.Item"?

Pete

 
 
 

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

Post by Pavel Mina » Wed, 16 Jul 2008 15:30:04


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).
 
 
 

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

Post by jmDeskto » Wed, 16 Jul 2008 22:49:04


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);
}
}
 
 
 

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

Post by Pavel Mina » Wed, 16 Jul 2008 23:29:16


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