Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
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
    Offline

    drakeblood2 Guest

    thank

     
     
  7. ilanamlas1
    Offline

    ilanamlas1 Guest

  8. ASAPLTDA
    Offline

    ASAPLTDA DF Junior

    GRACIAS
     
  9. laolao876
    Offline

    laolao876 DF Member

    mark floor4,good example
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block