Recent

Author Topic: [SOLVED]Tlabel Caption  (Read 1286 times)

vdubeau

  • New Member
  • *
  • Posts: 27
[SOLVED]Tlabel Caption
« on: April 24, 2023, 09:00:30 pm »
I'm just learning Lazarus and Free Pascal. Sorry for asking too many questions.

I'm testing the components one at a time. I put just one Label on a form. If I change the caption in the inspector the program compiles fine but if If I try to change it in code. I get the following error: unit1.pas(29,3) Error: Identifier not found "Label1". According to the documentation Caption is a valid property of a Label. Am I doing something wrong. Program code follows.

    unit Unit1;

{$mode objfpc}{$H+}

interface

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

type

  { TForm1 }

  TForm1 = class(TForm)
    Label1: TLabel;
  private

  public

  end;

var
  Form1: TForm1;

implementation

{$R *.lfm}
BEGIN
  Label1.caption:= 'Testing';
end.


Thanks.
« Last Edit: April 29, 2023, 04:14:40 pm by vdubeau »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Tlabel Caption
« Reply #1 on: April 24, 2023, 09:08:42 pm »
You can't use BEGIN..END here. Lazarus apps does not work like that. They rather run in loop and wait for events.
Try to implement for example OnClick event of the form and change caption there.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

vdubeau

  • New Member
  • *
  • Posts: 27
Re: Tlabel Caption
« Reply #2 on: April 24, 2023, 10:22:51 pm »
Blazon,

Thanks for the quick reply. It helped. I did add a default FormCreate event to to form which worked perfectly. My ultimate goal is to create a form that changes the label to the current month and year. If I ran it today the label should become "April 2023". I have already figured out how to deal with dates in Free Pascal.

Thanks again.

Bart

  • Hero Member
  • *****
  • Posts: 5648
    • Bart en Mariska's Webstek
Re: Tlabel Caption
« Reply #3 on: April 24, 2023, 10:23:27 pm »
You can't use BEGIN..END here.

That's not true.
It's perfectly fine to use begin .. end there.
The code there would run when initializing the unit.
(It would have the same effect as initialization .. end
Label1 is not a valid identifier, because Label1 only exists in the context of the TForm1 class.
Form1.Label1.Caption := 'Something'; should compile just fine.
It would instantly crash though, because at te point in time that this would be executed (at unit initialization), Form1 has not been created yet (it would be either nil, or just an uninitialized pointer).

But, for the intent and purpose that vdubeau seems to have: better use the OnCreate of TForm1.
For that: in the for designe doublle click on an empty space of the form.
The IDE will create the OnCreate (named: TForm1.FormCreate(Sender: TObject);)  for you.
Inside that event handler, you can the put the original code you wanted to execute.

Bart
« Last Edit: April 24, 2023, 10:26:22 pm by Bart »

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Tlabel Caption
« Reply #4 on: April 25, 2023, 12:44:02 am »
Thanks, Bart. I probably never saw begin..end in units. Only initialization.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 5648
    • Bart en Mariska's Webstek
Re: Tlabel Caption
« Reply #5 on: April 25, 2023, 10:10:52 am »
Thanks, Bart. I probably never saw begin..end in units. Only initialization.

It's old style.
Now we have initialization and finalization.
Begin .. end was the way to initialize a unit.
Finalization did not exist back then.
If you needed something like that you needed to hook into ExitProc IIRC.

Oh, the old days (started with TP 3.0 on DOS way back).

Bart

 

TinyPortal © 2005-2018