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. s Text Property in a TreeNode

s Text Property in a TreeNode

Scheduled Pinned Locked Moved C#
data-structuresdebugging
7 Posts 3 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.
  • B Offline
    B Offline
    Bill Dean
    wrote on last edited by
    #1

    Greetings all, My goal: write a TreeNode class that contains another object and have the display text for the TreeNode be automatically set to some method on the object. Psuedo-ish code:public class DerivedNode : System.Windows.Forms.TreeNode { public MidTierClassLib.SomeClass oObject; public DerivedNode(MidTierClassLib.SomeClass SomeObject) { this.oObject = SomeObject; } public new string Text { get { return (this.oObject.ToString ()); } } }
    Trouble is: When I add a new DerivedNode to my TreeView, the text is blank. Stepping through in the debugger shows that the get clause is never called. This suggests I am barking up the wrong tree... Ideally, I want to be able to change properties on oObject and have the display updated automagically. I am pretty green with using TreeViews and TreeNodes (pun intended)...couldn't find anything that looked like a databinding...so any guidance would be appreciated. Thanks in advance, Bill

    J 1 Reply Last reply
    0
    • B Bill Dean

      Greetings all, My goal: write a TreeNode class that contains another object and have the display text for the TreeNode be automatically set to some method on the object. Psuedo-ish code:public class DerivedNode : System.Windows.Forms.TreeNode { public MidTierClassLib.SomeClass oObject; public DerivedNode(MidTierClassLib.SomeClass SomeObject) { this.oObject = SomeObject; } public new string Text { get { return (this.oObject.ToString ()); } } }
      Trouble is: When I add a new DerivedNode to my TreeView, the text is blank. Stepping through in the debugger shows that the get clause is never called. This suggests I am barking up the wrong tree... Ideally, I want to be able to change properties on oObject and have the display updated automagically. I am pretty green with using TreeViews and TreeNodes (pun intended)...couldn't find anything that looked like a databinding...so any guidance would be appreciated. Thanks in advance, Bill

      J Offline
      J Offline
      Judah Gabriel Himango
      wrote on last edited by
      #2

      public DerivedNode(MidTierClassLib.SomeClass SomeObject)
      {
      this.oObject = SomeObject;
      base.Text = SomeObject.ToString();
      }

      --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

      B 1 Reply Last reply
      0
      • J Judah Gabriel Himango

        public DerivedNode(MidTierClassLib.SomeClass SomeObject)
        {
        this.oObject = SomeObject;
        base.Text = SomeObject.ToString();
        }

        --------------------------- He who knows that enough is enough will always have enough. -Lao Tsu

        B Offline
        B Offline
        Bill Dean
        wrote on last edited by
        #3

        Thanks Judah, Clearly that'll do it on construction. But that won't help afterwards. Sometime later when I set a property on the oObject, I want the DerivedNode's Text property to be automatically updated as a result. I am thinking about adding an event to the oObject that it can raise whenever a change that needs to be displayed happens. That'll work but it breaks the division between mid-tier and GUI (the oObject should not explicitly tell the GUI when to do anything). Bill

        H 1 Reply Last reply
        0
        • B Bill Dean

          Thanks Judah, Clearly that'll do it on construction. But that won't help afterwards. Sometime later when I set a property on the oObject, I want the DerivedNode's Text property to be automatically updated as a result. I am thinking about adding an event to the oObject that it can raise whenever a change that needs to be displayed happens. That'll work but it breaks the division between mid-tier and GUI (the oObject should not explicitly tell the GUI when to do anything). Bill

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          Then in the set accessor for that object's property, also set the DerivedNode.Text property, which means you'll have to pass an instance of the DerivedNode to the other object:

          public class DerivedNode : TreeNode
          {
          private SomeObject someObject;
          public DerivedNode(string text)
          {
          someObject = new SomeObject(this);
          Text = someObject.SomeProp; // Really no reason to use "this" or "base".
          }
          }
          public class SomeObject
          {
          private DerivedNode node;
          private string someProp;
          public SomeObject(DerivedNode node)
          {
          this.node = node;
          }
          public string SomeProp
          {
          get { return someProp; }
          set
          {
          someProp = value;
          if (node != null) node.Text = someProp;
          }
          }
          }

          Microsoft MVP, Visual C# My Articles

          B 1 Reply Last reply
          0
          • H Heath Stewart

            Then in the set accessor for that object's property, also set the DerivedNode.Text property, which means you'll have to pass an instance of the DerivedNode to the other object:

            public class DerivedNode : TreeNode
            {
            private SomeObject someObject;
            public DerivedNode(string text)
            {
            someObject = new SomeObject(this);
            Text = someObject.SomeProp; // Really no reason to use "this" or "base".
            }
            }
            public class SomeObject
            {
            private DerivedNode node;
            private string someProp;
            public SomeObject(DerivedNode node)
            {
            this.node = node;
            }
            public string SomeProp
            {
            get { return someProp; }
            set
            {
            someProp = value;
            if (node != null) node.Text = someProp;
            }
            }
            }

            Microsoft MVP, Visual C# My Articles

            B Offline
            B Offline
            Bill Dean
            wrote on last edited by
            #5

            Thanks Heath, ...but that's not an appealing option either... (ain't I fun to help?!?). The objects need to be able to function without the gui. For example we may need to use then in Console applications for data maintainence or to slice and dice the data in other ways through WebApps, etc, so I think I favor the event-base approach since other application can just ignore the events. That said, do you know why the get accessor on the DerivedNode.Text property is not called when the node is displayed? That bit baffles me. Bill

            H 1 Reply Last reply
            0
            • B Bill Dean

              Thanks Heath, ...but that's not an appealing option either... (ain't I fun to help?!?). The objects need to be able to function without the gui. For example we may need to use then in Console applications for data maintainence or to slice and dice the data in other ways through WebApps, etc, so I think I favor the event-base approach since other application can just ignore the events. That said, do you know why the get accessor on the DerivedNode.Text property is not called when the node is displayed? That bit baffles me. Bill

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              Then using events would be the logical way to go. The reason the get accessor is not called is because the TreeView uses the TreeNode type. Its Text property is not virtual so the IL instruction call is used, as opposed to callvirt. Even if you hide it using the new operator, this will not work because the TreeView is using call on the actual TreeNode type. If you were to get the Text property referencing your type (DerivedNode), then your get accessor would be called.

              Microsoft MVP, Visual C# My Articles

              B 1 Reply Last reply
              0
              • H Heath Stewart

                Then using events would be the logical way to go. The reason the get accessor is not called is because the TreeView uses the TreeNode type. Its Text property is not virtual so the IL instruction call is used, as opposed to callvirt. Even if you hide it using the new operator, this will not work because the TreeView is using call on the actual TreeNode type. If you were to get the Text property referencing your type (DerivedNode), then your get accessor would be called.

                Microsoft MVP, Visual C# My Articles

                B Offline
                B Offline
                Bill Dean
                wrote on last edited by
                #7

                Thanks again Heath, As per the usual: you told me what I needed to know. I'll post a generic version of what I come up with here so maybe future TreeNode-noobs won't have to take up forum-bandwidth. Out, Bill

                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