Board index » delphi » Component Access via Name??? - Help needed!

Component Access via Name??? - Help needed!

Is it possible to access a component(for example TEdit) by name?
I need an algorithm to access all components in a form like

If Component('Edit1').Type = TEdit then
   Component('Edit1').Text := 'a Text'
Else If Component(byName).Type = TRadiobutton then
etc..

Has anybody an idea?

Frank

 

Re:Component Access via Name??? - Help needed!


Frank,
 Take a look at my webpage under Delphi stuff. I have an example there
to determine what control is clicked. The essentials for what you are
looking for are there.

       Good luck!!
Dave
http://www.erols.com/dparsons

On 14 May 1998 05:37:47 GMT, jack...@aol.com (Jackomo) wrote:

Quote
>Is it possible to access a component(for example TEdit) by name?
>I need an algorithm to access all components in a form like

>If Component('Edit1').Type = TEdit then
>   Component('Edit1').Text := 'a Text'
>Else If Component(byName).Type = TRadiobutton then
>etc..

>Has anybody an idea?

>Frank

Re:Component Access via Name??? - Help needed!


Quote
Jackomo <jack...@aol.com> wrote:

: Is it possible to access a component(for example TEdit) by name?
: I need an algorithm to access all components in a form like

for i:=0 to form.componentcount-1 do begin
  if Comonent[i].name = 'Edit1 then TEdit(Component[i]).Text:='blah'
  else if Component[i] is TRadiobutton {I know, not an exact match, but an
indication to how to use it}
end;

: If Component('Edit1').Type = TEdit then
:    Component('Edit1').Text := 'a Text'
: Else If Component(byName).Type = TRadiobutton then
: etc..

: Has anybody an idea?

: Frank

HTH

  Roel

Re:Component Access via Name??? - Help needed!


Frank,

Quote
> Is it possible to access a component(for example TEdit) by name?

Yes, using FindComponent.

Quote
> I need an algorithm to access all components in a form like

> If Component('Edit1').Type = TEdit then
>    Component('Edit1').Text := 'a Text'
> Else If Component(byName).Type = TRadiobutton then
> etc..

Your loop would go something like this:

var
    Component: TComponent;
begin
    Component := FindComponent('Edit1');
    if not Assigned(Component) then
      Exit;
    if Component is TEdit then
       TEdit(Component).Text := 'a Text'
    else if Component is TRadioButton then
       TRadioButton(Component).Checked := True;
end;

I'm not sure why you're wanting to identify the control by name,
though... If you have the name 'Edit1', isn't it pretty obvious
that it's a TEdit?

If you could explain what you're trying to accomplish, there is
probably a pretty straightforward way of doing it.

HTH

Ken
--
Ken White
kwh...@westelcom.com

Clipper Functions for Delphi
http://members.aol.com/clipfunc/

Other Threads