Little question about a TreeTag

  • Thread starter Thread starter tiger79
  • Start date Start date
T

tiger79

Hi uber-programmers ;)
I have made a TreeView (yes again ;) ) and I made a struct(ure) so that I
could define my own object to place in the Nodes.Tag propery.
But now I'd like to query the node to see which kind of structure it has as
a Tag, something like this :

if (treeView1.SelectedNode.Tag == EntityTag() )
{
blah blah blah
}

else if (treeView1.SelectedNode.Tag == AttributeTag() )
{
blah blah again
)

So you can see I will be using a couple of structs.
If I use this code it will (obviously) give me an error :
'FinalDemo1.EntityTag' denotes a 'class' where a 'variable' was expected

ok, I see it doesnt want a clas (or struct I guess) so is there a way to
check (in a fast and easy way) which kind of struct is in the Tag ???
 
You can use treeView1.SelectedNode.Tag.GetType() to return the type of
object stored in the tag - since you can assign any type of object to the
tag property. You can then determine if it is one of your custom types and
cast it accordingly. You will need to define your custom types as classes
rather than structs since Tag requires a reference type (object).

Peter
 
Peter Foot said:
You can use treeView1.SelectedNode.Tag.GetType() to return the type of
object stored in the tag - since you can assign any type of object to the
tag property. You can then determine if it is one of your custom types and
cast it accordingly.

hhmm... dont get this :( How can I determine if the returned object is one
of mine ???
I did something like this :
object test;

test = treeView1.SelectedNode.Tag.GetType();

if (test == FinalDemo1.EntityTag())

{

}



but that wont work...

You will need to define your custom types as classes
rather than structs since Tag requires a reference type (object).
Ok, I get the fact that I should be making classes in which to define the
different types of objects i'd like, but what about that casting ? I don't
understand what u mean :(

Is there maybe a way of knowing the depth or level of the selected node ?
Cause I could use that instead... I know that level 1 for exam ple are all
Entities, and level 2 are all attributes and level 3 attributevalues and so
on....
 
how about this

Object mytag = treeView1.SelectedNode.Tag;

if (mytag.GetType()== typeof(FinalDemo1.EntityTag()))
{
EntityTag et = (EntityTag)mytag;

//do something with et here
}

Peter

--
Peter Foot
Windows Embedded MVP
www.inthehand.com | www.opennetcf.org
 
Back
Top