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. Other Discussions
  3. IT & Infrastructure
  4. TreeView/Node Question

TreeView/Node Question

Scheduled Pinned Locked Moved IT & Infrastructure
helpquestiontutorial
6 Posts 4 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
    MCSD Gandalf
    wrote on last edited by
    #1

    Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)

    J M J 3 Replies Last reply
    0
    • M MCSD Gandalf

      Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)

      J Offline
      J Offline
      J4amieC
      wrote on last edited by
      #2

      Can be done (with some minor annoyances) pretty easily. I created a blank windows app and dropped a single treeview onto it (treeView1). I created a simple structure of nodes as follows -+ Root --+ Node 1 ---- Node 1_1 ---- Node 1_2 -- Node 2 Next, created 2 recursive methods to make a node and all children bold or regular. private void MakeBoldRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Bold); foreach(TreeNode child in node.Nodes) { MakeBoldRecursive(child); } } private void MakeRegularRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Regular); foreach(TreeNode child in node.Nodes) { MakeUnboldRecursive(child); } } Next, have a private variable to hold onto the selected node, so that we can revert it when it is no longer selected: private TreeNode selectedNode = null; Then subscribe to the BeforeSelect event of the treeview (note: this can also be in the AfterSelect event handler, but BefoerSelect was visually slightly quicker) private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) { if(this.selectedNode != null) MakeRegularRecursive(this.selectedNode); MakeBoldRecursive(e.Node); this.selectedNode = e.Node; } I think this gives the effect you describe, one minor annoyance is that when turning the node from regular to bold it does not re-layout the control so sometimes the last few pixels of the bolded text get chopped off. You could workaround this by appending a few extra spaces to the end of the text for the node. Hope that helps, and that you can read the C# code. This was really a coding question and could have been put in your prefered language board.

      M 1 Reply Last reply
      0
      • J J4amieC

        Can be done (with some minor annoyances) pretty easily. I created a blank windows app and dropped a single treeview onto it (treeView1). I created a simple structure of nodes as follows -+ Root --+ Node 1 ---- Node 1_1 ---- Node 1_2 -- Node 2 Next, created 2 recursive methods to make a node and all children bold or regular. private void MakeBoldRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Bold); foreach(TreeNode child in node.Nodes) { MakeBoldRecursive(child); } } private void MakeRegularRecursive(TreeNode node) { node.NodeFont = new Font(this.treeView1.Font,FontStyle.Regular); foreach(TreeNode child in node.Nodes) { MakeUnboldRecursive(child); } } Next, have a private variable to hold onto the selected node, so that we can revert it when it is no longer selected: private TreeNode selectedNode = null; Then subscribe to the BeforeSelect event of the treeview (note: this can also be in the AfterSelect event handler, but BefoerSelect was visually slightly quicker) private void treeView1_BeforeSelect(object sender, System.Windows.Forms.TreeViewCancelEventArgs e) { if(this.selectedNode != null) MakeRegularRecursive(this.selectedNode); MakeBoldRecursive(e.Node); this.selectedNode = e.Node; } I think this gives the effect you describe, one minor annoyance is that when turning the node from regular to bold it does not re-layout the control so sometimes the last few pixels of the bolded text get chopped off. You could workaround this by appending a few extra spaces to the end of the text for the node. Hope that helps, and that you can read the C# code. This was really a coding question and could have been put in your prefered language board.

        M Offline
        M Offline
        MCSD Gandalf
        wrote on last edited by
        #3

        THANK YOU. That is REALLY close to what I want and will get me started. The only change I'll need to make is to NOT reset when a child node is selected, but can probably figure that out. I thought about putting this in the C# group, but figured if someone had a VB solution I could get it and convert. MANY Thanks.

        J 1 Reply Last reply
        0
        • M MCSD Gandalf

          Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)

          M Offline
          M Offline
          Michael Dunn
          wrote on last edited by
          #4

          MCSD-Gandalf wrote:

          but so far have not been able to find one that gives me access to make the text bold.

          Set the TVIS_BOLD state on the item you want to be bold.

          --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | NEW!! PimpFish | CP SearchBar v3.0 | C++ Forum FAQ

          1 Reply Last reply
          0
          • M MCSD Gandalf

            THANK YOU. That is REALLY close to what I want and will get me started. The only change I'll need to make is to NOT reset when a child node is selected, but can probably figure that out. I thought about putting this in the C# group, but figured if someone had a VB solution I could get it and convert. MANY Thanks.

            J Offline
            J Offline
            J4amieC
            wrote on last edited by
            #5

            Glad I could help, hope you spotted the deliberate mistake... I renamed MakeUnboldRecursive to MakeRegularRecursive but didnt compile afterwards...so missed one call from inside the recursive method. I thought you were language agnostic...so all worked out well.

            1 Reply Last reply
            0
            • M MCSD Gandalf

              Hopefully someone can help. I’ve been researching this for two days now, and I'm beginning to feel like an ID10T! Is it me, or does Microsoft INSIST on making things that should be easy really, really HARD. :-D Here's my problem: When an item appears in your inbox in Outlook, the treenode text becomes BOLD, and when you have looked at all the mail, it is reset. That is basically what I want to do. I need to be able to make a TreeNode bold when it is selected, and reset it when another TreeNode is selected. I have tried several of the events, but so far have not been able to find one that gives me access to make the text bold. I can change the font for the entire control, but have not been able to figure out how to change the code for just the selected node (and it's children) Any ideas? Thanks. WhiteWizard (aka Gandalf)

              J Offline
              J Offline
              justin_wang
              wrote on last edited by
              #6

              mark :) I Love C#/VS2005! http://justinw.cnblogs.com

              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