What is the best way to distinguish between TreeNodes

D

DrPete

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
 
B

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
 
N

Nick Hounsome

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

garyusenet

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.
 
C

Cerebrus

Hi,

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.
 
N

Nick Hounsome

Cerebrus said:
Hi,


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
 
N

Nick Hounsome

Nick Hounsome said:
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.
 
D

DrPete

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
 
R

RichS

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top