By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,670 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,670 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Jul 14 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Mon, 14 Jul 2008 11:00:37 -0700, jmDesktop <ne***********@gmail.com>
wrote:
[...] 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.
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

Jul 14 '08 #2

P: n/a
On Jul 14, 10:00*pm, jmDesktop <needin4mat...@gmail.comwrote:
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).
Jul 15 '08 #3

P: n/a
On Jul 15, 2:30*am, Pavel Minaev <int...@gmail.comwrote:
On Jul 14, 10:00*pm, jmDesktop <needin4mat...@gmail.comwrote:


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);
}
}
Jul 15 '08 #4

P: n/a
On Jul 15, 5:49*pm, jmDesktop <needin4mat...@gmail.comwrote:
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?
Jul 15 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.