Board index » delphi » Difficulty referencing form's fields

Difficulty referencing form's fields

Hi,
I have a project that loads a form containing a List box, which it
then attempts to populate. The code is simplified as follows:

uses
[etc.].., SecondForm; {name of the .PAS file of the second form}

. . .

procedure TMainForm.menuLoadSecondClick(Sender:TObject);
var
CurrentForm: TForm;
...
begin
CurrentForm := TfrmSecond.Create(Self); {frmSecond is the name
property of the second form. I have removed the Create statements from
the DPR}
CurrentForm.Caption := 'Another Form';
CurrentForm.lstHeading.Items.Add('First Item');
end;

The same code loads and uses a form called FirstForm perfectly.
(FirstForm is not of type TfrmSecond, it is a slightly different form
of type TfrmFirst. It also has a list box accessed in the identical
manner, and successfully.) However, in this case, the compiler stops
at the line
CurrentForm.lstHeading.Items.Add('First Item');
saying 'field identifier expected ' - i.e. it is not recognising the
list box lstHeading which is on that second form. In fact, all the
Objects on the form are similarly unrecognised, but the form itself &
its properties are accessible (eg. the 'Caption' statement works
fine.)
All the proper modules are loaded in the appropriate uses clauses -
and all the obvious syntax and other errors have been checked for.
Anyone got any ideas?
Thanks,
Rod
rsus...@iafrica.com.

 

Re:Difficulty referencing form's fields


In article <41uvr5$...@grovel.iafrica.com>, rsus...@iafrica.com (Rod

Quote
Suskin) wrote:
> procedure TMainForm.menuLoadSecondClick(Sender:TObject);
> var
> CurrentForm: TForm;
> ...
> begin
> CurrentForm := TfrmSecond.Create(Self); {frmSecond is the name
> property of the second form. I have removed the Create statements from
> the DPR}
> CurrentForm.Caption := 'Another Form';
> CurrentForm.lstHeading.Items.Add('First Item');
> end;

The reason I think this code is incorrect (I may be wrong) is the declaration:
    CurrentForm: TForm
Since you declared CurrentForm as TForm you cannot reference lstHeading
since that is not a valid field in a regular TForm.  Either you can do:
   TfrmSecond(CurrentForm).lstHeading.Items.Add('First Item');
or change the declaration:
   CurrentForm: TfrmSecond;

Tom

Re:Difficulty referencing form's fields


Quote
Rod Suskin (rsus...@iafrica.com) wrote:

: uses
: [etc.].., SecondForm; {name of the .PAS file of the second form}
: procedure TMainForm.menuLoadSecondClick(Sender:TObject);
: var
: CurrentForm: TForm;
: ...
: begin
: CurrentForm := TfrmSecond.Create(Self); {frmSecond is the name
: property of the second form. I have removed the Create statements from
: the DPR}
: CurrentForm.Caption := 'Another Form';
: CurrentForm.lstHeading.Items.Add('First Item');
: end;
:
: The same code loads and uses a form called FirstForm perfectly.
: (FirstForm is not of type TfrmSecond, it is a slightly different form
: of type TfrmFirst. It also has a list box accessed in the identical
: manner, and successfully.) However, in this case, the compiler stops
: at the line
: CurrentForm.lstHeading.Items.Add('First Item');
: saying 'field identifier expected ' - i.e. it is not recognising the
: list box lstHeading which is on that second form. In fact, all the
: Objects on the form are similarly unrecognised, but the form itself &
: its properties are accessible (eg. the 'Caption' statement works
: fine.)

The problem you are encountering is more than likely due to the fact
that the SecondForm you have is BASED on a TForm, but is MORE than
a TForm.

Declaring CurrentForm as a TForm basically tells Delphi "limit the
use of this object to that of a TForm", and a TForm has no
"lstHeading".

Two ways around the problem.  First, is to declare CurrentForm as
a TSecondForm.  This will give it instant access to the "lstHeading",
but you will only be able to put forms derived from >SecondForm<
into the CurrentForm (because CurrentForm: TSecondForm says that
anything you put into CurrentForm will be at LEAST a TSecondForm)

The second way, which will let you use other types of forms, is
using "is" or "as", like this:

  if CurrentForm is TSecondForm then
    (CurrentForm as TSecondForm).lstHeading.Items.Add('First item');

You can make your life easier with the WITH statement:

  if CurrentForm is TSecondForm then
    with CurrentForm as TSecondForm,lstHeading.Items do
    begin
      Add('First item');
      Add('Second item');
      {etc.}
    end;

Hope that helps!

  --=- Ritchie Annand
       New Product Development
       Malibu Engineering Ltd.

Re:Difficulty referencing form's fields


Thanks Ritchie, Thomas and Human for your help - everything is back on
track and I learned a bit along the way!
Rod Suskin.

Other Threads