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');
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I had to change the TWinControl to TComponent, but other than that, thanks for a great solution.
SF
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,for mName)=0 then
by
if Screen.Forms[vi].Name = formName then
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
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;
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.
It just can't be much effective.
ASKER
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
Cheers
SF
ASKER
setLabel (formName: String, componentName: String; value: String);
?