Recent

Author Topic: I can't access to TreeView if the form container is created by code  (Read 4808 times)

BlueIcaro

  • Hero Member
  • *****
  • Posts: 818
    • Blog personal
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

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;     

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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4570
  • I like bugs.
Re: I can't access to TreeView if the form container is created by code
« Reply #1 on: September 08, 2010, 02:38:21 pm »
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
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

BlueIcaro

  • Hero Member
  • *****
  • Posts: 818
    • Blog personal
Re: I can't access to TreeView if the form container is created by code
« Reply #2 on: September 08, 2010, 10:20:13 pm »
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.


Lazarus shows a exception on TreeView1.Items.Clear

/BlueIcaro

BlueIcaro

  • Hero Member
  • *****
  • Posts: 818
    • Blog personal
Re: I can't access to TreeView if the form container is created by code
« Reply #3 on: September 08, 2010, 10:26:06 pm »
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;


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;

Thanks JuhaManninen  for you answer.

/BlueIcaro

 

TinyPortal © 2005-2018