Treeview -> TreeNode -> Object

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

Craig Lister

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
 
A

AlexS

Yes,

myObjectType myObject = new myObjectType( ...);
....
treeView.SelectedNode.Tag = myObject;
....
((myObjectType)treeView.SelectedNode.Tag).MyObjectProperty = newValue;
....
HTH
Alex
 
C

Craig Lister

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
 
M

Morten Wennevik

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);
}
 

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