Find a component by name

1

I have a String:

var
  vMinhastring : string;
begin
  vMinhastring := 'Edit1';

In my form I have a Tedit Compound with the name Edit1. How do I pass some value to my Edit1 by using the aVMinhastring as Component name?

  vMinhastring.text := 'batatinha';

I'm trying to do this, but I'm having problems:

contElem := 1;
Rdb1:= TRadioGroup.Create(TabSheet1);
Rdb1.Parent:= TabSheet1;
Rdb1.OnExit := Validacao;
Rdb1.Name:='Rdb'+IntToStr(contElem);
Rdb1.Items.Add('C');

Edit := TMaskEdit.Create(TabSheet1);
Edit.Parent:= TabSheet1;
Edit.Name:='Edit'+IntToStr(contElem);
Edit.Clear;
Edit.EditMask := ('!99;1;');

contElem := 2;
Rdb1:= TRadioGroup.Create(TabSheet1);
Rdb1.Parent:= TabSheet1;
Rdb1.OnExit := Validacao;
Rdb1.Name:='Rdb'+IntToStr(contElem);
Rdb1.Items.Add('C');

Edit := TMaskEdit.Create(TabSheet1);
Edit.Parent:= TabSheet1;
Edit.Name:='Edit'+IntToStr(contElem);
Edit.Clear;
Edit.EditMask := ('!99;1;');

I created a procedure:

procedure TFCad_AnaliseDeTendencias.Validacao(Sender: TObject);
var
 name, name2 :string;
 i : integer;
begin
   name := TRadioGroup(Sender).Name;
   name2 := '';
   for i := 1 to Length(name) do
   begin
      if name[i] in ['0'..'9'] then
         name2 := name2 + name[i];
   end;
   name2 := 'Edit'+name2;
   TMaskEdit(FindComponent(name2)).Text := '01'; //Esta dando erro aqui.
end;

You are giving

error
  

Access violation at address

    
asked by anonymous 16.02.2017 / 16:34

3 answers

1

Well, I put together a prototype with RTTI that looks better. Here is the code:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ExtCtrls, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Edit2: TEdit;
    Button1: TButton;
    Panel1: TPanel;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    function CriarComponente(AClassType: TClass; AName: String; AParent: TWinControl): TComponent;
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses
  Rtti;

{$R *.dfm}

{ TForm1 }



procedure TForm1.Button1Click(Sender: TObject);
begin

  CriarComponente(GetClass(Edit1.Text), Edit2.Text, Panel1);

end;



function TForm1.CriarComponente(AClassType: TClass; AName: String; AParent: TWinControl): TComponent;
var
  RttiContext: TRttiContext;
  RttiInstanceType: TRttiInstanceType;
  Value: TValue;
begin

  RttiInstanceType                       := (RttiContext.GetType(AClassType) as TRttiInstanceType);
  Value                                  := RttiInstanceType.GetMethod('Create').Invoke(RttiInstanceType.metaClassType, [self]);
  (Value.AsObject as TWinControl).Parent := AParent;

end;



procedure TForm1.FormCreate(Sender: TObject);
begin
  RegisterClasses([TButton, TForm, TEdit]);
end;

end.

In the program screen, I added 2 edits where you will enter the Type of component and its name, a button to execute, and a panel to be the parent of the components.

Note: It is necessary to register the classes you can create, eg: RegisterClasses([TButton, TForm, TEdit]);

    
16.02.2017 / 21:21
0

You can use the System.Classes.TComponent.FindComponent function:

TEdit(FindComponent(vMinhastring)).Text := 'Meu texto';
  

FindComponent

     

Original: FindComponent returns the component in the Components property array with the name that matches the string in the AName parameter.

     

FindComponent returns the component in the Component array property with the name that matches the string in the AName parameter.

    
17.02.2017 / 12:15
0

See this:

procedure TForm13.Button1Click(Sender: TObject);
Var
   tabSheet: TTabSheet;
   AComponent: TComponent;
   aIndex: Integer;
begin
   aIndex:=-1;

   AComponent := FindComponent('TabSheet1');
   if Assigned(AComponent) then
     if AComponent is TTabSheet then
       aIndex := TTabSheet(AComponent).PageIndex; //get the index of the 'TabSheet1'  

   tabSheet := TTabSheet.Create(PageControl1);
   tabSheet.PageControl := PageControl1;
   tabSheet.Caption := 'My TabSheet'+IntToStr(PageControl1.PageCount);
   if aIndex>-1 then
     tabSheet.PageIndex := aIndex; //Set the index of the new TabSheet
end;
    
22.02.2017 / 13:24