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. Question with treeview

Question with treeview

Scheduled Pinned Locked Moved C#
questiondata-structuresxmltutorial
3 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
    markdbd
    wrote on last edited by
    #1

    First of all, hi to everybody, it´s my first post :) And sorry for my english I am from Spain. Well I am making a little app that write questions with their answers into an xml file. I am using a treeview to display the data. The question is the top node, and in it there are subnodes each for an answer. I know how to see which node was selected with this code: if(this.IsNode(tree.GetNodeAt(this.tree.PointToClient(Cursor.Position)))==true) Where IsNode is: private bool IsNode(TreeNode node) { try { string foo = node.Text; return true; } catch { return false; } } My question is: How can I see if an subnode is selected or an node? I want to show an menu for answers and another for question. Thanks a lot :)

    U 1 Reply Last reply
    0
    • M markdbd

      First of all, hi to everybody, it´s my first post :) And sorry for my english I am from Spain. Well I am making a little app that write questions with their answers into an xml file. I am using a treeview to display the data. The question is the top node, and in it there are subnodes each for an answer. I know how to see which node was selected with this code: if(this.IsNode(tree.GetNodeAt(this.tree.PointToClient(Cursor.Position)))==true) Where IsNode is: private bool IsNode(TreeNode node) { try { string foo = node.Text; return true; } catch { return false; } } My question is: How can I see if an subnode is selected or an node? I want to show an menu for answers and another for question. Thanks a lot :)

      U Offline
      U Offline
      User 260964
      wrote on last edited by
      #2

      You are doing it the hard way. ;) There is an easier way to get what you want, by using the TreeView.SelectedNode[^] property. This property returns the currently selected TreeNode, or null if no node is selected. Now you can test whether the returned TreeNode is a subnode. Remember that a root node doesn't have a Parent node, so we test that:

      public bool IsSubnodeSelected()
      {
      TreeNode selected = MyTreeView.SelectedNode;
      if (selected != null)
      {
      if (selected.Parent != null)
      return true;
      }
      return false;
      }

      The above method would return true if a subnode is selected, and false when a rootnode or no node is selected. - Daniël Pelsmaeker

      M 1 Reply Last reply
      0
      • U User 260964

        You are doing it the hard way. ;) There is an easier way to get what you want, by using the TreeView.SelectedNode[^] property. This property returns the currently selected TreeNode, or null if no node is selected. Now you can test whether the returned TreeNode is a subnode. Remember that a root node doesn't have a Parent node, so we test that:

        public bool IsSubnodeSelected()
        {
        TreeNode selected = MyTreeView.SelectedNode;
        if (selected != null)
        {
        if (selected.Parent != null)
        return true;
        }
        return false;
        }

        The above method would return true if a subnode is selected, and false when a rootnode or no node is selected. - Daniël Pelsmaeker

        M Offline
        M Offline
        markdbd
        wrote on last edited by
        #3

        Thanks a lot, it works but SelectedNode doesn´t work with right click so I use: private bool IsSubNode() { TreeNode selected =treeview.GetNodeAt(this.treeview.PointToClient(Cursor.Position)); if (selected != null) { if (selected.Parent != null) return true; } return false; } GetNoteAt with Current Mouse Position works with right click, now subnode works, and I can follow with my little app, thank you :)

        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