• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 920
  • Last Modified:

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');
0
sheepfarmer
Asked:
sheepfarmer
  • 3
  • 3
1 Solution
 
dprochownikCommented:
procedure setLabel(componentName: String, value: String);
var
  myHandle: TWinControl;
begin
  myHandle := FormOwner.FindComponent(componentName);
  if assigned(myHandle) then
    TLabel(myHandle).caption:= value;
end;

Where FormOwner is a form on which Label is placed
0
 
sheepfarmerAuthor Commented:
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);

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

SF
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
dprochownikCommented:
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

0
 
MerijnBSr. Software EngineerCommented:
Be aware that even though this works, it's not really fast.
0
 
dprochownikCommented:
No it isn't, but this is implication of enumarating components by name.
It just can't be much effective.
0
 
sheepfarmerAuthor Commented:
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
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now