Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: How to store and retrieve struct or class into TreeViewItem->Data


This question is answered.


Permlink Replies: 11 - Last Post: Jul 28, 2017 1:10 PM Last Post By: Remy Lebeau (Te...
Yilmaz Yoru


Posts: 167
Registered: 1/1/03
How to store and retrieve struct or class into TreeViewItem->Data  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2017 7:05 PM
Last few days I stuck in storing to a TreeViewItem->Data. I found some examples (mostly VCL) but those are old and not compatible with Tokyo C++ Builder.

I have a TreeView and each its TreeViewItem will be created with its button and I want to store this button addresses and some other database IDs in my struct or class.
I want to store this struct or class into TreeViewItem->Data and when button of its TreeViewItem is clicked I want to retrieve data parameters back to my struct or class.
Sometimes I am having Internal Errors, No success yet :D

With a TreeView and a Button; A Simple example is like as below ;

//------------------------------------------------------
struct st_nodedb
{
unsigned int nodeID;
TButton *button;
} ;

class Tnodedb
{
public:
unsigned int nodeID;
TButton *button;
};

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TTreeViewItem *node = new TTreeViewItem(TreeView1);

node->Parent = TreeView1;
node->Text = "TEST";

TreeView1->Selected=node;

if (node->Data.IsEmpty)
{
Tnodedb *nodedb=new(Tnodedb);
nodedb->button=Button1;
node->Data = nodedb; // node->Data.From< Tnodedb* >(nodedb); // stuck in this line getting errors <void *> and (void *) doesnt work also
}
}
// and when click i want to retrieve data or pointer to my struct or class
nodedb= node->Data; // and stuck in this line

...
I know node->Data is TValue
I try to use Data.From(), Data.FromClass(), Data.AsClass() and some other techniques by using TValue, TValueRec etc.

Hahah I feel so bad now, cuz stuck in this simple thing with old examples and all my efforts are not working and i am missing something here

Thanks for answers

--
Yilmaz Yoru

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 24, 2017 7:45 PM   in response to: Yilmaz Yoru in response to: Yilmaz Yoru
Yilmaz Yoru wrote:

Last few days I stuck in storing to a TreeViewItem->Data. I found
some examples (mostly VCL) but those are old and not compatible with
Tokyo C++ Builder.

In VCL, the TTreeNode::Data property is a raw void* pointer. You can
store whatever you want in it.

In FMX, the TTreeViewItem::Data property is a TValue struct, which is
not designed to hold void* pointers (only TObject pointers and
IInterface pointers), so you will have to either:

1. derive your struct from TObject.

2. type-cast your struct pointers to int or __int64 (depending on
whether you are compiling for 32bit or 64bit) when storing them, and
cast them back when retreiving.

I have a TreeView and each its TreeViewItem will be created with its
button and I want to store this button addresses and some other
database IDs in my struct or class.

You might consider deriving a new class from TTreeViewItem and give it
data members for those values. Similar to VCL, FMX does not limit you
to just the base TTreeViewItem class.

class TMyTreeViewItem : public TTreeViewItem
{
public:
    __fastcall TMyTreeViewItem(TComponent *AOwner);
 
    unsigned int nodeID;
    TButton *button;
};
 
__fastcall TMyTreeViewItem::TMyTreeViewItem(TComponent *AOwner)
    : TTreeViewItem(AOwner)
{
}
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TMyTreeViewItem *node = new TMyTreeViewItem(TreeView1);
    try
    {
        node->Parent = TreeView1;
        node->Text = L"TEST";
 
        node->nodeID = ...;
        node->button = new TButton(node);
        node->button->Parent = ...;
        // configure other button properties as needed...
        node->button->OnClick = &TreeViewButtonClicked;
        node->button->Data = node;
    }
    catch (const Exception &)
    {
        delete node;
        throw;
    }
 
    TreeView1->Selected = node;
}
 
void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    TMyTreeViewItem *node =
static_cast<TMyTreeViewItem*>(btn->Data.AsObject);
    // use node as needed...
}


I want to store this struct or class into TreeViewItem->Data and when
button of its TreeViewItem is clicked I want to retrieve data
parameters back to my struct or class.

If you really want to use TTreeViewItem::Data, then try something like
this:

class Tnodedb : public TObject
{
public:
    unsigned int nodeID;
    TButton *button;
};
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TTreeViewItem *node = new TTreeViewItem(TreeView1);
    try
    {
        node->Parent = TreeView1;
        node->Text = L"TEST";
 
        Tnodedb *nodedb = new Tnodedb;
        nodedb->nodeID = ...;
        nodedb->button = new TButton(node);
        nodedb->button->Parent = ...;
        // configure other button properties as needed...
        nodedb->button->OnClick = &TreeViewButtonClicked;
        nodedb->button->Data = node;
 
        node->Data = nodedb;
    }
    catch (const Exception &)
    {
        delete node;
        throw;
    }
 
    TreeView1->Selected = node;
}
 
void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    TTreeViewItem *node =
static_cast<TTreeViewItem*>(btn->Data.AsObject);
    Tnodedb *nodedb = static_cast<Tnodedb*>(node->Data.AsObject);
    //...
}


Or:

class Tnodedb
{
public:
    unsigned int nodeID;
    TButton *button;
};
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
    TTreeViewItem *node = new TTreeViewItem(TreeView1);
    try
    {
        node->Parent = TreeView1;
        node->Text = L"TEST";
 
        Tnodedb *nodedb = new Tnodedb;
        nodedb->nodeID = ...;
        nodedb->button = new TButton(node);
        nodedb->button->Parent = ...;
        // configure other button properties as needed...
        nodedb->button->OnClick = &TreeViewButtonClicked;
        nodedb->button->Data = node;
 
        node->Data = reinterpret_cast<NativeInt>(nodedb);
    }
    catch (const Exception &)
    {
        delete node;
        throw;
    }
 
    TreeView1->Selected = node;
}
 
void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    TTreeViewItem *node =
static_cast<TTreeViewItem*>(btn->Data.AsObject);
    Tnodedb *nodedb =
reinterpret_cast<Tnodedb*>(node->Data.AsType<NativeInt>());
    //...
}


--
Remy Lebeau (TeamB)
Yilmaz Yoru


Posts: 167
Registered: 1/1/03
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 25, 2017 6:11 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy thank you for the fast and detailed response, i see it but i was so sleepy ,it is time to reply,

I have error in this line .

Remy Lebeau (TeamB) wrote:

node->button->Data = node;
--
Remy Lebeau (TeamB)

About the line above my RADS 10.2 Tokyo says "[bcc32 Error] Unit1.cpp(59): E2034 Cannot convert 'TMyTreeViewItem *' to 'TValue'
Full parser context Unit1.cpp(47): parsing: void _fastcall TForm1::Button1Click(TObject *)"

This is where i am stuck. I had similar problem on node->Data too.

--
Yilmaz Yoru
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 25, 2017 10:39 AM   in response to: Yilmaz Yoru in response to: Yilmaz Yoru
Yilmaz Yoru wrote:

I have error in this line .

node->button->Data = node;

About the line above my RADS 10.2 Tokyo says "[bcc32 Error]
Unit1.cpp(59): E2034 Cannot convert 'TMyTreeViewItem *' to 'TValue'

TValue has overloaded assignment operators, and one of them accepts a
TObject* as input. Since TMyTreeViewItem ultimately derives from
TObject, a TMyTreeViewItem* pointer should be assignable to a TValue.

However, my mistake is forgetting that Data is a property, not a data
field, and so you can't call the TValue assignment operators on TValue
property. You have to assign an actual TValue only. So, try this
instead:

node->button->Data = TValue::From<TMyTreeViewItem*>(node);


Or even:

node->button->Data = TValue::From<TObject*>(node);


--
Remy Lebeau (TeamB)
Yilmaz Yoru


Posts: 167
Registered: 1/1/03
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 25, 2017 8:35 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks now it compiles successfully, we need one more step.

When I click to button of the TreeViewItem shows Error Message "Invalid Class Typecast"

void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
TButton *btn = static_cast<TButton*>(Sender);
TMyTreeViewItem *node = static_cast<TMyTreeViewItem*>(btn->Data.AsObject()); // << error here
//ShowMessage(IntToStr((int)node->nodeID));
}

--
Yilmaz Yoru

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 26, 2017 9:48 AM   in response to: Yilmaz Yoru in response to: Yilmaz Yoru
Yilmaz Yoru wrote:

When I click to button of the TreeViewItem shows Error Message
"Invalid Class Typecast"

I don't see how that is possible, given that:

1. the TValue is holding a TObject-derived object pointer.

2. TValue::AsObject() returns a TObject* pointer.

3. static_cast can type-cast a base class pointer to a derived class
pointer.

Which example of TValue::From() are you using? That one that takes
TMyTreeViewItem* or TObject* as input?

--
Remy Lebeau (TeamB)
Yilmaz Yoru


Posts: 167
Registered: 1/1/03
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 26, 2017 10:58 AM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:
Which example of TValue::From() are you using? That one that takes
--
Remy Lebeau (TeamB)

Yess I use the first one, TValue::From()

Here is the whole test example, succesfully complied and runs, buttonclick adds nodes and when i clck button created in the item of treeview error msg occurs "invalid class typecast".

//---------------------------------------------------------------------------

#include <fmx.h>
#pragma hdrstop

#include "TreeView_Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;

class TMyTreeViewItem : public TTreeViewItem
{
public:
__fastcall TMyTreeViewItem(TComponent *AOwner);

unsigned int nodeID;
TButton *button;
};

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{

}

//---------------------------------------------------------------------------
__fastcall TMyTreeViewItem::TMyTreeViewItem(TComponent *AOwner) : TTreeViewItem(AOwner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
TButton *btn = static_cast<TButton*>(Sender);
TMyTreeViewItem *node = static_cast<TMyTreeViewItem*>(btn->Data.AsObject()); // error msg here on run

ShowMessage(IntToStr((int)node->nodeID));

}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMyTreeViewItem *node = new TMyTreeViewItem(TreeView1);
TButton *button;

try
{
node->Parent = TreeView1;
node->Text = L"TEST";

node->nodeID = 67;
button = new TButton(node);
button->Parent = node;
// configure other button properties as needed...
button->OnClick = &TreeViewButtonClicked;
button->Data = TValue::From(node);
button->Text = IntToStr(node->Index);
node->button = button;

}
catch (const Exception &)
{
delete node;
throw;
}

TreeView1->Selected = node;
}

--
Yilmaz Yoru

Edited by: Yilmaz Yoru on Jul 26, 2017 11:06 AM

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit] [Edit]
Correct
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 26, 2017 11:38 AM   in response to: Yilmaz Yoru in response to: Yilmaz Yoru
Yilmaz Yoru wrote:

Here is the whole test example, succesfully complied and runs,
buttonclick adds nodes and when i clck button created in the item
of treeview error msg occurs "invalid class typecast".

That error comes from TValue::AsObject(). The ONLY time it throws that
error is when TValue::IsObject() returns false.

The only time TValue::IsObject() returns false is when
TValue::IsEmpty() returns false, and TValue::Kind does not return
tkClass.

Since you are clearly storing a class object pointer in your TValue,
IsEmpty() should not be false, which means Kind is wrong, which means
TValue is not picking up the correct RTTI for your TMyTreeViewItem
class. And that is actually a known problem with Delphi Generics, and
is documented on Embarcadero's DocWiki:

http://docwiki.embarcadero.com/RADStudio/en/How_to_Handle_Delphi_Generics_in_C++

Since TObject is already known and instantiated on the Delphi side of
the RTL, you might try simply specifying TObject* explicitly when
calling TValue::From(), like I showed you earlier:

button->Data = TValue::From<TObject*>(node); 


That will make From() take a TObject* pointer as input instead of a
TMyTreeViewItem* pointer (the code will still compile since a
TMyTreeViewItem* pointer is implicitly convertible to a TObject*
pointer) and hopefully TValue will pick up the RTTI of TObject instead
of TMyTreeViewItem.

If that doesn't work, then you will have to resort to implementing
TMyTreeViewItem in Delphi code instead of C++ code (and yes, you can
use Delphi code in C++ projects), eg:

MyTreeViewItem.pas:

unit MyTreeViewItem;
 
interface
 
uses
  FMX.StdCtrls, FMX.TreeView;
 
type
  TMyTreeViewItem = class(TTreeViewItem)
  public
    nodeID: LongWord;
    button: TButton;
  end;
 
implementation
 
end.

TreeView_Unit1.cpp:

//----------------------------------------------------------------------
-----
 
#include <fmx.h>
#pragma hdrstop
 
#include "TreeView_Unit1.h"
#include "MyTreeViewItem.hpp" // <-- add this
 
//----------------------------------------------------------------------
-----
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
 
//----------------------------------------------------------------------
-----
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
 
}
 
//----------------------------------------------------------------------
-----
 
void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
	TButton *btn = static_cast<TButton*>(Sender);
	TMyTreeViewItem *node =
static_cast<TMyTreeViewItem*>(btn->Data.AsObject());
	ShowMessage(IntToStr(node->nodeID));
}
//----------------------------------------------------------------------
-----
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TMyTreeViewItem *node = new TMyTreeViewItem(TreeView1);
	TButton *button;
 
	try
	{
		node->Parent = TreeView1;
		node->Text = L"TEST";
 
		node->nodeID = 67;
		button = new TButton(node);
		button->Parent = node;
		// configure other button properties as needed...
		button->OnClick = &TreeViewButtonClicked;
		button->Data = TValue::From(node); 
		button->Text = IntToStr(node->Index);
		node->button = button;
 
	}
	catch (const Exception &)
	{
		delete node;
		throw;
	}
 
	TreeView1->Selected = node;
}

--
Remy Lebeau (TeamB)

Yilmaz Yoru


Posts: 167
Registered: 1/1/03
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 27, 2017 6:11 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Remy Lebeau (TeamB) wrote:

Since TObject is already known and instantiated on the Delphi side of
the RTL, you might try simply specifying TObject* explicitly when
calling TValue::From(), like I showed you earlier:

button->Data = TValue::From<TObject*>(node); 


That will make From() take a TObject* pointer as input instead of a
TMyTreeViewItem* pointer (the code will still compile since a
TMyTreeViewItem* pointer is implicitly convertible to a TObject*
pointer) and hopefully TValue will pick up the RTTI of TObject instead
of TMyTreeViewItem.

If that doesn't work, then you will have to resort to implementing
TMyTreeViewItem in Delphi code instead of C++ code (and yes, you can
use Delphi code in C++ projects), eg:

MyTreeViewItem.pas:

unit MyTreeViewItem;
 
interface
 
uses
  FMX.StdCtrls, FMX.TreeView;
 
type
  TMyTreeViewItem = class(TTreeViewItem)
  public
    nodeID: LongWord;
    button: TButton;
  end;
 
implementation
 
end.

TreeView_Unit1.cpp:

//----------------------------------------------------------------------
-----
 
#include <fmx.h>
#pragma hdrstop
 
#include "TreeView_Unit1.h"
#include "MyTreeViewItem.hpp" // <-- add this
 
//----------------------------------------------------------------------
-----
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
 
//----------------------------------------------------------------------
-----
__fastcall TForm1::TForm1(TComponent* Owner)
	: TForm(Owner)
{
 
}
 
//----------------------------------------------------------------------
-----
 
void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
	TButton *btn = static_cast<TButton*>(Sender);
	TMyTreeViewItem *node =
static_cast<TMyTreeViewItem*>(btn->Data.AsObject());
	ShowMessage(IntToStr(node->nodeID));
}
//----------------------------------------------------------------------
-----
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TMyTreeViewItem *node = new TMyTreeViewItem(TreeView1);
	TButton *button;
 
	try
	{
		node->Parent = TreeView1;
		node->Text = L"TEST";
 
		node->nodeID = 67;
		button = new TButton(node);
		button->Parent = node;
		// configure other button properties as needed...
		button->OnClick = &TreeViewButtonClicked;
		button->Data = TValue::From(node); 
		button->Text = IntToStr(node->Index);
		node->button = button;
 
	}
	catch (const Exception &)
	{
		delete node;
		throw;
	}
 
	TreeView1->Selected = node;
}

--
Remy Lebeau (TeamB)


Thanks again;

Case 1:
button->Data = TValue::From<TObject*>(node);

This results with "Acces Violation" Error on runtime with C++ class definition when I do Button1Click()

Case 2:

button->Data = TValue::From<TObject*>(node);

I also include pascal header and file and I removed C++ class as you mentioned above.
again results Acces Violation Error on runtime with Delphi implementation when I do Button1Click()

Case 3:
With Delphi Class when I do change as below
button->Data = TValue::From(node);


Button1 generates nodes with buttons on my TreeView1 when i click on one of a button (TreeViewButtonClicked () )
I receive same "Invalid class typecast Error" on runtime as it happened before with in C++ Class definitions.

Embarcadero should change "TreeViewItem->Data" property to "->Nightmare" :D lol. Too much fails on the same line about a week feels me bad :(

There is a way to do this creating classes or structs with ID numbers and saving ID numbers for each Button in "hint" or somewhere, then when click finding class by ID will operate.
But this is not what I want to do.

--
Yilmaz Yoru
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit] [Edit]
Helpful
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 27, 2017 8:31 PM   in response to: Yilmaz Yoru in response to: Yilmaz Yoru
Yilmaz Yoru wrote:
Case 1:

button->Data = TValue::From<TObject*>(node);


This results with "Acces Violation" Error on runtime with C++ class definition when I do Button1Click()

Yet, this is how the RTL source code documents Delphi-style objcts should be assigned to TValue in C++!

I don't know why it is crashing, and I don't really care to debug into it. It is clearly a bug on Embarcadero's side, they need to fix it.

button->Data = TValue::From(node);


Button1 generates nodes with buttons on my TreeView1 when i click on one of a button (TreeViewButtonClicked () )
I receive same "Invalid class typecast Error" on runtime as it happened before with in C++ Class definitions.

This, I did debug a little bit.

TValue::From(node) does construct a proper TValue containing a TTreeViewItem* pointer and a TTypeInfo* pointer to RTTI that describes a tkClass, as expected.

However, by the time the TValue round-trips into the TButton::OnClick handler, somehow it has been changed, because the TTypeInfo* pointer is now pointing at RTTI that describes a tkMethod instead! That is why AsObject() is throwing the error. I have no idea why the TValue is being altered, and again, I don't really care to debug into it. This is clearly another bug on Embarcadero's side, they need to fix it.

Embarcadero should change "TreeViewItem->Data" property to "->Nightmare" :D lol. Too much fails on the same line about a week feels me bad :(

I don't know what is going on, and I don't really care at this point, as there is a simpler solution that doesn't rely on RTTI at all. Just forget using the button's Data property altogether. Since the tree node is the button's Parent, just use the Parent property to access the node, instead of using the Data property:

void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
    TButton *btn = static_cast<TButton*>(Sender);
    TMyTreeViewItem *node = static_cast<TMyTreeViewItem*>(btn->Parent); // <-- here
    ShowMessage(node->nodeID);
}


--
Remy Lebeau (TeamB)
Yilmaz Yoru


Posts: 167
Registered: 1/1/03
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 27, 2017 9:50 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
:)

Thank You Very Much Remy,
Appreciated !

This works fine without pascal implementation, now users may have any parameters in TMyTreeViewItem Class.

//---------------------------------------------------------------------------
 
#include <fmx.h>
#pragma hdrstop
 
#include "TreeView_Unit1.h"
 
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
TForm1 *Form1;
 
 class TMyTreeViewItem : public TTreeViewItem
{
public:
	__fastcall TMyTreeViewItem(TComponent *AOwner);
 
	unsigned int nodeID;
	TButton *button;
};
 
//---------------------------------------------------------------------------
__fastcall TMyTreeViewItem::TMyTreeViewItem(TComponent *AOwner)	: TTreeViewItem(AOwner)
{
}
 
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
{
 
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TreeViewButtonClicked(TObject *Sender)
{
	TButton *btn = static_cast<TButton*>(Sender);
	TMyTreeViewItem *node = static_cast<TMyTreeViewItem *>(btn->Parent);
	ShowMessage(IntToStr((int)node->nodeID));
}
//---------------------------------------------------------------------------
 
void __fastcall TForm1::Button1Click(TObject *Sender)
{
	TMyTreeViewItem *node = new TMyTreeViewItem(TreeView1);
	TButton *button;
 
	try
	{
		node->Parent = TreeView1;
		node->Text = L"TEST";
 
		node->nodeID = node->Index*100+1;
		button = new TButton(node);
		button->Parent = node;
		// configure other button properties as needed...
		button->OnClick = &TreeViewButtonClicked;
		button->Data = TValue::From(node);
		button->Text = IntToStr(node->Index);
		node->button = button;
                TreeView1->Selected = node;
	}
	catch (const Exception &)
	{
		delete node;
		throw;
        }
}


--
Best Regards
Dr. Yilmaz Yoru
Esenja Company
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: How to store and retrieve struct or class into TreeViewItem->Data [Edit] [Edit] [Edit] [Edit]  
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jul 28, 2017 1:10 PM   in response to: Yilmaz Yoru in response to: Yilmaz Yoru
Yilmaz Yoru wrote:

button->Data = TValue::From(node);

You can remove that line, since the button's Data property is not being
used anymore.

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02