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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jmDesktop

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

    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_ItemD rag(object sender, ItemDragEventAr gs
    e)
    {
    TreeNode sourceNode = (TreeNode)e.Ite m;
    if (sourceNode.Par ent != null &&
    sourceNode.Pare nt.IsSelected)
    DoDragDrop(e.It em, 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.
  • Peter Duniho

    #2
    Re: How can I select only the parent node of a treeview treenode?

    On Mon, 14 Jul 2008 11:00:37 -0700, jmDesktop <needin4mation@ 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.Par ent" instead of "e.Item"?

    Pete

    Comment

    • Pavel Minaev

      #3
      Re: How can I select only the parent node of a treeview treenode?

      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_ItemD rag(object sender, ItemDragEventAr gs
      e)
              {
                  TreeNode sourceNode = (TreeNode)e.Ite m;
                  if (sourceNode.Par ent != null &&
      sourceNode.Pare nt.IsSelected)
                      DoDragDrop(e.It em, 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==ro ot (and root would be
      TreeView.Nodes[0] in your case).

      Comment

      • jmDesktop

        #4
        Re: How can I select only the parent node of a treeview treenode?

        On Jul 15, 2:30 am, Pavel Minaev <int...@gmail.c omwrote:
        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_ItemD rag(object sender, ItemDragEventAr gs
        e)
                {
                    TreeNode sourceNode = (TreeNode)e.Ite m;
                    if (sourceNode.Par ent != null &&
        sourceNode.Pare nt.IsSelected)
                        DoDragDrop(e.It em, 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==ro ot (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.Ite m;
        if (sourceNode.Par ent != null) //check for root
        {
        if (sourceNode.Par ent.Text == "myRootNodeText ")
        {
        DoDragDrop(sour ceNode, DragDropEffects .Move);
        }
        }

        Comment

        • Pavel Minaev

          #5
          Re: How can I select only the parent node of a treeview treenode?

          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.Ite m;
                      if (sourceNode.Par ent != null) //check for root
                      {
                          if (sourceNode.Par ent.Text == "myRootNodeText ")
                          {
                              DoDragDrop(sour ceNode, DragDropEffects .Move);
                          }
                      }
          It is a hack if you want to consider localization. Why not Nodes[0],
          anyway?

          Comment

          Working...