PDA

View Full Version : How can i do this?



azrael11
13-02-2011, 08:20 AM
I have 10 buttons with name like Button1,Button2....

How can i make a simple procudere like

Procedure showbuttons(howmany: byte);
var
i: integer;
begin
for i := 0 to howmany do
button(i).visible := true; ???????
end;

Thanks.... I think is simply but i stack... My brain is stack...

paul_nicholls
13-02-2011, 08:57 AM
I think there is a TForm.FindControl() method that returns a control by name like so:


Var
Button: TButton;
begin
Button := FindControl('Button1') as TButton;

cheers,
Paul

azrael11
13-02-2011, 12:43 PM
I think there is a TForm.FindControl() method that returns a control by name like so:


Var
Button: TButton;
begin
Button := FindControl('Button1') as TButton;

cheers,
Paul

Thank you Paul for anwser but
"Find Control" wants a handle control not a string
FindControl(Handle: HWnd)

Can i use the findComponet function ?
But how i can get the control of the componet so i can make it visible?

Thanks again...

chronozphere
13-02-2011, 01:39 PM
Just use this:



component := findComponent('somestring');
if component is TControl then
TControl(component).Visible := True;


Hope that helps. :)

azrael11
13-02-2011, 04:52 PM
Just use this:



component := findComponent('somestring');
if component is TControl then
TControl(component).Visible := True;


Hope that helps. :)

When i run your code i get the error that findcomponet not exists.
I give a var that component is a Tcomponent and change the line in
component.findcomponent('button'+IntToStr(i));
It compiles run ok but no visible button hmmm why?

Thank you for the help....

Update
I made this and it works

for i := 0 to howmany do
begin
With TBitBtn(FindComponent('BitBtn'+inttostr(i))) do
Visible := true;
end;

But it works only in In the main form if take the code and put it in an empty unit not working
Any thoughts....

azrael11
13-02-2011, 06:01 PM
Ok i solve this too.

I found a really good function from Torry's page.
here it is...


function FindComponentEx(const Name: string): TComponent;
var
FormName: string;
CompName: string;
P: Integer;
Found: Boolean;
Form: TForm;
I: Integer;
begin
// Split up in a valid form and a valid component name
P := Pos('.', Name);
if P = 0 then
begin
raise Exception.Create('No valid form name given');
end;
FormName := Copy(Name, 1, P - 1);
CompName := Copy(Name, P + 1, High(Integer));
Found := False;
// find the form
for I := 0 to Screen.FormCount - 1 do
begin
Form := Screen.Forms[I];
// case insensitive comparing
if AnsiSameText(Form.Name, FormName) then
begin
Found := True;
Break;
end;
end;
if Found then
begin
for I := 0 to Form.ComponentCount - 1 do
begin
Result := Form.Components[I];
if AnsiSameText(Result.Name, CompName) then Exit;
end;
end;
Result := nil;
end;

This function gives you the flexibility to found any component in any form... an use it as you like

Thank you all for the anwsers... This Case is SOLVED...

Traveler
13-02-2011, 08:09 PM
Hmm how 'bout this one:



procedure renameAllLabels();
var i : byte;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TLabel then
(Components[i] as TLabel).caption := 'Hello';
end;

It does roughly the same as your example. Perhaps not so flexible, but less code.

azrael11
13-02-2011, 10:22 PM
Hmm how 'bout this one:



procedure renameAllLabels();
var i : byte;
begin
for i := 0 to ComponentCount - 1 do
if Components[i] is TLabel then
(Components[i] as TLabel).caption := 'Hello';
end;

It does roughly the same as your example. Perhaps not so flexible, but less code.

This is a nice small gold peace of code... ;)

User137
14-02-2011, 12:46 AM
How about
Application.FindComponent(string)

azrael11
14-02-2011, 05:54 AM
How about
Application.FindComponent(string)

Hello User137
Application.FindComponent(String) works only from the form that have the desire component.
If you want to control the components from an independent unit use the above code and dont forget to have the corresponding in uses....

Thanks my friend for the anwser...

paul_nicholls
14-02-2011, 09:52 AM
Thank you Paul for anwser but
"Find Control" wants a handle control not a string
FindControl(Handle: HWnd)

Can i use the findComponet function ?
But how i can get the control of the componet so i can make it visible?

Thanks again...

Sorry about that, I was thinking about FindComponent and I wrote FindControl! :)

cheers,
Paul

azrael11
14-02-2011, 12:10 PM
Sorry about that, I was thinking about FindComponent and I wrote FindControl! :)

cheers,
Paul

You dont have to be sorry...
That you spent your time to anwser is great....

Thank you...

farcodev
15-02-2011, 12:30 AM
Thanks to all for the codes, it'S always useful ;)