Recent

Author Topic: Iterate through components on TTabSheet  (Read 13341 times)

ProgEd

  • New Member
  • *
  • Posts: 25
Iterate through components on TTabSheet
« on: March 13, 2013, 03:28:10 pm »
I have a TTabSheet with lots of TEdit fields on it. I'm looking for a way to write a single event handler for all of them (they get processed identically). I'd also like to be able to iterate through all of them when I'm ready to save the data that's been entered. My memory tells me there's an enumerator that does this, but I can't remember what it is or how to use it. Can anyone help me, please?

Ed

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Iterate through components on TTabSheet
« Reply #1 on: March 13, 2013, 03:53:40 pm »
If you want single handler you need:
Code: [Select]
with (Sender as TEdit) do ...it will always take actual TEdit.

To list all Edits on TabSheet is a little more difficult because owner is always TForm but Parent is TabSheet. Try this:
Code: [Select]
procedure TForm1.Button1Click(Sender: TObject);
var i: Integer;
begin
  for i:=0 to ComponentCount-1 do
    if (Components[i] is TEdit) then
      if (Components[i] as TEdit).Parent=TabSheet1 then writeln(Components[i].Name);
end;
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/

ProgEd

  • New Member
  • *
  • Posts: 25
Re: Iterate through components on TTabSheet
« Reply #2 on: March 13, 2013, 04:50:05 pm »
Thank you! Precisely what I was looking for.

Ed

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Iterate through components on TTabSheet
« Reply #3 on: March 13, 2013, 05:49:01 pm »
Actually its even easier instead of components use controls. eg

Code: [Select]
var
  vCnt : Integer;
begin
  for vCnt := 0 to TabSheet2.ControlCount -1 do
  begin
    TabSheet2.Controls[vCnt].Enabled := not TabSheet2.Controls[vCnt].Enabled;
  end;
end;

Components has all the objects that the component owns controls collection has all the objects that it parents.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Iterate through components on TTabSheet
« Reply #4 on: March 13, 2013, 05:55:48 pm »
Yes, this way is better.
Code: [Select]
var i: Integer;
begin
  for i:=0 to TabSheet1.ControlCount-1 do
    if TabSheet1.Controls[i] is TEdit then ... ;
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/

vrull

  • Full Member
  • ***
  • Posts: 118
Re: Iterate through components on TTabSheet
« Reply #5 on: March 18, 2013, 09:09:14 pm »
Not sure, if it's true for Lazarus, but in Delphi the control is not in the list if it's placed on the child container, like TPanel ot TTabSheet. So iterating through Components on the form will give you everything on the form, but Controls suit better if you are interested in a particular TabSheet which has no child containers.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Iterate through components on TTabSheet
« Reply #6 on: March 19, 2013, 01:02:30 am »
There is also panel1.Controls[]... Reason that using Controls-array is better, is that the array to go through is more propably precise and smaller, faster. If you have a complicated form with many controls, then going through Components would result in lots of false matches.

 

TinyPortal © 2005-2018