Recent

Author Topic: How to change Object proprierties in a FOR cycle  (Read 4113 times)

ciammaruca

  • New Member
  • *
  • Posts: 20
How to change Object proprierties in a FOR cycle
« on: November 03, 2014, 08:12:54 am »
hi,
how to change properties of several Objects in a cycle FOR? In my example i'd like to change to Enabled:=FALSE all my seven LabeledEdit.
Like this approximately:
   for i:=1 to 7 do
   begin
   ('LabeledEdit+i').Enabled:=False;
   end;


hy

  • Full Member
  • ***
  • Posts: 224
Re: How to change Object proprierties in a FOR cycle
« Reply #1 on: November 03, 2014, 08:40:11 am »
Code: [Select]
var

      i     :  integer;
   begin
      for i := 0 to self.ComponentCount do
      begin
         if self.Components[i] is TLabeledEdit then
            TLabeledEdit(Components[i]).enabled := false;
      end;
   end;
   
_____
***hy

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: How to change Object proprierties in a FOR cycle
« Reply #2 on: November 03, 2014, 03:12:26 pm »
ok, thanks; neverthless:
if i put only one line it's all ok; but when i put more then one line it's a caos! The first line is not executed, the second one is applied to all objects!
So, my TLabeledEdit are with a checkbox inside a groupbox, in a Form2.
For example, when i put these lines, also checkbox is made disabled:
       TLabeledEdit(Components).Font.Size:=6;
       TLabeledEdit(Components).enabled := false;
On the contrary when i put  these lines, also checkbox and groupbox captions are made  Font.Size:=6:
       TLabeledEdit(Components).enabled := false;
       TLabeledEdit(Components).Font.Size:=6;
And so on with the different proprierties.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12888
  • FPC developer.
Re: How to change Object proprierties in a FOR cycle
« Reply #3 on: November 03, 2014, 05:04:44 pm »
Your code has   TLabeledEdit(components).xxx, while hy's code has
Code: [Select]
TLabeledEdit(components[i]).xxx

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: How to change Object proprierties in a FOR cycle
« Reply #4 on: November 03, 2014, 05:24:54 pm »
Quote
Your code has   TLabeledEdit(components).xxx, while hy's code has ...
I guess he has it too but forum software changed [ i ] to italic font because he didn't use the [ code ] tag for code.
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

Bart

  • Hero Member
  • *****
  • Posts: 5727
    • Bart en Mariska's Webstek
Re: How to change Object proprierties in a FOR cycle
« Reply #5 on: November 03, 2014, 05:26:21 pm »
ok, thanks; neverthless:
if i put only one line it's all ok; but when i put more then one line it's a caos! The first line is not executed, the second one is applied to all objects!
So, my TLabeledEdit are with a checkbox inside a groupbox, in a Form2.
For example, when i put these lines, also checkbox is made disabled:
Code: [Select]
       TLabeledEdit(Components[i]).Font.Size:=6;
       TLabeledEdit(Components[i]).enabled := false;
On the contrary when i put  these lines, also checkbox and groupbox captions are made  Font.Size:=6:
Code: [Select]
       TLabeledEdit(Components[i]).enabled := false;
       TLabeledEdit(Components[i]).Font.Size:=6;

A guess you forgot to put these into a begin / end block. Try:
Code: [Select]
var
   i     :  integer;
begin
   for i := 0 to self.ComponentCount do
   begin
      if self.Components[i] is TLabeledEdit then
      begin //if you don't use the begin/end then the second statement will always be executed!
         TLabeledEdit(Components[i]).enabled := false;
         TLabeledEdit(Components[i]).Font.Size:=6;
      end;
   end;
end;

Bart
« Last Edit: November 03, 2014, 05:27:59 pm by Bart »

ciammaruca

  • New Member
  • *
  • Posts: 20
Re: How to change Object proprierties in a FOR cycle
« Reply #6 on: November 03, 2014, 07:29:09 pm »
hi, 
yes, you right! I forgot the begin/end.
Cadena perpetua para mi.
saluti

 

TinyPortal © 2005-2018