We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Accessing a component by variable name

sheepfarmer
sheepfarmer asked
on
Medium Priority
930 Views
Last Modified: 2013-11-23
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');
Comment
Watch Question

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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

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);

?

Author

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

MerijnBSr. Software Engineer
CERTIFIED EXPERT

Commented:
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.

Author

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.