Re: using value of a string as reference to Delphi component


[ Delphi Forum ] [ Delphi Tutorials -- by DelphiLand ]

Posted by webmaster Guido on June 20, 2003
In Reply to: using the value of a string as a reference to a component posted by p12334 Alex diNorcia on June 19, 2003

: i'm not sure if this can be done, but i'd like to read in a string, and then reference a component from it.

: please let me know if this is doable (and an example of it working).

: here is a simple example :
: var
: ident_txt: string;
: begin
: form1_unit.Form1.Show;
: ident_txt := 'form1_unit.Form1.Label1.Caption';

: // you can do
: form1_unit.Form1.Label1.Caption := 'hello world';

: // you CANT do
: //(ident_txt) := 'hello world';
: // or
: //&&ident_txt := 'hello world';
: // or something like that

: end;
--------------------

In Delphi, you can not *directly* reference a component from a string, but there is a way around, although with some limitations.

Given the component's name, you can search through all the components of a form, and if a component with that name is found, put a reference to that component in a temporary variable, say TempComp. If TempComp is of the desired TYPE, you can change properties of TempComp. So, you have to decide if you're going to modify a TLabel with a certain name, or a TEdit, or another type of component.

An example: a Delphi form contains several TLabel components, named Label1, Label2, and so on... We want a procedure to change the caption of a certain label by specifying the label's name, like this:

ChangeLabelCap('Label2', 'Caption of Label2');

This procedure will do the job, and additionally give a warning if the label is not found:

procedure TForm1.ChangeLabelCap(CompName, CompCaption: string);
var
  Found: Boolean;
  i: integer;
  TempComp: TComponent;
begin
  Found := False;
  for i := 0 to ComponentCount - 1 do begin
    if LowerCase(Components[i].Name) = LowerCase(CompName) then begin
      TempComp := Components[i];
      Found := TempComp is TLabel;
    end;
    if Found then break;
  end;
  if Found then
    // TYPECAST TempComp as a TLabel before using it
    (TempComp as TLabel).Caption := CompCaption 
  else
    ShowMessage('No label found with the name ' + CompName);
end;
With this code as a basis, you can easily write other procedures, like for example: change the color of a named TPanel. You could also expand the procedure above for *several* types of components, and change a string-property if a suitable component with the given name is found. Some ideas: change the caption of a label, OR the caption of a button, OR the text of an edit, and more... Here's an example:
procedure TForm1.ChangeCompString(CompName, S: string);
var
  Found: Boolean;
  i: integer;
  TempComp: TComponent;
begin
  Found := False;
  for i := 0 to ComponentCount - 1 do begin
    if LowerCase(Components[i].Name) = LowerCase(CompName) then begin
      TempComp := Components[i];
      Found := (TempComp is TLabel)
        or (TempComp is TButton) or (TempComp is TEdit);
    end;
    if Found then break;
  end;
  if not Found then
    ShowMessage('Not found of correct type: ' + CompName)
  else if TempComp is TLabel then
    (TempComp as TLabel).Caption := S
  else if TempComp is TButton then
    (TempComp as TButton).Caption := S
  else if TempComp is TEdit then
    (TempComp as TEdit).Text := S;
end;

Related Articles and Replies:


[ Delphi Forum ]
[ DelphiLand: free Delphi source code, tips, tutorials ]