Programmation en dynamique
Bonjour à tous,
je me suis fais un petit programme avec des composants crées dynamiquement et gerés dynamiquement. J'ai des exceptions assez étranges. C'est surtout les erreurs lors du "moins" qui m'intriguent. Est ce parce que mes objets ne se liberent pas ?
Voici le code. Je précise que je travaille en Delphi 7.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
sb : TScrollBox ;
gb : TgroupBox ;
btPlus : TBitBtn ;
btMoins : TBitBtn ;
ICompteur : integer ;
itop : integer ;
Procedure CreeGroupBox ;
Procedure OnclickPlusGB(Sender: TObject) ;
Procedure OnclickMoinsGB(Sender: TObject) ;
public
{ Déclarations publiques }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
Procedure TForm1.CreeGroupBox ;
var
i : integer ;
begin
if sb <> nil then
freeandnil(sb) ;
sb := TScrollBox.Create(Form1) ;
sb.Parent := Self ;
sb.Align := alClient ;
for i:=0 to Icompteur-1 do
begin
gb := TgroupBox.Create(sb) ;
gb.Parent := sb ;
gb.Left := 200;
gb.Width := 500 ;
gb.Top := gb.Top + (200*i) ;
gb.Height := 150 ;
btPlus := TBitBtn.Create(gb) ;
btPlus.Parent := gb ;
btPlus.Top := 20 ;
btPlus.Left := 100 ;
btPlus.OnClick := OnclickPlusGB ;
btPlus.Caption := 'Plus' ;
btMoins := TBitBtn.Create(gb) ;
btMoins.Parent := gb ;
btMoins.Top := 80 ;
btMoins.Left := 100 ;
btMoins.OnClick := OnclickMoinsGB ;
btMoins.Caption := 'Moins' ;
end ;
end ;
Procedure TForm1.OnclickPlusGB(Sender: TObject) ;
begin
Inc(ICompteur);
CreeGroupBox ;
end ;
Procedure TForm1.OnclickMoinsGB(Sender: TObject) ;
begin
if ICompteur > 1 then
ICompteur := ICompteur -1 ;
CreeGroupBox ;
end ;
procedure TForm1.FormCreate(Sender: TObject);
begin
ICompteur := 1 ;
itop := 50 ;
CreeGroupBox ;
end;
end. |