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
LVL 1
hidrauAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

calinutzCommented:
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;
hidrauAuthor Commented:
Isn't there any general way to this ? I must inform the component class like Tedit, Tbitbtn, etc

Thanks
calinutzCommented:
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
Learn SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

calinutzCommented:
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
David_WardCommented:
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.
David_WardCommented:
nuts, I should remember to "refresh" before I post :)

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

:)
calinutzCommented:
No problem
:-)
David_WardCommented:
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.
David_WardCommented:
:)

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;
David_WardCommented:

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;
calinutzCommented:
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
:-)
hidrauAuthor Commented:
No David_Ward , it didn't work
calinutzCommented:
You cannot refference a property of a component that you don't know what it is...
Do you test these code lines?
hidrauAuthor Commented:
so calinutz
Isn't there any way to do something more generic?

David_WardCommented:
David laughs at calinutz ...

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

calinutzCommented:
It should not be that difficult to question all kinds of components in a for statement...


var
   i : integer;
begin
   for i:= 0  to ComponentCount - 1 do
   If Components[i].Tag=5 then
   begin
    if (Components[i] is TEdit) then TEdit(Components[i]).Enabled := false;
    if (Components[i] is TButton) then TButton(Components[i]). Enabled := false;
    if (Components[i] is TMemo) then TMemo(Components[i]). Enabled := false;
    if (Components[i] is TLabel) then TLabel(Components[i]). Enabled := false;
    if (Components[i] is TComboBox) then TComboBox(Components[i]). Enabled := false;
    if (Components[i] is TCheckBox) then TCheckBox(Components[i]). Enabled := false;
    if (Components[i] is TRadioButton) then TRadioButton(Components[i]). Enabled := false;
   end;
end;

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
hidrauAuthor Commented:
Thanks calinutz
alex
calinutzCommented:
" David laughs at calinutz ..."

:))

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

regards
kretzschmarCommented:
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 ;-)

hidrauAuthor Commented:
Hello kretzschmar

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

Thanks
kretzschmarCommented:
>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 ;-)
hidrauAuthor Commented:
ok thanks even though
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.