Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

TreeView control and an object

Posted on 2001-09-12
10
Medium Priority
?
708 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 6

Accepted Solution

by:
andyclap earned 400 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

604 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