Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2013-09

TTreenode.data

24 messages · 10 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Richard Mace

Hi,
Can anyone give me an example of adding and retrieving an integer value to a TTreenode via it's data property please?

I've tried the below, but it doesn't seem to work.

Set:
with
tvMain.Items.AddChildObject(tvMain.Selected,'string',TObject(fUserList.Items[I].Id)) do

and getting as, throws an exception on start:
procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode);
begin
  ShowMessage('Node ' + IntToStr(Integer(Node.Data)));
end;

Thanks in advance

Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130911/6be89c4f/attachment.html>

Reply by email

Antonio Fortuny
to Richard Mace

Le 11/09/2013 07:51, Richard Mace a ?crit :

Show quoted text 10 lines
Hi, Can anyone give me an example of adding and retrieving an integer value to a TTreenode via it's data property please? I've tried the below, but it doesn't seem to work. Set: with tvMain.Items.AddChildObject(tvMain.Selected,'string',TObject(fUserList.Items[I].Id)) do

change to:
with tvMain.Items.AddChildObject(tvMain.Selected,'string', *Pointer*(fUserList.Items[I].Id)) do

Show quoted text 6 lines
and getting as, throws an exception on start: procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); begin ShowMessage('Node ' + IntToStr(Integer(Node.Data)));
change to:
ShowMessage('Node ' + IntToStr(*PtrInt***(Node.Data)));

Antonio.

Show quoted text 12 lines
end; Thanks in advance Richard -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130911/2acb448f/attachment.html>

Reply by email

Howard Page-Clark
to Antonio Fortuny
Antonio Fortuny wrote 6 lines
Le 11/09/2013 07:51, Richard Mace a ?crit : > procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); > begin > ShowMessage('Node ' + IntToStr(Integer(Node.Data))); change to: ShowMessage('Node ' + IntToStr(*PtrInt***(Node.Data)));

Also unless you want the treeview to behave as if it were fully initialised with Data integer values of zero, you would need to add:

procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode);
begin
   if Assigned(Node) then
     ShowMessage('Node ' + IntToStr(PtrInt(Node.Data)));

Reply by email

Graeme Geldenhuys
to Richard Mace: Can anyone give me an example of adding and retrieving an integer value to a TTreenode…
Richard Mace wrote
Can anyone give me an example of adding and retrieving an integer value to a TTreenode via it's data property please?

The Data property holds a pointer value, so don't assign an Integer directly. Rather define a Record structure (or a Class) that holds the data you want (in your case an Integer field), then assign the pointer to the record structure to the Treenode.Data property.

There is an example in the LCL documentation:

http://lazarus-ccr.sourceforge.net/docs/lcl/comctrls/ttreenode.data.html

Regards,
Graeme

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/

Reply by email

Richard Mace
to Graeme Geldenhuys

Thanks for the reply.
Can't it be type casted because both a pointer and integer are 4 bytes?

"Graeme Geldenhuys" wrote 29 lines
On 2013-09-11 06:51, Richard Mace wrote: > Can anyone give me an example of adding and retrieving an integer value to > a TTreenode via it's data property please? The Data property holds a pointer value, so don't assign an Integer directly. Rather define a Record structure (or a Class) that holds the data you want (in your case an Integer field), then assign the pointer to the record structure to the Treenode.Data property. There is an example in the LCL documentation: http://lazarus-ccr.sourceforge.net/docs/lcl/comctrls/ttreenode.data.html Regards, Graeme -- fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/ -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130911/96f2289b/attachment.html>

Reply by email

Hans-Peter Diettrich
to Richard Mace: Thanks for the reply. Can't it be type casted because both a pointer and integer are 4…
Thanks for the reply. Can't it be type casted because both a pointer and integer are 4 bytes?

Dunno about Lazarus, but casts work in Delphi.

Hint: pointers may be longer than integers, but never shorter. So you won't loose information when e.g. storing an 32 bit integer into a 64 bit pointer field. Consequently the opposite direction, i.e. storing an pointer in an integer field, may not work.

That's why newer Delphi versions also have the Tag properties expanded, so that it's possible to store pointers there.

DoDi

Reply by email

Hans-Peter Diettrich
to Antonio Fortuny: Le 11/09/2013 07:51, Richard Mace a ?crit : change to: with…
Show quoted text 15 lines
Le 11/09/2013 07:51, Richard Mace a ?crit : > Hi, > Can anyone give me an example of adding and retrieving an integer > value to a TTreenode via it's data property please? > > I've tried the below, but it doesn't seem to work. > > Set: > with > tvMain.Items.AddChildObject(tvMain.Selected,'string',TObject(fUserList.Items[I].Id)) > do change to: with tvMain.Items.AddChildObject(tvMain.Selected,'string', *Pointer*(fUserList.Items[I].Id)) do

I'd store fUserList.Items[I] there, which is an address and allows access to further values later.

DoDi

Reply by email

Flávio Etrusco
to Howard Page-Clark: Also unless you want the treeview to behave as if it were fully initialised with Data…
Howard Page-Clark wrote 12 lines
Also unless you want the treeview to behave as if it were fully initialised with Data integer values of zero, you would need to add: procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); begin if Assigned(Node) then ShowMessage('Node ' + IntToStr(PtrInt(Node.Data)));

I guess you mean "if Assigned(Node.Data) then" ?

-Fl?vio

Reply by email

Howard Page-Clark
to Flávio Etrusco
Fl?vio Etrusco wrote 15 lines
On Wed, Sep 11, 2013 at 5:41 AM, Howard Page-Clark wrote: > > > Also unless you want the treeview to behave as if it were fully initialised > with Data integer values of zero, you would need to add: > > > > procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); > begin > if Assigned(Node) then > ShowMessage('Node ' + IntToStr(PtrInt(Node.Data))); > I guess you mean "if Assigned(Node.Data) then" ?

Indeed that is what I meant, not what I wrote!

Reply by email

Hans-Peter Diettrich
to Howard Page-Clark
Show quoted text 12 lines
On 11/09/2013 18:03, Fl?vio Etrusco wrote: > On Wed, Sep 11, 2013 at 5:41 AM, Howard Page-Clark >> procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); >> begin >> if Assigned(Node) then >> ShowMessage('Node ' + IntToStr(PtrInt(Node.Data))); >> > > I guess you mean "if Assigned(Node.Data) then" ? Indeed that is what I meant, not what I wrote!

In this case you can omit the "if" entirely.

DoDi

Reply by email

Flávio Etrusco
to Graeme Geldenhuys: The Data property holds a pointer value, so don't assign an Integer directly. Rather…
Graeme Geldenhuys wrote 16 lines
On 2013-09-11 06:51, Richard Mace wrote: > Can anyone give me an example of adding and retrieving an integer value to > a TTreenode via it's data property please? The Data property holds a pointer value, so don't assign an Integer directly. Rather define a Record structure (or a Class) that holds the data you want (in your case an Integer field), then assign the pointer to the record structure to the Treenode.Data property. There is an example in the LCL documentation: http://lazarus-ccr.sourceforge.net/docs/lcl/comctrls/ttreenode.data.html Regards, Graeme

I don't agree this is a rule of thumb. If you know what you're doing you should store whatever data fits in Data (pointer size) and avoid the hundreds of tiny annoying allocations...

Regards,
Fl?vio

Reply by email

Richard Mace
to Graeme Geldenhuys: The Data property holds a pointer value, so don't assign an Integer directly. Rather…
Graeme Geldenhuys wrote 13 lines
On 2013-09-11 06:51, Richard Mace wrote: > Can anyone give me an example of adding and retrieving an integer value to > a TTreenode via it's data property please? The Data property holds a pointer value, so don't assign an Integer directly. Rather define a Record structure (or a Class) that holds the data you want (in your case an Integer field), then assign the pointer to the record structure to the Treenode.Data property. There is an example in the LCL documentation: http://lazarus-ccr.sourceforge.net/docs/lcl/comctrls/ttreenode.data.html

OK, thanks for everyone's input. I have re-written it with the following, but ShowMessage still isn't display the correct User.Id For clarification fUserList is a TObjectList

with tvMain.Items.AddChild(tvMain.Selected,IntToStr(fUserList.Items[I].Id))
do
  begin
    Data := fUserList.Items[I];
    ImageIndex := 2;
  end;
procedure TfrmMain.tvMainSelectionChanged(Sender: TObject);
begin
  if Assigned(TTreeNode(Sender).Data) then
    ShowMessage('Node ' + IntToStr(TUser(TTreeNode(Sender).Data).Id));
end;

I'm obviously doing something a bit daft, but can't work out what? Any input would be greatly received.

Thanks

Richard
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130912/796637dd/attachment.html>

Reply by email

Mattias Gaertner
to Richard Mace
[...] procedure TfrmMain.tvMainSelectionChanged(Sender: TObject); begin

Sender is the tvMain.

Show quoted text 6 lines
if Assigned(TTreeNode(Sender).Data) then ShowMessage('Node ' + IntToStr(TUser(TTreeNode(Sender).Data).Id)); end; I'm obviously doing something a bit daft, but can't work out what? Any input would be greatly received.

Mattias

Reply by email

Richard Mace
to Antonio Fortuny: Le 11/09/2013 07:51, Richard Mace a ?crit : change to: with…

OK, I now have this code:
with
tvMain.Items.AddChildObject(tvMain.Selected,IntToStr(fUserList.Items[I].Id),Pointer(fUserList.Items[I].Id)) do

and this:
procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode);
begin
  if Assigned(Node.Data) then
    ShowMessage('Node ' + IntToStr(PtrInt(Node.Data)));
end;

And I am now getting an exception when my app run?

Richard

Antonio Fortuny wrote 30 lines
Le 11/09/2013 07:51, Richard Mace a ?crit : Hi, Can anyone give me an example of adding and retrieving an integer value to a TTreenode via it's data property please? I've tried the below, but it doesn't seem to work. Set: with tvMain.Items.AddChildObject(tvMain.Selected,'string',TObject(fUserList.Items[I].Id)) do change to: with tvMain.Items.AddChildObject(tvMain.Selected,'string', *Pointer*(fUserList.Items[I].Id)) do and getting as, throws an exception on start: procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); begin ShowMessage('Node ' + IntToStr(Integer(Node.Data))); change to: ShowMessage('Node ' + IntToStr(*PtrInt***(Node.Data))); Antonio.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130913/9a526e55/attachment.html>

Reply by email

Antonio Fortuny
to Richard Mace: OK, I now have this code: with tvMain.Items.AddChildObject(tvMain.Selected,IntToStr(fUserL…

Le 13/09/2013 07:48, Richard Mace a ?crit :

Show quoted text 16 lines
OK, I now have this code: with tvMain.Items.AddChildObject(tvMain.Selected,IntToStr(fUserList.Items[I].Id),Pointer(fUserList.Items[I].Id)) do and this: procedure TfrmMain.tvMainChange(Sender: TObject; Node: TTreeNode); begin if Assigned(Node.Data) then ShowMessage('Node ' + IntToStr(PtrInt(Node.Data))); end; And I am now getting an exception when my app run? Richard

Details are missing about your implementation: Lzarus & FPC, OS, etc. I've made a little test on Win32 and Linux_X64, Lazarus 1.0.12 I added some nodes to the TTreeView like this:

var
   SData: Integer=179;
procedure TFrmMain.BtnTestTvClick(Sender: TObject);
var
   wNode: TTreeNode = nil;
   s: String;
begin
   Inc(SData);
   wNode := Ttv.Items.AddChildObject(nil, 'node new', Pointer(SData));
   s := Format('node %s data:%d', [wNode.Text, PtrInt(wNode.Data)]);
   MessageDlg('Titre', s, mtInformation, [mbOK], 0, mbOK);
end;
The I added an event to Ttv.OnChange event like that:
procedure TFrmMain.TtvChange(Sender: TObject; Node: TTreeNode);
var
   wData: Integer;
begin
   if Assigned(Node) then begin
     wData := PtrInt(Node.Data);
     MessageDlg('Titre',Format('data for "%s" %d', [Node.Text, wData]),
mtInformation, [mbOK], 0, mbOK);
   end;
end;

I get a new node on every BtnTestTvClick and a value starting from 180 on every OnChange event
On both platforms.

Antonio.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130913/5f6d4771/attachment-0001.html>

Reply by email

Richard Mace
to Reinier Olislagers

It is, and yes I am. :-)

"Reinier Olislagers" wrote 17 lines
On 13/09/2013 09:37, Antonio Fortuny wrote: > Le 13/09/2013 07:48, Richard Mace a ?crit : >> OK, I now have this code: > Details are missing about your implementation: Lzarus & FPC, OS, etc. Guessing Windows x86 - not writing a FreeSwitch control application, by any chance [1] ;) http://thread.gmane.org/gmane.comp.telephony.freeswitch.user/64508 -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130914/f6072793/attachment.html>

Reply by email

Reinier Olislagers
to Richard Mace

Nice. Use FS a bit myself - nice to see Lazarus tooling for it appear.

Richard Mace wrote 14 lines
It is, and yes I am. :-) On 13 Sep 2013 16:01, "Reinier Olislagers" < <mailto:>> wrote: On 13/09/2013 09:37, Antonio Fortuny wrote: > Le 13/09/2013 07:48, Richard Mace a ?crit : >> OK, I now have this code: > Details are missing about your implementation: Lzarus & FPC, OS, etc. Guessing Windows x86 - not writing a FreeSwitch control application, by any chance [1] ;) http://thread.gmane.org/gmane.comp.telephony.freeswitch.user/64508

Reply by email

Richard Mace
to Antonio Fortuny: Le 13/09/2013 07:48, Richard Mace a ?crit : Details are missing about your…

Thanks Antonio, that worked fine.
Goodness knows what I was originally doing!

Richard

Show quoted text 37 lines
Details are missing about your implementation: Lzarus & FPC, OS, etc. I've made a little test on Win32 and Linux_X64, Lazarus 1.0.12 I added some nodes to the TTreeView like this: var SData: Integer=179; procedure TFrmMain.BtnTestTvClick(Sender: TObject); var wNode: TTreeNode = nil; s: String; begin Inc(SData); wNode := Ttv.Items.AddChildObject(nil, 'node new', Pointer(SData)); s := Format('node %s data:%d', [wNode.Text, PtrInt(wNode.Data)]); MessageDlg('Titre', s, mtInformation, [mbOK], 0, mbOK); end; The I added an event to Ttv.OnChange event like that: procedure TFrmMain.TtvChange(Sender: TObject; Node: TTreeNode); var wData: Integer; begin if Assigned(Node) then begin wData := PtrInt(Node.Data); MessageDlg('Titre',Format('data for "%s" %d', [Node.Text, wData]), mtInformation, [mbOK], 0, mbOK); end; end; I get a new node on every BtnTestTvClick and a value starting from 180 on every OnChange event On both platforms. Antonio.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus.freepascal.org/pipermail/lazarus/attachments/20130917/34e999ce/attachment.html>

Reply by email