s Text Property in a TreeNode
-
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 -
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, Billpublic DerivedNode(MidTierClassLib.SomeClass SomeObject)
{
this.oObject = SomeObject;
base.Text = SomeObject.ToString();
}--------------------------- He who knows that enough is enough will always have enough. -Lao Tsu
-
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
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
-
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
Then in the
set
accessor for that object's property, also set theDerivedNode.Text
property, which means you'll have to pass an instance of theDerivedNode
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
-
Then in the
set
accessor for that object's property, also set theDerivedNode.Text
property, which means you'll have to pass an instance of theDerivedNode
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
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
-
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
Then using events would be the logical way to go. The reason the
get
accessor is not called is because theTreeView
uses theTreeNode
type. ItsText
property is not virtual so the IL instructioncall
is used, as opposed tocallvirt
. Even if you hide it using thenew
operator, this will not work because theTreeView
is usingcall
on the actualTreeNode
type. If you were to get theText
property referencing your type (DerivedNode
), then yourget
accessor would be called.Microsoft MVP, Visual C# My Articles
-
Then using events would be the logical way to go. The reason the
get
accessor is not called is because theTreeView
uses theTreeNode
type. ItsText
property is not virtual so the IL instructioncall
is used, as opposed tocallvirt
. Even if you hide it using thenew
operator, this will not work because theTreeView
is usingcall
on the actualTreeNode
type. If you were to get theText
property referencing your type (DerivedNode
), then yourget
accessor would be called.Microsoft MVP, Visual C# My Articles