Recent

Author Topic: Loop to disable/enable TEdits  (Read 704 times)

babycode

  • New Member
  • *
  • Posts: 42
Loop to disable/enable TEdits
« on: September 01, 2024, 04:48:19 pm »
How do I disable the Edits that are inside the GroupBox, which in turn are in a TabSheet of a specific PageControl of the Form? This procedure is in a separate unit because I want to use it in other forms (reuse) so I pass the Form, the name of the PageControl. I'm using Lazarus Free Pascal. It doesn't show an error but it also doesn't change the status of the Edits.

Code: Pascal  [Select][+][-]
  1. procedure ApenasVisualizar(Frm: TForm; PageControlName: String; Ativo: Boolean);
  2. var
  3.   i, j, k, l: Integer;
  4.   PageControl: TPageControl;
  5.   TabSheet: TTabSheet;
  6.   GroupBox: TGroupBox;
  7.   Component: TComponent;
  8. begin
  9.   PageControl := Frm.FindComponent(PageControlName) as TPageControl;
  10.  
  11.   if Assigned(PageControl) then
  12.   begin
  13.     for i := 0 to PageControl.PageCount - 1 do
  14.     begin
  15.       TabSheet := PageControl.Pages[i];
  16.  
  17.       for k := 0 to TabSheet.ComponentCount - 1 do
  18.       begin
  19.         if TabSheet.Components[k] is TGroupBox then
  20.         begin
  21.           GroupBox := TGroupBox(TabSheet.Components[k]);
  22.  
  23.           for l := 0 to GroupBox.ComponentCount - 1 do
  24.           begin
  25.             Component := GroupBox.Components[l];
  26.  
  27.             if Component is TEdit then
  28.             begin:
  29.               TEdit(Component).Enabled := not Ativo;
  30.             end;
  31.           end;
  32.         end;
  33.          end;
  34.        end;
  35.         end
  36.         else
  37.     raise Exception.CreateFmt('PageControl "%s" não encontrado no Form "%s".', [PageControlName, Frm.Name]);
  38. end;

bobby100

  • Sr. Member
  • ****
  • Posts: 301
    • Malzilla
Re: Loop to disable/enable TEdits
« Reply #1 on: September 01, 2024, 05:07:40 pm »
Does your loop finds the TEdits? (debug step by step)
Did you try to invalidate the TabSheet or the whole form after the loop?

dseligo

  • Hero Member
  • *****
  • Posts: 1674
Re: Loop to disable/enable TEdits
« Reply #2 on: September 01, 2024, 05:10:45 pm »
Use Controls instead of components:

Code: Pascal  [Select][+][-]
  1. procedure ApenasVisualizar(Frm: TForm; PageControlName: String; Ativo: Boolean);
  2. var
  3.   i, j, k, l: Integer;
  4.   PageControl: TPageControl;
  5.   TabSheet: TTabSheet;
  6.   GroupBox: TGroupBox;
  7.   Control: TControl;
  8. begin
  9.   PageControl := Frm.FindComponent(PageControlName) as TPageControl;
  10.  
  11.   if Assigned(PageControl) then
  12.   begin
  13.     for i := 0 to PageControl.PageCount - 1 do
  14.     begin
  15.       TabSheet := PageControl.Pages[i];
  16.  
  17.       for k := 0 to TabSheet.ControlCount - 1 do
  18.       begin
  19.         if TabSheet.Controls[k] is TGroupBox then
  20.         begin
  21.           GroupBox := TGroupBox(TabSheet.Controls[k]);
  22.  
  23.           for l := 0 to GroupBox.ControlCount - 1 do
  24.           begin
  25.             Control := GroupBox.Controls[l];
  26.  
  27.             if Control is TEdit then
  28.             begin
  29.               TEdit(Control).Enabled := not Ativo;
  30.             end;
  31.           end;
  32.         end;
  33.       end;
  34.     end;
  35.   end
  36. else
  37.    raise Exception.CreateFmt('PageControl "%s" não encontrado no Form "%s".', [PageControlName, Frm.Name]);
  38. end;
« Last Edit: September 01, 2024, 05:13:53 pm by dseligo »

cpicanco

  • Hero Member
  • *****
  • Posts: 674
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Loop to disable/enable TEdits
« Reply #3 on: September 01, 2024, 05:15:44 pm »
You may loop Forms' components and use the component parent for filtering:
Code: Pascal  [Select][+][-]
  1. var
  2.   i : integer;
  3. begin
  4.   for i := 0 to ComponentCount-1 do begin
  5.     if Components[i] is TEdit then begin
  6.       if TEdit(Components[i]).Parent is TGroupBox then begin
  7.         TEdit(Components[i]).Enabled := False;
  8.       end;
  9.     end;
  10.   end;  
« Last Edit: September 01, 2024, 05:18:13 pm by cpicanco »
Be mindful and excellent with each other.
https://github.com/cpicanco/

Sieben

  • Sr. Member
  • ****
  • Posts: 385
Re: Loop to disable/enable TEdits
« Reply #4 on: September 01, 2024, 05:25:24 pm »
The TControl.Components array holds all components whose Owner is the Control in question, to find all child controls you have to use the TWinControl.Controls array which holds all controls whose Parent is the WinControl in question. So instead of ComponentCount and Components[] you have to use ControlCount and Controls[] - like dseligo just pointed out.
« Last Edit: September 01, 2024, 05:30:08 pm by Sieben »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

BrunoK

  • Hero Member
  • *****
  • Posts: 766
  • Retired programmer
Re: Loop to disable/enable TEdits
« Reply #5 on: September 01, 2024, 06:26:49 pm »
Guaranteed untested but something like
Code: Pascal  [Select][+][-]
  1. procedure ApenasVisualizar(Frm: TForm1; PageControlName: String; Ativo: Boolean);
  2.  
  3.   function FindPageControl(aControl: TControl): TPageControl;
  4.   begin
  5.     for TPageControl(Result) in aControl.Components do
  6.       if (Result is TPageControl) and (Result.Name = PageControlName) then
  7.         exit(TPageControl(lControl))
  8.       else
  9.         FindPageControl(Result);
  10.     Result := nil;
  11.   end;
  12.  
  13.   procedure SetEditEnabled(aWinControl: TWinControl);
  14.   var
  15.     lWinControl: TWinControl;
  16.   begin
  17.     for TWinControl(lWinControl) in aWinControl.Controls do
  18.       if lWinControl.InheritsFrom(TWinControl) then
  19.         if (lWinControl is TEdit) and (lWinControl.Parent.InheritsFrom(TGroupBox)) then
  20.           TEdit(lWinControl).Enabled := Ativo
  21.         else
  22.           SetEditEnabled(lWinControl);
  23.   end;
  24.  
  25. var
  26.   PageControl: TPageControl;
  27. begin
  28.   PageControl := FindPageControl(Frm);
  29.   if Assigned(PageControl) then
  30.     SetEditEnabled(PageControl)
  31.   else
  32.     raise Exception.CreateFmt('PageControl "%s" não encontrado no Form "%s".',
  33.       [PageControlName, Frm.Name]);
  34. end;
might work.

babycode

  • New Member
  • *
  • Posts: 42
Re: Loop to disable/enable TEdits
« Reply #6 on: September 01, 2024, 06:32:22 pm »
Thanks for the help! the implementation of dseligo worked. Thanks guys.

 

TinyPortal © 2005-2018