Recent

Author Topic: OnChange Event For TLabel ??  (Read 1509 times)

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
OnChange Event For TLabel ??
« on: November 12, 2020, 01:49:49 pm »
Hey Guys,

is there Any Way to Reproduce the On Change Event like the 1 TEdit has ?

Should Be like this :

Function WhenLabelXXChanged
do begin
   Code
end;

There is only OnChangeBounds But that is not what i was Looking For. :/

Handoko

  • Hero Member
  • *****
  • Posts: 5524
  • My goal: build my own game engine using Lazarus
Re: OnChange Event For TLabel ??
« Reply #1 on: November 12, 2020, 02:00:06 pm »
TLabel is not supposed to change. Why don't just use a TEdit? Any reason?

wp

  • Hero Member
  • *****
  • Posts: 13415
Re: OnChange Event For TLabel ??
« Reply #2 on: November 12, 2020, 02:27:00 pm »
Since the user cannot enter text in a label any change of the label caption occurs under control of your code where you can react.

If you - for whatever reason - nevertheless absolutely need an OnChange event you must write a specialized Label component. Every TLabel has a protected TextChanged method which you can override to fire an OnChange event handler:
Code: Pascal  [Select][+][-]
  1. type
  2.   TNewLabel = class(TLabel)
  3.     private
  4.       FOnChange: TNotifyEvent;
  5.     protected
  6.       procedure TextChanged; override;
  7.     public
  8.       property OnChange: TNotifyEvent read FOnChange write FOnChange;
  9.     end;  
  10.  
  11. procedure TNewLabel.TextChanged;
  12. begin
  13.   inherited;
  14.   if Assigned(FOnChange) then FOnChange(self);
  15. end;

Since this feature probably is needed only for a few labels I'd replace these labels on the fly by the new label.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormCreate(Sender: TObject);
  2. var
  3.   Lbl: TNewLabel;
  4. begin
  5.   Lbl := TNewLabel.Create(Self);
  6.   Lbl.Parent := Label1.Parent;
  7.   Lbl.CloneLabel(Label1);
  8.   Lbl.OnChange := @<your event handler for the OnChange event of the new label>
  9.   Label1.Free;    // Destroy the old label
  10.   Label1 := Lbl;  // and replace by the new one
  11. end;

The method CloneLabel saves some typing if there are several labels to be replaced:

Code: Pascal  [Select][+][-]
  1. procedure TNewLabel.CloneLabel(ALabel: TLabel);
  2. begin
  3.   Parent := ALabel.Parent;
  4.   SetBounds(ALabel.Left, ALabel.Top, ALabel.Width, ALabel.Height);
  5.   Caption := ALabel.Caption;
  6.   AutoSize := ALabel.AutoSize;
  7.   Font := ALabel.Font;
  8.   // plus any more properties that you change from default...
  9. end;  

See the attached project.
« Last Edit: November 12, 2020, 04:00:54 pm by wp »

Weitentaaal

  • Hero Member
  • *****
  • Posts: 554
Re: OnChange Event For TLabel ??
« Reply #3 on: November 12, 2020, 04:33:50 pm »
Thanks A Lot :)

I need it because as u said "User can't write into it"

I Have A ComboBox with produkts and if u Change Produkts then the Value of the displayed Labael changes

lucamar

  • Hero Member
  • *****
  • Posts: 4217
Re: OnChange Event For TLabel ??
« Reply #4 on: November 12, 2020, 05:57:07 pm »
I need it because as u said "User can't write into it"

I Have A ComboBox with produkts and if u Change Produkts then the Value of the displayed Labael changes

Even so, it must be your own code who changes the label, maybe something like:

Code: Pascal  [Select][+][-]
  1. SomeLabel.Caption := ComboBox.Items[ComboBox.ItemIndex]

You can, then, add a proc/function which you can call for example as:

Code: Pascal  [Select][+][-]
  1. ...
  2. SomeLabel.Caption := ComboBox.Items[ComboBox.ItemIndex]
  3. ALabelChanged(SomeLabel);
  4. ...

There is no need to sub-class TLabel. Unless you're doing something else, of course ...
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

 

TinyPortal © 2005-2018