By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,767 Members | 1,605 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,767 IT Pros & Developers. It's quick & easy.

What is the best way to distinguish between TreeNodes

P: n/a
Hi People

Could anyone suggest to me the best way to distinguish treenodes? By
this I mean I'm in a situation where I have a TreeNode that displays a
number of categories and within those nodes there are items of stock.

There are a number of things I wish to do, such as use a ContextMenu to
Rename the node, but I can't seemingly tell if the Node I'm renaming is
a category or an item of stock.

At the moment I put the CategoryId or StockId unique value as the
TreeNode name.

Thank you for your time
Peter

Mar 29 '06 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Bob
Hi Pete,
One way is to derive a class from treenode for each type of thing that you
want to hang on the tree.
When working on a node, check its type and make your decisions accordingly.
regards
Bob

"DrPete" <p.*******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Hi People

Could anyone suggest to me the best way to distinguish treenodes? By
this I mean I'm in a situation where I have a TreeNode that displays a
number of categories and within those nodes there are items of stock.

There are a number of things I wish to do, such as use a ContextMenu to
Rename the node, but I can't seemingly tell if the Node I'm renaming is
a category or an item of stock.

At the moment I put the CategoryId or StockId unique value as the
TreeNode name.

Thank you for your time
Peter

Mar 29 '06 #2

P: n/a
To add to that: rather than checking its type give your derived TreeNodes a
common interface and use polymorphism.

"Bob" <bo*@nowhere.com> wrote in message
news:eF**************@TK2MSFTNGP14.phx.gbl...
Hi Pete,
One way is to derive a class from treenode for each type of thing that you
want to hang on the tree.
When working on a node, check its type and make your decisions
accordingly.
regards
Bob

"DrPete" <p.*******@gmail.com> wrote in message
news:11*********************@u72g2000cwu.googlegro ups.com...
Hi People

Could anyone suggest to me the best way to distinguish treenodes? By
this I mean I'm in a situation where I have a TreeNode that displays a
number of categories and within those nodes there are items of stock.

There are a number of things I wish to do, such as use a ContextMenu to
Rename the node, but I can't seemingly tell if the Node I'm renaming is
a category or an item of stock.

At the moment I put the CategoryId or StockId unique value as the
TreeNode name.

Thank you for your time
Peter


Mar 29 '06 #3

P: n/a
I could do with something similiar, can you tell me if you think this
would work for me, and if theres any advantage in working with controls
this way than how I currently am.

At the moment i'm using the treenodes and each treenode needs a unique
entry into my rich text box. After sitting at home with MSDN i've
managed to get this functionality by storing a RTF string in the tag
property of the treenode. On every 'before update' of the treeview I
save the current RTF string from the rich textbox to the tag property
of the selected node. On every 'after update' I pull in the RTF from
the selected tag property into the rich text box.

Would it be better for me to have a new class with a richtext property
or something? or is the way i'm doing it perfectly OK?

Am i right in thinking a new class would mean that i'd create new
objects which would have all the functionality of the treeview control,
but also extra properties / methods that I specify?

Finally I don't understand the last post which refers to common
interface and polymorphism could someone explain please.

Many Thanks.

Gary.

Mar 29 '06 #4

P: n/a
Hi,
One way is to derive a class from treenode for each type of thing that you
want to hang on the tree. To add to that: rather than checking its type give your derived TreeNodes a
common interface and use polymorphism.


Could anyone post a link to some sample article or code that
illustrates how to do this. I hadn't thought of this way to accomplish
this.

Thanks,

Cerebrus.

Mar 29 '06 #5

P: n/a

"Cerebrus" <zo*****@sify.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Hi,
One way is to derive a class from treenode for each type of thing that
you
want to hang on the tree. To add to that: rather than checking its type give your derived
TreeNodes a
common interface and use polymorphism.


Could anyone post a link to some sample article or code that
illustrates how to do this. I hadn't thought of this way to accomplish
this.


It's totally trivial eg a directory tree type app:

public interface IMyNode { void MyOp(); }

public class MyFileNode : TreeNode, IMyNode
{
....
}

public class MyDirectoryNode : TreeNode, IMyNode
{
....
}

....
treeView.Nodes.Add( new MyDirectoryNode(@"C:\Program Files") );
....

TreeNode node;
IMyNode myNode = (IMyNode)node; // ALL nodes must implement it (or us "as"
and test)
myNode.MyOp(); // does directory thing or file thing according to node type


Mar 29 '06 #6

P: n/a

"Nick Hounsome" <nh***@nickhounsome.me.uk> wrote in message
news:qw********************@fe3.news.blueyonder.co .uk...

"Cerebrus" <zo*****@sify.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Hi,
One way is to derive a class from treenode for each type of thing that
you
want to hang on the tree.
To add to that: rather than checking its type give your derived
TreeNodes a
common interface and use polymorphism.


Could anyone post a link to some sample article or code that
illustrates how to do this. I hadn't thought of this way to accomplish
this.


It's totally trivial eg a directory tree type app:

public interface IMyNode { void MyOp(); }

public class MyFileNode : TreeNode, IMyNode
{
....
}

public class MyDirectoryNode : TreeNode, IMyNode
{
....
}

...
treeView.Nodes.Add( new MyDirectoryNode(@"C:\Program Files") );
...

TreeNode node;


I should have mentioned that this node should be one given to you by the
TreeView NOT just an unititialized variable.
IMyNode myNode = (IMyNode)node; // ALL nodes must implement it (or us "as"
and test)
myNode.MyOp(); // does directory thing or file thing according to node
type

Mar 29 '06 #7

P: n/a
Hi Nick

Thanks for the example, that's just what I was looking for. I'm really
new to c# but I think I can figure it out from what you've written.

Thanks again
Peter

Mar 30 '06 #8

P: n/a
Thanks a lot for that example, Nick. You rock !

Regards,

Cerebrus.

Mar 30 '06 #9

P: n/a
I believe this is what the "Tag" property is there for. You can assign
it to whatever you want ( in this instance you can create your own
class with info about your nodes )

RichS

Mar 30 '06 #10

P: n/a
I found this article, It's in VB.NET but looks interesting....

Use Inheritance to Create a Custom TreeView
http://www.devcity.net/PrintArticle.aspx?ArticleID=23

Peter

Mar 30 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.