Solved

TreeView control and an object

Posted on 2001-09-12
10
693 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Use closed file on desktop in vba 6 67
Concatenate 2 userform comboboxes to a userform text box 4 56
VB 6.0 printer how to align 6 58
using web browser with BING 40 111
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

867 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

19 Experts available now in Live!

Get 1:1 Help Now