Treeview -> TreeNode -> Object

  • Thread starter Thread starter Craig Lister
  • Start date Start date
C

Craig Lister

I have a treeview with a Root, a Contact Type, and a Contact. Like MSN
Messenger.

Is there a way that I can say something like

if treenode.tag is classContact then
string Surname = treenode.tag.surname

And somehow, each time, I create the node, I can assign the object of that
classContact to that node ?

I think the 'Tag' part of the TreeNode would be used, but not sure how...
 
Thanks.

I'm looking to assign a class object that I have created, to my treenode
though...

Something like Treeview.SelectedNode.Tag = myObject..

And then later, I'd like to be able to access this objects properties via
the treenode.tag. Is this possible ?

Craig
 
Yes,

myObjectType myObject = new myObjectType( ...);
....
treeView.SelectedNode.Tag = myObject;
....
((myObjectType)treeView.SelectedNode.Tag).MyObjectProperty = newValue;
....
HTH
Alex
 
Thanks for the reply..

Getting an error:
Unable to cast object of type 'System.Int32' to type
'Address_Book_on_Steroids.classContact'.

on the following line:

MessageBox.Show(((classContact)tvMaster.SelectedNode.Tag).Title);



Where



tvMaster is the TreeView
 
Hi Craig,

Your Tag contains a number (System.Int32) and trying to cast it to classContact will then fail.
Are all your tags classContact? Or are some of them different data types.

object o = tvMaster.SelectedNode.Tag
if(o is classContact)
{
classContact c = (classContact)o;
MessageBox.Show(c.Title);
}
 
Back
Top