Recent

Author Topic: using TTreeNode.Data to store and retrieve integer  (Read 6804 times)

MarcusM

  • New Member
  • *
  • Posts: 12
using TTreeNode.Data to store and retrieve integer
« on: September 26, 2010, 09:21:18 pm »
Hi all - I am pleased to have found Lazarus for GUI programming on Ubuntu!

I cannot figure out how to store and retrieve an integer value in a TTreeNode's Data property. Here is a simple sample program to demonstrate my efforts. Please note, I am only casting the variable "i" to a string in order to display the results in the textbox. However, an exception is thrown and the message is "Access Violation."

Your help will be appreciated. Thanks.


Code: [Select]
unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, ComCtrls, StdCtrls, SysUtils, FileUtil, LResources, Forms, Controls,
  Graphics, Dialogs;

type
  { TForm1 }
  TForm1 = class(TForm)
    btnAdd: TButton;
    btnDisplay: TButton;
    txtDisplay: TEdit;
    tree: TTreeView;
    procedure btnAddClick(Sender: TObject);
    procedure btnDisplayClick(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

{ TForm1 }

procedure TForm1.btnAddClick(Sender: TObject);
var
  node: TTreeNode;
begin
  node := tree.Items.Add(nil, 'hello world');
  node.Data := pointer(1);
end;

procedure TForm1.btnDisplayClick(Sender: TObject);
var
  i: integer;
  str: string;
begin
  if (tree.Selected = nil) then begin
    ShowMessage('Please select ''hello world'' item and click again');
    exit
  end;

  try
    i := integer(tree.Selected.Data);
    str := string(i);
    txtDisplay.Text := str;
  except on E: Exception do begin
    ShowMessage(E.Message);
    end;
  end;
end;

initialization
  {$I unit1.lrs}

end.

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927
Re: using TTreeNode.Data to store and retrieve integer
« Reply #1 on: September 26, 2010, 10:00:39 pm »
I am only casting the variable "i" to a string in order to display the results in the textbox.

It's not possible to cast an integer to a string. These are completely different types.
You can convert the integer:
str:=IntToStr(i);

MarcusM

  • New Member
  • *
  • Posts: 12
Re: using TTreeNode.Data to store and retrieve integer
« Reply #2 on: September 26, 2010, 11:05:41 pm »
Ha! Thanks for the reply!

Chalk one up to being a newb.

 

TinyPortal © 2005-2018