[ NOOB ALERT ]... instead of writing nearly identical code for hundreds of lines.
If I can help it, I don't wanna have all that nearly same code over and over again.I'm porting a graphical program of mine to FPC 3.0.0 + Lazarus 1.6.2. The program uses 1+3 forms and those 3 have loads of checkboxes, textboxes and command buttons, with 1 each on same line, "grouped". Also has a load of command buttons which toggle a specific bunch, all of them, or reversing the states, of checkboxes, ending with another command button.
The name for the checkboxes is
chkNormal# where # is number starting from 0.
txtNormal# for textboxes (same syntax).
cmdNormal# for the buttons. The other toggling ones have different names but the code is nearly the same for all of them, calling the same procedure.
The checkboxes control the textbox and the command button next to it by toggling their
enabled property.
The program being designed in Visual Basic 6, I took advantage of component indexing, where you have multiple components with identical name but different index number. Whenever over 3 (2 in FPC) sequential checkboxes needed to be toggled, the code just iterated through every checkbox according to the name and index.
Once the last button is pressed, it checks each checkbox's state, complains if all are disabled, otherwise reads each textbox's contents and performs some calculation.
I've been unable to find a way to that, or something else which yields the same result. This is the code I barely found:
http://stackoverflow.com/questions/18402124/free-pascal-use-for-loop-to-determine-visibility-of-component-based-on-status-of/18402550 and what little I find suggests I'm using wrong way to do that.
This is the adapted, stripped code:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes , Sysutils , Forms , Controls , Graphics ,
Dialogs , StdCtrls , ExtCtrls ;
type
{ TfrmNormalSpells }
TfrmNormalSpells = class(Tform)
chkNormal0: Tcheckbox;
cmdNormal0: Tbutton;
txtNormal0: Tedit;
cmdAllDisable: Tbutton;
cmdAllEnable: Tbutton;
procedure cmdAllDisableClick(Sender: Tobject);
procedure cmdAllEnableClick(Sender: Tobject);
private
{ private declarations }
public
{ public declarations }
end;
var
frmNormalSpells: TfrmNormalSpells;
implementation
{$R *.lfm}
procedure TfrmNormalSpells.cmdAllEnableClick(Sender: Tobject);
begin
EnableDisableSpells(true, 'all', Self);
end;
procedure TfrmNormalSpells.cmdAllDisableClick(Sender: Tobject);
begin
EnableDisableSpells(false, 'all', Self);
end;
procedure EnableDisableSpells(ToEnable:boolean; strSpellType:string; AOwner:TComponent);
var
i:integer;
begin
for i := 0 to intSpellCount do //§intSpellCount§, 52, is also the count of those checkboxes, textboxes and command buttons. It's const-declared in the unit's start.
TCheckBox(AOwner.FindComponent('chkNormal' + IntToStr(i))).Checked := not ToEnable;
end;
Although FPC/Laz doesn't say anything being wrong, exactly nothing happens when I click either of those buttons, especially the
AllEnable one. I confirmed all that exists, by hovering on the names. The only messages Laz gives are the
Sender parts in those 2 procedure definitions not being used. That might explain why this doesn't work.