Link to home
Start Free TrialLog in
Avatar of sheepfarmer
sheepfarmer

asked on

Accessing a component by variable name

How do you get a handle to a component by name:

e.g.
Three labels, label1, label2, label3

Simplied, I want to write a procedure to set the label caption, give the name of the label, thus

procedure setLabel(componentName: String, value: String);
var
  myHandle: <some sort of handle>
begin
  myHandle := <some handle getting function>(componentName);
  myHandle.caption:= value;
end;

Calling:
setLabel('label1', 'my caption for label1');
ASKER CERTIFIED SOLUTION
Avatar of dprochownik
dprochownik
Flag of Poland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sheepfarmer
sheepfarmer

ASKER

Could you go one step further and softcode the FormOwner so that a generic function could be write, thus:

setLabel (formName: String, componentName: String; value: String);

?
I had to change the TWinControl to TComponent, but other than that, thanks for a great solution.

SF
Below you have two variants of this procedure. One sets Label value only on first instance of form named like formName parameter. The second one sets label value on all form named like formName parameter.

PS: these functions are case insensitive because CompareStr function is case insensitive. If you wish to make them case sensitive on formName then replace:
if CompareStr(Screen.Forms[vi].Name,formName)=0 then
by
if Screen.Forms[vi].Name = formName then
 




//Set label caption only on the first Form named like formName parameter
procedure setLabel (formName: String; componentName: String; value: String);
var
  vForm: TComponent;
  vLabel: TComponent;
  vi : Integer;
begin
  vForm := nil;
  vi := Screen.FormCount-1;
  for vi := vi downto 0 do
  begin
    if CompareStr(Screen.Forms[vi].Name,formName)=0 then
    begin
      vForm := Screen.Forms[vi];
      break;
    end;
  end;
  if not assigned(vForm) then exit;
 
  vLabel := TCustomForm(vForm).FindComponent(componentName);
  if assigned(vLabel)and
     (vLabel is TCustomLabel) then
    TCustomLabel(vLabel).caption:= value;
end;
 
//Set label caption on all forms named like formName parameter
procedure setLabel (formName: String; componentName: String; value: String);
var
  vForm: TComponent;
  vLabel: TComponent;
  vi : Integer;
begin
  vForm := nil;
  vi := Screen.FormCount-1;
  for vi := vi downto 0 do
  begin
    if CompareStr(Screen.Forms[vi].Name,formName)=0 then
    begin
      vForm := Screen.Forms[vi];
      vLabel := TCustomForm(vForm).FindComponent(componentName);
      if assigned(vLabel)and
         (vLabel is TCustomLabel) then
        TCustomLabel(vLabel).caption:= value;
    end;
  end;
end;

Open in new window

Avatar of MerijnB
Be aware that even though this works, it's not really fast.
No it isn't, but this is implication of enumarating components by name.
It just can't be much effective.
Thanks for the followup - I think I can manage with specifying the form name in the componenet lookup - especially as the other method is (expectidly) slow.

Cheers
SF