Setting a control's tag property to reference an object


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 13 of 13

Thread: Setting a control's tag property to reference an object

  1. #1
    Ara Ashikian Guest

    Setting a control's tag property to reference an object

    What is the recommended way of setting a treeview.tag property to reference
    an object? Does this involve copy memory approach? Is there a safe way of
    doing this (+ being able to debug in IDE) without memory leak crashes?

    -Ara



  2. #2
    Arthur Wood Guest

    Re: Setting a control's tag property to reference an object


    Ara,
    Since the Tag property is a String value, I doubt that you can accomplish
    what you are asking.

    Arthur Wood


    "Ara Ashikian" <ashikian@structural-concepts.com> wrote:
    >What is the recommended way of setting a treeview.tag property to reference
    >an object? Does this involve copy memory approach? Is there a safe way

    of
    >doing this (+ being able to debug in IDE) without memory leak crashes?
    >
    >-Ara
    >
    >



  3. #3
    Brad Siemens Guest

    Re: Setting a control's tag property to reference an object

    Ara

    If you use the SSTreeView from Infragistics (Sheridan), it has a TagVariant
    Property that will hold an object.

    Set SSTReeView.Nodes(oObject.Key).TagVariant = oObject

    Otherwise I think you're stuck with maintaining the object hierarchy some
    other way.

    Brad


    "Ara Ashikian" <ashikian@structural-concepts.com> wrote in message
    news:3b71bf13$1@news.devx.com...
    > What is the recommended way of setting a treeview.tag property to

    reference
    > an object? Does this involve copy memory approach? Is there a safe way

    of
    > doing this (+ being able to debug in IDE) without memory leak crashes?
    >
    > -Ara
    >
    >




  4. #4
    D. Sainsbury Guest

    Re: Setting a control's tag property to reference an object

    I use ObjPtr() to get a pointer to an object. This is an undocumented
    function in
    VB

    eg
    Text1. Tag= CStr( ObjPtr(myObject))


    "Ara Ashikian" <ashikian@structural-concepts.com> wrote in message
    news:3b71bf13$1@news.devx.com...
    > What is the recommended way of setting a treeview.tag property to

    reference
    > an object? Does this involve copy memory approach? Is there a safe way

    of
    > doing this (+ being able to debug in IDE) without memory leak crashes?
    >
    > -Ara
    >
    >




  5. #5
    Anthony Jones Guest

    Re: Setting a control's tag property to reference an object

    Ara,

    Do you really mean the Treeview.Tag?

    I use:

    Set oNode.Tag = Item

    For nodes in treeviews, never caused me an problems.

    --
    Anthony Jones
    Nuesoft Ltd




  6. #6
    Ara Ashikian Guest

    Re: Setting a control's tag property to reference an object

    Although the tag is a string property, I have seen some workarounds to use
    it to store a pointer to an object. Vbaccelerator.com had a
    recommendation/sample using CopyMemory in conjuction with ObjPtr and a type
    lib in order to have access to the COM AddRef/Release. I was wondering if
    anyone had opinions/suggestions.



  7. #7
    Peter Young Guest

    Re: Setting a control's tag property to reference an object

    > What is the recommended way of setting a treeview.tag property to
    reference
    > an object? Does this involve copy memory approach? Is there a safe way

    of
    > doing this (+ being able to debug in IDE) without memory leak crashes?
    >


    The TreeView Node object's Tag property is a Variant, and as Anthony pointed
    out, you can assign an object to that just fine.

    As for the TreeView.Tag property, it depends on the scope of the object you
    want to reference. If the object will remain in scope for the duration of
    the time you need it, then you don't care about AddRef/Release. All you need
    is to store its pointer as a String, as was mentioned in another reply. e.g.
    TreeView.Tag = CStr(ObjPtr(oObject))

    Of course, that only gets you halfway there. When you want to access the
    object from the Tag property, you would use something like this:

    Function MyObjectFromPointer(ByVal lPtr As Long) As MyObject
    Dim oTemp As MyObject
    CopyMemory oTemp, lPtr, 4
    Set ObjectFromPointer = oTemp
    CopyMemory oTemp, 0&, 4
    End Function

    Dim oObjectAs MyObject

    Set oObject= ObjectFromPointer(CLng(TreeView.Tag))

    Again, you must be positive that the object still exists.

    If you need to keep a strong reference to the object, then AddRef/Release
    are required, and getting at that is possible, but I've personally never
    seen it used or recommended.

    -Pete



  8. #8
    Ara Ashikian Guest

    Re: Setting a control's tag property to reference an object

    You are right: the node.tag is a variant that can hold a strong reference to
    an object. For some reason, I assumed it had to be a string. Thanks for all
    the feedback.

    -Ara



  9. #9
    Brad Siemens Guest

    Re: Setting a control's tag property to reference an object

    Well dip me in ... I stand corrected!

    Thanks for the Heads up
    Brad

    "Peter Young" <youngpa@mediaone.net.nospam> wrote in message
    news:3b72a58c$1@news.devx.com...
    > > What is the recommended way of setting a treeview.tag property to

    > reference
    > > an object? Does this involve copy memory approach? Is there a safe way

    > of
    > > doing this (+ being able to debug in IDE) without memory leak crashes?
    > >

    >
    > The TreeView Node object's Tag property is a Variant, and as Anthony

    pointed
    > out, you can assign an object to that just fine.
    >
    > As for the TreeView.Tag property, it depends on the scope of the object

    you
    > want to reference. If the object will remain in scope for the duration of
    > the time you need it, then you don't care about AddRef/Release. All you

    need
    > is to store its pointer as a String, as was mentioned in another reply.

    e.g.
    > TreeView.Tag = CStr(ObjPtr(oObject))
    >
    > Of course, that only gets you halfway there. When you want to access the
    > object from the Tag property, you would use something like this:
    >
    > Function MyObjectFromPointer(ByVal lPtr As Long) As MyObject
    > Dim oTemp As MyObject
    > CopyMemory oTemp, lPtr, 4
    > Set ObjectFromPointer = oTemp
    > CopyMemory oTemp, 0&, 4
    > End Function
    >
    > Dim oObjectAs MyObject
    >
    > Set oObject= ObjectFromPointer(CLng(TreeView.Tag))
    >
    > Again, you must be positive that the object still exists.
    >
    > If you need to keep a strong reference to the object, then AddRef/Release
    > are required, and getting at that is possible, but I've personally never
    > seen it used or recommended.
    >
    > -Pete
    >
    >




  10. #10
    Arthur Wood Guest

    Re: Setting a control's tag property to reference an object


    Me too...the crow is delicious this time of year, especially with Chipotle
    BBG sauce. <LOL>


    "Brad Siemens" <ebspc@sympatico.ca> wrote:
    >Well dip me in ... I stand corrected!
    >
    >Thanks for the Heads up
    >Brad
    >
    >"Peter Young" <youngpa@mediaone.net.nospam> wrote in message
    >news:3b72a58c$1@news.devx.com...
    >> > What is the recommended way of setting a treeview.tag property to

    >> reference
    >> > an object? Does this involve copy memory approach? Is there a safe

    way
    >> of
    >> > doing this (+ being able to debug in IDE) without memory leak crashes?
    >> >

    >>
    >> The TreeView Node object's Tag property is a Variant, and as Anthony

    >pointed
    >> out, you can assign an object to that just fine.
    >>
    >> As for the TreeView.Tag property, it depends on the scope of the object

    >you
    >> want to reference. If the object will remain in scope for the duration

    of
    >> the time you need it, then you don't care about AddRef/Release. All you

    >need
    >> is to store its pointer as a String, as was mentioned in another reply.

    >e.g.
    >> TreeView.Tag = CStr(ObjPtr(oObject))
    >>
    >> Of course, that only gets you halfway there. When you want to access the
    >> object from the Tag property, you would use something like this:
    >>
    >> Function MyObjectFromPointer(ByVal lPtr As Long) As MyObject
    >> Dim oTemp As MyObject
    >> CopyMemory oTemp, lPtr, 4
    >> Set ObjectFromPointer = oTemp
    >> CopyMemory oTemp, 0&, 4
    >> End Function
    >>
    >> Dim oObjectAs MyObject
    >>
    >> Set oObject= ObjectFromPointer(CLng(TreeView.Tag))
    >>
    >> Again, you must be positive that the object still exists.
    >>
    >> If you need to keep a strong reference to the object, then AddRef/Release
    >> are required, and getting at that is possible, but I've personally never
    >> seen it used or recommended.
    >>
    >> -Pete
    >>
    >>

    >
    >



  11. #11
    Brad Siemens Guest

    Re: Setting a control's tag property to reference an object

    Arthur

    I prefer crow tartar.... much quicker to digest <g>

    Brad

    "Arthur Wood" <wooda@saic-trsc.com> wrote in message
    news:3b74726a$1@news.devx.com...
    >
    > Me too...the crow is delicious this time of year, especially with Chipotle
    > BBG sauce. <LOL>
    >
    >
    > "Brad Siemens" <ebspc@sympatico.ca> wrote:
    > >Well dip me in ... I stand corrected!
    > >
    > >Thanks for the Heads up
    > >Brad
    > >
    > >"Peter Young" <youngpa@mediaone.net.nospam> wrote in message
    > >news:3b72a58c$1@news.devx.com...
    > >> > What is the recommended way of setting a treeview.tag property to
    > >> reference
    > >> > an object? Does this involve copy memory approach? Is there a safe

    > way
    > >> of
    > >> > doing this (+ being able to debug in IDE) without memory leak

    crashes?
    > >> >
    > >>
    > >> The TreeView Node object's Tag property is a Variant, and as Anthony

    > >pointed
    > >> out, you can assign an object to that just fine.
    > >>
    > >> As for the TreeView.Tag property, it depends on the scope of the object

    > >you
    > >> want to reference. If the object will remain in scope for the duration

    > of
    > >> the time you need it, then you don't care about AddRef/Release. All you

    > >need
    > >> is to store its pointer as a String, as was mentioned in another reply.

    > >e.g.
    > >> TreeView.Tag = CStr(ObjPtr(oObject))
    > >>
    > >> Of course, that only gets you halfway there. When you want to access

    the
    > >> object from the Tag property, you would use something like this:
    > >>
    > >> Function MyObjectFromPointer(ByVal lPtr As Long) As MyObject
    > >> Dim oTemp As MyObject
    > >> CopyMemory oTemp, lPtr, 4
    > >> Set ObjectFromPointer = oTemp
    > >> CopyMemory oTemp, 0&, 4
    > >> End Function
    > >>
    > >> Dim oObjectAs MyObject
    > >>
    > >> Set oObject= ObjectFromPointer(CLng(TreeView.Tag))
    > >>
    > >> Again, you must be positive that the object still exists.
    > >>
    > >> If you need to keep a strong reference to the object, then

    AddRef/Release
    > >> are required, and getting at that is possible, but I've personally

    never
    > >> seen it used or recommended.
    > >>
    > >> -Pete
    > >>
    > >>

    > >
    > >

    >




  12. #12
    Kees Hoogakker Guest

    Re: Setting a control's tag property to reference an object


    Using the ObjPtr function is not a good idea if you plan to migrate to the
    .Net technology in the future. It is no longer there.
    Furthermore the node might contain the reference to an object which no longer
    exists.
    Another solution is to build a collection filled with your objects.
    Use a specific key to identify each object and put the object's key in the
    tag property of the tree node associated with this object.

    No memory leaks, safe to debug. However do not forget to cleanup your collection
    when closing the form! Simply setting the collection to nothing might not
    be enough. Better to remove all members and then set it to nothing.

    Kees

    "D. Sainsbury" <dsains@bluep.com> wrote:
    >I use ObjPtr() to get a pointer to an object. This is an undocumented
    >function in
    >VB
    >
    >eg
    >Text1. Tag= CStr( ObjPtr(myObject))
    >
    >
    >"Ara Ashikian" <ashikian@structural-concepts.com> wrote in message
    >news:3b71bf13$1@news.devx.com...
    >> What is the recommended way of setting a treeview.tag property to

    >reference
    >> an object? Does this involve copy memory approach? Is there a safe way

    >of
    >> doing this (+ being able to debug in IDE) without memory leak crashes?
    >>
    >> -Ara
    >>
    >>

    >
    >



  13. #13
    Greg Osborne Guest

    Re: Setting a control's tag property to reference an object


    "Ara Ashikian" <ashikian@structural-concepts.com> wrote:
    >What is the recommended way of setting a treeview.tag property to reference
    >an object? Does this involve copy memory approach? Is there a safe way

    of
    >doing this (+ being able to debug in IDE) without memory leak crashes?
    >
    >-Ara
    >
    >

    We can not only display hierarchical data using the Microsoft TreeView control,
    but can also store that data using our control, and this negates the use
    of some other structure to manage the data. As an example, I will walk through
    an a scenario you may be familiar with, a data structure which tracks farms.
    You can think of each of the pseudo-data elements below as a database table…

    Accounts store account information (ID, NAME)
    Farms store data about a farm (ID, ACCTID, NAME, STATE, COUNTY)
    Fields store data about a field (ID, FARMID, NAME, ACRES)

    This is very simple data but will suffice for our example. We have accounts
    that would uniquely identify a user. Each account may have many farms, linked
    to the Accounts data store by ACCTID. Each farm may have many fields, linked
    to Farms data store by FARMID. As we can see this is hierarchical and can
    be represented in a TreeView

    Acct1
    Farm1
    Field1
    Field2
    Field3
    Farm2
    Field1
    Acct2
    Farm1
    Field1
    Field2
    Field3
    Farm2
    Field1
    Farm3
    Field1
    Field2
    Acct3

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center