Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TreeView does not show selected node!

TreeView does not show selected node!

Scheduled Pinned Locked Moved C#
databasedata-structuresxmlquestionlearning
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mark F
    wrote on last edited by
    #1

    First of all, I have HideSelection set to false. I implemented drag and drop in a TreeView so that the indexed cards in the tree can be moved manually (cards store book info like Library card files). An XML file is updated with the newly arranged tree. The TreeView is refreshed in the Drag_Drop event, but the SelectedNode does not show! Is there a workaround for this? Here's the code...,

    private void cardsTreeView_DragDrop( object sender, DragEventArgs e )
    {
        nodePosition.X = e.X;
        nodePosition.Y = e.Y;
        nodePosition = cardsTreeView.PointToClient(nodePosition);
    
        TreeNode dropNode = cardsTreeView.GetNodeAt(nodePosition);
    
        if (dropNode != null && dropNode.Parent == this.nodeToDelete.Parent)
        {
            TreeNode dragNode = this.nodeToDelete;
            
            if (dropNode == dragNode)
                return;
    
            dropNode.Parent.Nodes.Remove(this.nodeToDelete);
            dropNode.Parent.Nodes.Insert(dropNode.Index + 1, dragNode);
    
            Cards.MoveCardTo(GetIndexString(dropNode), GetIndexString(nodeToDelete));
            Cards.ReOrderCards();
    
            if (cardsTreeView.Nodes.Count > 0)
                cardsTreeView.Nodes.Clear();
    
            cardsTreeView.BeginUpdate();
    
            // load all cards from XML document
            Cards.GetAllCards(ref cardsTreeView);
    
            cardsTreeView.EndUpdate();
            cardsTreeView.ExpandAll();
           
            cardsTreeView.EndUpdate();
            
            // Fails to show the selected node 
            cardsTreeView.SelectedNode = cardsTreeView.Nodes[dropNode.Index];
            cardsTreeView.SelectedNode.EnsureVisible();
        }
    }
    
    private void cardsTreeView_DragEnter( object sender, DragEventArgs e )
    {
        if(e.Data.GetDataPresent(DataFormats.Text))
            e.Effect = DragDropEffects.Move;
        else
            e.Effect = DragDropEffects.None;
    }
    
    private void cardsTreeView_ItemDrag( object sender, ItemDragEventArgs e )
    {
        nodeToDelete = (TreeNode) e.Item;
        string strItem = e.Item.ToString();
        DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);
    }
    

    Thanks, Mark

    M 1 Reply Last reply
    0
    • M Mark F

      First of all, I have HideSelection set to false. I implemented drag and drop in a TreeView so that the indexed cards in the tree can be moved manually (cards store book info like Library card files). An XML file is updated with the newly arranged tree. The TreeView is refreshed in the Drag_Drop event, but the SelectedNode does not show! Is there a workaround for this? Here's the code...,

      private void cardsTreeView_DragDrop( object sender, DragEventArgs e )
      {
          nodePosition.X = e.X;
          nodePosition.Y = e.Y;
          nodePosition = cardsTreeView.PointToClient(nodePosition);
      
          TreeNode dropNode = cardsTreeView.GetNodeAt(nodePosition);
      
          if (dropNode != null && dropNode.Parent == this.nodeToDelete.Parent)
          {
              TreeNode dragNode = this.nodeToDelete;
              
              if (dropNode == dragNode)
                  return;
      
              dropNode.Parent.Nodes.Remove(this.nodeToDelete);
              dropNode.Parent.Nodes.Insert(dropNode.Index + 1, dragNode);
      
              Cards.MoveCardTo(GetIndexString(dropNode), GetIndexString(nodeToDelete));
              Cards.ReOrderCards();
      
              if (cardsTreeView.Nodes.Count > 0)
                  cardsTreeView.Nodes.Clear();
      
              cardsTreeView.BeginUpdate();
      
              // load all cards from XML document
              Cards.GetAllCards(ref cardsTreeView);
      
              cardsTreeView.EndUpdate();
              cardsTreeView.ExpandAll();
             
              cardsTreeView.EndUpdate();
              
              // Fails to show the selected node 
              cardsTreeView.SelectedNode = cardsTreeView.Nodes[dropNode.Index];
              cardsTreeView.SelectedNode.EnsureVisible();
          }
      }
      
      private void cardsTreeView_DragEnter( object sender, DragEventArgs e )
      {
          if(e.Data.GetDataPresent(DataFormats.Text))
              e.Effect = DragDropEffects.Move;
          else
              e.Effect = DragDropEffects.None;
      }
      
      private void cardsTreeView_ItemDrag( object sender, ItemDragEventArgs e )
      {
          nodeToDelete = (TreeNode) e.Item;
          string strItem = e.Item.ToString();
          DoDragDrop(strItem, DragDropEffects.Copy | DragDropEffects.Move);
      }
      

      Thanks, Mark

      M Offline
      M Offline
      Mark Churchill
      wrote on last edited by
      #2

      Give this a try. I've noticed that some properties don't seem to update until they are rendered (bad form IMO). // Fails to show the selected node cardsTreeView.SelectedNode = cardsTreeView.Nodes[dropNode.Index]; cardsTreeView.Nodes[dropNode.Index].EnsureVisible();

      Mark Churchill Director Dunn & Churchill

      M 1 Reply Last reply
      0
      • M Mark Churchill

        Give this a try. I've noticed that some properties don't seem to update until they are rendered (bad form IMO). // Fails to show the selected node cardsTreeView.SelectedNode = cardsTreeView.Nodes[dropNode.Index]; cardsTreeView.Nodes[dropNode.Index].EnsureVisible();

        Mark Churchill Director Dunn & Churchill

        M Offline
        M Offline
        Mark F
        wrote on last edited by
        #3

        Sorry, but I don't understand your reply. You posted the code that I already have in my project. Mark

        M 1 Reply Last reply
        0
        • M Mark F

          Sorry, but I don't understand your reply. You posted the code that I already have in my project. Mark

          M Offline
          M Offline
          Mark Churchill
          wrote on last edited by
          #4

          Ah not quite. Where you set the tree.SelectedNode to someNode, and then called EnsureVisible on tree.SelectedNode , I set the tree.SelectedNode and then called EnsureVisible on someNode. It might not seem like theres actually a difference, but I have noticed sometimes you can set properties on some of the web controls and they don't come back out correctly until they are rendered. Ie the following code fails:

          someTree.selectedNode = someNode;
          assert(someTree.selectedNode == someNode);

          Odd, I know, but theres no harm trying it.

          Mark Churchill Director Dunn & Churchill

          M 1 Reply Last reply
          0
          • M Mark Churchill

            Ah not quite. Where you set the tree.SelectedNode to someNode, and then called EnsureVisible on tree.SelectedNode , I set the tree.SelectedNode and then called EnsureVisible on someNode. It might not seem like theres actually a difference, but I have noticed sometimes you can set properties on some of the web controls and they don't come back out correctly until they are rendered. Ie the following code fails:

            someTree.selectedNode = someNode;
            assert(someTree.selectedNode == someNode);

            Odd, I know, but theres no harm trying it.

            Mark Churchill Director Dunn & Churchill

            M Offline
            M Offline
            Mark F
            wrote on last edited by
            #5

            Well I tried your suggestion and the TreeView still does not scroll to the selected node and show it as selected. Must be a bug in the TreeView control. Thanks for the assistance. Mark

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups