1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram

  5. Delphifan Magazine
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Convert a string into a componentname

Discussion in 'Delphi Programming' started by aikona, Feb 7, 2014.

  1. aikona
    Offline

    aikona DF Member

    Hi

    I have about 20 labels and need to change the caption according to a formula .

    I can write the code ( 20 lines) for each of the 20 edits 'onchange' event but would prefer one code for all.( 10 -15 line of code instead of 400 lines of code

    let me explain :

    if change in edit1 then do code and put result in label1.caption

    Now ... with the code

     procedure TMain1.SpinEditR_01Change(Sender: TObject);
    var
    tempcom : Tcomponent ;
     test :string ;
    Begin
    with  sender as TJvSpinEdit do
        Begin
        test := copy(name,11,2 );
        test := 'Label' + test ;
    // HERE IN NEED TO MAKE THE STRING TEST INTO COMPONENTNAME
     //     Label011.caption:= floattostr(Value*100/2000)+' %'  ;
         (tempcomp as Tlabel).Caption:= floattostr(Value*100/2000)+' %'   ;


    Thanks
     
  2. wesleytorres
    Offline

    wesleytorres DF Member

     
  3. nemilose
    Offline

    nemilose DF Member

    Hi,

    You may use
    Code:
    TLabel(FindComponent(test).Caption:=FloatToStr(Value*100/2000)+' %' 
    as wesleytorres suggested, but you might consider creating arrays of TEdit and TLabel components at runtime. In that case you do not need to use FindComponent. Below is an example. The code creates 10 TEdits one below the other (you may also store coordinates in some array, is components' positions can not be described with some formula), and 10 TLabels besides TEdits. OnChange procedure is also assigned for TEdits.
    I used static arrays, but you may use dynamic arrays as well.


    Code:
    unit Unit1;
    
    interface
    
    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
    
    type
      TForm1 = class(TForm)
        procedure EditChange(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    var
      Edits : Array [1..10] of TEdit;
      Labels : Array [1..10] of TLabel;
    
    {$R *.dfm}
    
    procedure TForm1.FormCreate(Sender: TObject);
    var
      i : Integer;
    begin
       for i:=1 to 10 do
       begin
         Edits:=TEdit.Create(Self);
         Edits.Name:='Edit'+IntToStr(i);
         Edits.Parent:=Form1;
         Edits.Left:=10;
         Edits.Top:=10 + (i-1)*(Edits.Height+5);
         Edits.Width:=150;
         Edits.OnChange:=EditChange;
    
         Labels:=TLabel.Create(Self);
         Labels.Name:='Label'+IntToStr(i);
         Labels.Parent:=Form1;
         Labels.Left:=Edits.Left + Edits.Width + 10;
         Labels.Top:=Edits.Top + (Edits.Height - Labels.Height) div 2;
         Labels.Caption:='Label no. '+IntToStr(i);
       end;
    end;
    
    procedure TForm1.EditChange(Sender: TObject);
    var
      i : Integer;
      s : String;
    begin
      s:=(Sender as TEdit).Name;
      Delete(s,1,4);
      i:=StrToInt(s);
      Labels[i].Caption:='Edit'+s+' is changed';
      //You may also use
      //TLabel(FindComponent('Label' + s)).Caption:='Edit'+s+' is changed';
    end;
    
    
    end.
    [/i]
     
  4. aikona
    Offline

    aikona DF Member

    Thanks so far

    I am using the following and works

     with  sender as TJvSpinEdit do
      Begin
        test := copy(name,11,3 );// get last 3 char
        test := 'Labelname_' + test ;// add to label
        value1 := value ;
      end ;

     with TLabel(FindComponent(test)) do
      begin
         caption := 'OK'   ;
         if Value1 < valueG  then   Caption := 'Abnormal Condition detected'          ;

     
      end;
    end ;
     
  5. Prof_Ho2
    Offline

    Prof_Ho2 DF Member

    Good example
     
  6. drakeblood2
    Online

    drakeblood2 Guest

    thank

     
     
  7. ilanamlas1
    Online

    ilanamlas1 Guest

  8. ASAPLTDA
    Offline

    ASAPLTDA DF Junior

    GRACIAS
     
  9. laolao876
    Offline

    laolao876 DF Member

    mark floor4,good example
     

Share This Page