* * *

Author Topic: Arrays of DBEdit boxes  (Read 977 times)

Penticom

  • Newbie
  • Posts: 2
Arrays of DBEdit boxes
« on: February 28, 2017, 06:31:36 am »
OK, I am very used to changing the look and feel of Textboxes in an array. EG It is very handy for users to recognise if the back ground is white(edit) or Grey(viewonly) in our software. We have done this in the past be utilising arrays of texboxes or arrays of dbedit fields. EG EditField(0), EditField(1) etc etc etc.
So how do I get the Lazarus TEdit or Dbedit boxes to be part of an array for better control

molly

  • Hero Member
  • *****
  • Posts: 1681
Re: Arrays of DBEdit boxes
« Reply #1 on: February 28, 2017, 08:19:10 am »
Instead of referencing those component from an array, why not place all those items you wish to 'modify' on a panel on its own ?

You could then use something like this:
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, ExtCtrls,
  9.   StdCtrls;
  10.  
  11. type
  12.  
  13.   { TForm1 }
  14.  
  15.   TForm1 = class(TForm)
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     Edit4: TEdit;
  20.     Edit5: TEdit;
  21.     Memo1: TMemo;
  22.     Panel1: TPanel;
  23.     ToggleBox1: TToggleBox;
  24.     procedure ToggleBox1Click(Sender: TObject);
  25.   private
  26.     { private declarations }
  27.   public
  28.     { public declarations }
  29.   end;
  30.  
  31. var
  32.   Form1: TForm1;
  33.  
  34. implementation
  35.  
  36. {$R *.lfm}
  37.  
  38. { TForm1 }
  39.  
  40. procedure TForm1.ToggleBox1Click(Sender: TObject);
  41. var
  42.   i: integer;
  43. begin
  44.   for i := 0 to Pred(Panel1.ControlCount) do
  45.   begin
  46.     if (Sender as TToggleBox).Checked then
  47.     begin
  48.       case Panel1.Controls[i].ClassName of
  49.         'TEdit' : (Panel1.Controls[i] as TEdit).Color := clRed;
  50.         'TMemo' : (Panel1.Controls[i] as TMemo).Color := clYellow;
  51.       end;
  52.     end
  53.     else
  54.     begin
  55.       case Panel1.Controls[i].ClassName of
  56.         'TEdit' : (Panel1.Controls[i] as TEdit).Color := clBlue;
  57.         'TMemo' : (Panel1.Controls[i] as TMemo).Color := clGreen;
  58.       end;
  59.     end;
  60.   end;
  61. end;
  62.  
  63. end.
  64.  

Same technique can be used to 'store' the components into an array if you would still prefer that.

howardpc

  • Hero Member
  • *****
  • Posts: 2186
Re: Arrays of DBEdit boxes
« Reply #2 on: February 28, 2017, 11:00:01 am »
Forms already supply array properties containing all components and all controls.
So you can adapt code such as the following (this example calls ColorAllEdits in the OnCreate, but in your program you would call it wherever appropriate).
Code: Pascal  [Select]
  1. unit Unit1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Forms, Controls, Graphics, StdCtrls, DbCtrls;
  9.  
  10. type
  11.  
  12.   TForm1 = class(TForm)
  13.     DBEdit1: TDBEdit;
  14.     DBEdit2: TDBEdit;
  15.     DBEdit3: TDBEdit;
  16.     Edit1: TEdit;
  17.     Edit2: TEdit;
  18.     Edit3: TEdit;
  19.     Edit4: TEdit;
  20.     procedure FormCreate(Sender: TObject);
  21.   private
  22.     procedure ColorAllEdits;
  23.   end;
  24.  
  25.   TEditColors = array[boolean] of TColor;
  26.  
  27. const
  28.   EditColors: TEditColors = (clWhite, clLtGray); // set your colors here
  29.  
  30. var
  31.   Form1: TForm1;
  32.  
  33. implementation
  34.  
  35. {$R *.lfm}
  36.  
  37. { TForm1 }
  38.  
  39. procedure TForm1.ColorAllEdits;
  40. var
  41.   c: TControl;
  42.   ed: TEdit absolute c;
  43.   dbe: TDBEdit absolute c;
  44.   i: Integer;
  45. begin
  46.   for i:=0 to ControlCount-1 do begin
  47.     c:=Controls[i];
  48.     if (c is TEdit) then
  49.       ed.Color:=EditColors[ed.ReadOnly]
  50.     else if (c is TDBEdit) then
  51.       dbe.Color:=EditColors[dbe.ReadOnly];
  52.   end;
  53. end;
  54.  
  55. procedure TForm1.FormCreate(Sender: TObject);
  56. begin
  57.   ColorAllEdits;
  58. end;
  59.  
  60. end.

Penticom

  • Newbie
  • Posts: 2
Re: Arrays of DBEdit boxes
« Reply #3 on: March 01, 2017, 02:27:20 pm »
OMG, all that code for 5 TEDit background color changes on a status change. Normaly did it in 5 lines of code. So Lazarus is going to be a BLOATED  coding then. GREAT!!! NOT!

GetMem

  • Hero Member
  • *****
  • Posts: 2134
Re: Arrays of DBEdit boxes
« Reply #4 on: March 01, 2017, 02:45:10 pm »
Quote
OMG, all that code for 5 TEDit background color changes on a status change. Normaly did it in 5 lines of code. So Lazarus is going to be a BLOATED  coding then. GREAT!!! NOT!
Think again! What if you have 100 TEdits instead of five? Then your code is gonna be the bloated one. Both @molly and @howardpc gave you a generic solution and yes for five TEdit perhaps it's not worth the effort, but then what was your question about?

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus