Solved

TreeView control and an object

Posted on 2001-09-12
10
692 Views
Last Modified: 2013-11-26
This should not be a big deal, the answer may be "you do it this way" or "no, can't do".

I have a TreeView control being used to display some data coming from a hierarchical object structure. There is much more to each node than just a "text" and an "image", the objects involved are quite complex.

So, to be able to make decisions when the user click the tree, or drag&drop stuff around, I need an easy way to get a reference back to the corresponding object in my structure.  But there is no way to store a object reference on a tree node, just a "key" or a "tag", both not what I'm looking for.

Question: does anyone happen to know how to store a individual object reference (a pointer for the more C-like) within a node object?

Thanks!

Claudio A. Heckler
0
Comment
Question by:ca_heckler
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 6

Accepted Solution

by:
andyclap earned 100 total points
ID: 6477931
The tag of a treeview node is a variant, which can quite happily store an object

eg
dim nod as node
set nod=treeview1.nodes.add(,,,"Node with an object")
set nod.tag=new collection
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6477967
Interesting...
0
 
LVL 11

Expert Comment

by:thman
ID: 6477994
I do like these:

Way 1. Use "key" to locate the object
Let's say your objects are stored in a collection or more collections and the key of each node of the treeview is associated to a key of an item of a collection. You should also use the tag property to categorize different types of objects, fox ex. you can tag each level of your objects' hierarchy to a special name (let's say the collection name).

Way 2. Trough a class to operate the treeview
Define a class that knows where your objects are and has methods to associate objects to the treeview. Usually the instances of that class are your objects.
0
 
LVL 11

Expert Comment

by:thman
ID: 6478013
Use tag property to store an object?

Maybe I am too serious about the explanation of the tag property from MSDN to make an easy use of it. It says "You can use the Tag property to pass values, but it does not allow you to pass objects". I'll try this.
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6478052
That's strange, it says that in my help file too... but it works.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 1

Author Comment

by:ca_heckler
ID: 6478072
You know what I hate the most about MSDN? It does not give a damm about data types. Look at the TAG property and you see no clue about the underlying datatype. You need to add a stop at the object browser to get the complete information.

Well, although MSDN states (as THMAN also mentioned) that "it does not allow you to pass objects", I decided to follow the suggestion and it worked just fine. On the same process context, I don't expect any marshaling to take place here (but I'll try it someday)

THMAN: Your "way1" don't work for me because my structure is hierarchical, I just don't have a single collection of objects; about "way2", that works one way usually: the objects knows its location on the tree, but given the tree node you still need a object reference to find the other way around (that's where the TAG trick before plays an important role)

ANDYCLAP: thanks for pointing it out.  I tried it and it just worked out fine.  Just a warning to someone trying the same thing: do some hard testing to make sure that the destruction of the TreeView object decreases the object count for those references stored in the TAG property. It may be a bit difficult to test in VB, so I'll probably build a stress test, leave it running and watch for memory leaks.

Thanks everybody for the good brainstorming session!

Claudio
0
 
LVL 11

Expert Comment

by:thman
ID: 6478119
Use tag property to store an object?

Maybe I am too serious about the explanation of the tag property from MSDN to make an easy use of it. It says "You can use the Tag property to pass values, but it does not allow you to pass objects". I'll try this.
0
 
LVL 11

Expert Comment

by:thman
ID: 6478136
Sorry for the comment just above. I didn't mean to bother you. I refreshed this page and it sent the comment again.

I tried the tag property after dinner. Yes, it works just fine!! Thank you, Andy!

Claudio, the two ways I mentioned were the ways I used before, either in VB or VJ. I would select one of them depending on what situation I was in. I'm going to abandon them. I really have to thank Andy. And you too, if you hadn't post this question, I would've still been with my silly ways.
0
 
LVL 1

Author Comment

by:ca_heckler
ID: 6478188
Well, brainstorming is the beauty of forums like this and the newsgroups.

More on the subject:

Look at this KB for VB5: it states that it *should* work the way we expect (accepting objects), but there is a bug, corrected in version 6 (why the current documentation don't match)?
http://support.microsoft.com/support/kb/articles/Q176/3/92.ASP

Any news you guys may find, lets port it here ...

[]s
Claudio
0
 
LVL 6

Expert Comment

by:andyclap
ID: 6478254
ca_heckler - thanks for the link.
I remember something like this back in my VB5 days, and used to use a simple tracking collection (keyed on a cstr of the objptr of the node).
I've used the tag property in a couple of VB6 projects now, with good results and no memory leaks.

Mind you, I use my own home-grown tree control now, where the objects 'are' the nodes, they just implement an ITree interface which has a single method which returns a children collection. Makes building a tree a doddle (set tree1.root=object, bingo!)
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now