Link to home
Start Free TrialLog in
Avatar of hidrau
hidrauFlag for Brazil

asked on

Changing all the component in my form that tag = 5

Hello guys

I would like to create a " for " where I could get all the components on my form that has in their tag the value 5 and changed their propert enabled to false.

Thanks
Avatar of calinutz
calinutz
Flag of Romania image

Disable all Edits on a form that have TAG=5:


var
   i : integer;
begin
   for i:= 0  to ComponentCount - 1 do
     begin
     if (Components[i] is TEdit) and (Components[i].Tag=5) then
        TEdit(Components[i]).Enabled := false;
     end;
end;
Avatar of hidrau

ASKER

Isn't there any general way to this ? I must inform the component class like Tedit, Tbitbtn, etc

Thanks
Just add If clauses for any other kind of components that you have on your form that you want to disable in the FOR loop
something like for buttons for example:

     if (Components[i] is TButton) and (Components[i].Tag=5) then
        TButton(Components[i]).Enabled := false;


regards
for i:= 0  to ComponentCount - 1 do
     begin
     if (Components[i] is TEdit) and (Components[i].Tag=5) then
        TEdit(Components[i]).Enabled := false;

     if (Components[i] is TButton) and (Components[i].Tag=5) then
        TButton(Components[i]). Enabled := false;

     if (Components[i] is TLabel) and (Components[i].Tag=5) then
        TLabel(Components[i]). Enabled := false;

     if (Components[i] is TMemo) and (Components[i].Tag=5) then
        TMemo(Components[i]). Enabled := false;

// and so on for all kinds of components that you are using on that form

     end;



Regards
You could try something like this, I think it "should" work ...

in the .Create event for your form add this code;

  for i1:=0 to ComponentCount-1 do
    begin
      if Components[i1].tag=5 then
        Components[i1].enabled:=false;
    end;

Remeber to declare -

var
  i1:                  integer;

 - in there too.

If that does not work, you may have to get a little more specific, as follows;

  for i1:=0 to ComponentCount-1 do
    begin
       // replace TButton with your specific component types ...

       if Components[i1] is Tbutton then
         begin
            if (Components[i1] as TButton).Tag=5 then
              (Components[i1] as TButton).Enabled:=false;
         end
         else
           if Components[i1] is TEditBox then
             begin
                if (Components[i1] as TEditBox).Tag=5 then
                  (Components[i1] as TEditBox).Enabled:=false;
             end;

       // and so on ...
    end;



Cheers,

David.
nuts, I should remember to "refresh" before I post :)

David_Ward....
This does not work :         Components[i1].enabled:=false;
and the                            (Components[i1] as TEdit)    is the same as TEdit(Components[i])

:)
No problem
:-)
yep, I said the first bit might be too general

and as for: "(Components[i1] as TEdit)    is the same as TEdit(Components[i])" I know :) but we all have our own little ways now, dont we.
:)

What about this ... It might be worth a try ... (I have not tested it, just kicking it around ...)

var
  ThisObject: TObject;
  i1:              Integer;

begin
  for i1:=0 to pred(COmponentCount) do
     begin
        ThisObject:=Components[i1];

         if ThisObject.Tag=5 then
           ThisObject.ENabled:=true;
     end;
end;

var
  ThisObject: TObject;
  i1:              Integer;

begin
  for i1:=0 to pred(COmponentCount) do
     begin
        ThisObject:=Components[i1];

         if ThisObject.Tag=5 then
           ThisObject.ENabled:=False;  // not true; sorry :)
     end;
end;
Still does not work this way... You must specify the type of the component

Should be like:

procedure TForm1.Button3Click(Sender: TObject);
var
  thisObj:TObject;
 i1:              Integer;

begin
  for i1:=0 to pred(COmponentCount) do
     begin
        ThisObj:=Components[i1];
         If ThisObj is TEdit then
         if TEdit(ThisObj).Tag=5 then
           TEdit(ThisObj).ENabled:=False;  // not true; sorry :)
     end;
end;

Regards
:-)
Avatar of hidrau

ASKER

No David_Ward , it didn't work
You cannot refference a property of a component that you don't know what it is...
Do you test these code lines?
Avatar of hidrau

ASKER

so calinutz
Isn't there any way to do something more generic?

David laughs at calinutz ...

I said I had not tested it, do you not read the posts :)

ASKER CERTIFIED SOLUTION
Avatar of calinutz
calinutz
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hidrau

ASKER

Thanks calinutz
alex
" David laughs at calinutz ..."

:))

I only read question and code :-)
Alex, I have no knowledge of a more generic way to do it.
And I really thing that there isn't any :(

regards
Avatar of kretzschmar
usual the property enabled is introduced and published with tcontrol


so it should be simplified to

var
   i : integer;
begin
   for i:= 0  to ComponentCount - 1 do
     If (Components[i].Tag=5) and (Components[i] is TControl) then
       TControl(Components[i]).enabled := false;
end;

meikl ;-)

Avatar of hidrau

ASKER

Hello kretzschmar

Thanks for your help too.
Could you open this post for me so that I could spli the point once again?

Thanks
>Could you open this post for me so that I could spli the point once again?

this is not needed, hidrau, i have really enough points :-))

additional i was far too late . . .

its enough for me, if you find it usefull

meikl ;-)
Avatar of hidrau

ASKER

ok thanks even though