Recent

Author Topic: How to change TLabel Caption to default value ?  (Read 2695 times)

yahoo000

  • New Member
  • *
  • Posts: 20
How to change TLabel Caption to default value ?
« on: October 12, 2019, 03:56:05 pm »
Hey, is it possible to change label.caption to default value ? it is saved somewhere in resources ?  default value caption is let say "label1" .. after program execution I'm changing label1.caption to 'label2' .. and how to revert back to default value ? I do not want to specify again default value in code/procedure. hope you understand me, thanks for help.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: How to change TLabel Caption to default value ?
« Reply #1 on: October 12, 2019, 04:40:34 pm »
Like other controls, a TLabel has the flag csSetCaption included in its ControlStyle property.
This means its "default" Caption becomes its name when a label is dropped on a form.
A class helper can add a method to force a label's Caption to revert to its Name whenever you call the helper method.
For instance, drop a label and a button on the main form of a new project, and try this:
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, StdCtrls;
  9.  
  10. type
  11.  
  12.   TLabelHelper = class helper for TLabel
  13.   public
  14.     procedure CaptionToName;
  15.   end;
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     Label1: TLabel;
  20.     procedure Button1Click(Sender: TObject);
  21.     procedure FormCreate(Sender: TObject);
  22.   end;
  23.  
  24. var
  25.   Form1: TForm1;
  26.  
  27. implementation
  28.  
  29. {$R *.lfm}
  30.  
  31. procedure TForm1.Button1Click(Sender: TObject);
  32. begin
  33.   Label1.CaptionToName;
  34. end;
  35.  
  36. procedure TForm1.FormCreate(Sender: TObject);
  37. begin
  38.   Label1.Caption := 'Not your default Caption';
  39. end;
  40.  
  41. procedure TLabelHelper.CaptionToName;
  42. begin
  43.   Caption := Name;
  44. end;
  45.  
  46. end.

yahoo000

  • New Member
  • *
  • Posts: 20
Re: How to change TLabel Caption to default value ?
« Reply #2 on: October 12, 2019, 04:52:25 pm »
using this code, I revert back to "caption" its name, not default caption

"caption" is just "name" of label, not default "caption"

thanks
« Last Edit: October 12, 2019, 04:54:51 pm by yahoo000 »

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: How to change TLabel Caption to default value ?
« Reply #3 on: October 12, 2019, 05:05:28 pm »
As far as I know, no you cannot revert it back to the value you gave on the design time. Unless you write/extent the component or manually store the value to a variable in the OnCreate event.

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to change TLabel Caption to default value ?
« Reply #4 on: October 12, 2019, 07:30:06 pm »
As far as I know, no you cannot revert it back to the value you gave on the design time. Unless you write/extent the component or manually store the value to a variable in the OnCreate event.
Since TLabel.Caption has a stored modifier you can read back the design-time value through RTTI. And that will be your own design-time value if you changed the default instance Tlabel.Caption name.
It should not be too difficult to turn that into a function for all captions. This works in  Delphi and maybe!!! in rtti from trunk, which is still experimental.
« Last Edit: October 12, 2019, 08:20:13 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: How to change TLabel Caption to default value ?
« Reply #5 on: October 12, 2019, 09:18:00 pm »
Maybe he is talking about the component name that is used at the OI before overwriting the caption...

for example to restore the caption to its original default name..


Label1.Caption := Label1.Name;

That will put the caption to 'Label1'
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 14382
  • Sensorship about opinions does not belong here.
Re: How to change TLabel Caption to default value ?
« Reply #6 on: October 12, 2019, 09:33:58 pm »
Yes, that was already pointed out, but that does not have to be the case if the design-time value is changed *at design time*.
My understanding is that that is what he means, see his replies above.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Handoko

  • Hero Member
  • *****
  • Posts: 5158
  • My goal: build my own game engine using Lazarus
Re: How to change TLabel Caption to default value ?
« Reply #7 on: October 14, 2019, 06:16:17 pm »
I haven't tried it but I found this good explanation for RTTI:
http://www.blong.com/Conferences/BorConUK98/DelphiRTTI/CB140.htm

It seems to be a bit too technical to me. So I think it will be much easier for TS to use a variable to store the value for this purpose.

Thanks Thaddy for mentioning RTTI. I almost forget it.

 

TinyPortal © 2005-2018