Forum > LCL

I can't access to TreeView if the form container is created by code

(1/1)

BlueIcaro:
Hello, I have two forms. The second form, has a TTreeView with a few items. And a Public procedure to clear the items (called clearAll).

SomeThing like this:


--- Quote ---TreeView1.Items.Clear
--- End quote ---

The main form, has a button, to call "ClearAll".

The program compiles ok, and runs ok. But If I remove the second form, from automatic created. And I create the form, like this:


--- Quote --- FormTreeView:= TMyForm.Create(frMain);
 FormTreeView.Show;     
--- End quote ---

In the create event of the main form.

I have FormTreeView declarated on public secction of MainForm.

When the programs execute: TreeView1.Items.Clear gives me a exception.

What's wrong?

I have Lazarus 0.9.29 SNV:27267

Thanks

/BlueIcaro

JuhaManninen:
Where and when do you create FormTreeView?
It may not exist when you call TreeView1.Items.Clear.
Hard to say more without seeing the code.

Juha

BlueIcaro:
Hello, this is the code od unit1:

--- Quote ---unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls,
  unit2;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;
  F : TForm2;

implementation

{$R *.lfm}

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  f := TForm2.Create(Form1);
  F.Show;


end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  With Form2 do
  Begin
    TreeView1.Items.Clear;
  end;
end;

end.


--- End quote ---

Lazarus shows a exception on TreeView1.Items.Clear

/BlueIcaro

BlueIcaro:
Well I found the problem by my self:
I wrote

--- Quote ---procedure TForm1.Button1Click(Sender: TObject);
begin
  With Form2 do
  Begin
    TreeView1.Items.Clear;
  end;
end;


--- End quote ---

But I shold be

--- Quote ---procedure TForm1.Button1Click(Sender: TObject);
begin
  With F do //Because I created the form like this: f := TForm2.Create (Form1);
  Begin
    TreeView1.Items.Clear;
  end;
end;

--- End quote ---

Thanks JuhaManninen  for you answer.

/BlueIcaro

Navigation

[0] Message Index

Go to full version