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

Thanks
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

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

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

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

0
 
David_WardCommented:
David laughs at calinutz ...

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

0
 
hidrauAuthor Commented:
Thanks calinutz
alex
0
 
calinutzCommented:
" David laughs at calinutz ..."

:))

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

regards
0
 
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 ;-)

0
 
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
0
 
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 ;-)
0
 
hidrauAuthor Commented:
ok thanks even though
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.