Forum > LCL

using TTreeNode.Data to store and retrieve integer

(1/1)

MarcusM:
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: ---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.

--- End code ---

theo:

--- Quote from: MarcusM on September 26, 2010, 09:21:18 pm ---I am only casting the variable "i" to a string in order to display the results in the textbox.

--- End quote ---

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:
Ha! Thanks for the reply!

Chalk one up to being a newb.

Navigation

[0] Message Index

Go to full version