1

いくつかのチェックボックスを作成しようとしていますが、その数はクエリのレコード数によって決まります。また、チェック ボックスの位置を前の位置から +38 に設定する必要があります。誰でもこれについて助けてくれますか?チェックボックスを作成する方法がわからない、残りは私ができるはずです...とにかく彼は私が今まで持っているものです。

var
  i, top,left : integer;
begin
......
    left := 81;
    top := 119;
    while i < qry.RecordCount do
                  begin
                    // create check box
                    // set caption of checkbox to i
                    // set checkbox loction to left + 38, top
                    // left = left+38??
                  end;
4

2 に答える 2

6

ニーズを明確にした後TObjectList、チェック ボックスのコンテナーとして使用することをお勧めします。このリストはオブジェクトを所有できます。これにより、リストからアイテムを単純に削除することでオブジェクトを解放できますClearまたは によってDelete。また、取得したインデックス付きアイテム オブジェクトを既知のクラス型に型キャストすることにより、各要素への簡単なアクセスも提供します。次のテストされていない疑似コードの詳細:

uses
  Contnrs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    CheckList: TObjectList;
  public
    { Public declarations }
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CheckList := TObjectList.Create;
  // setting OwnsObjects to True will ensure you, that the objects
  // stored in a list will be freed when you delete them from list
  CheckList.OwnsObjects := True;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  // this will also release all check boxes thanks to OwnsObjects
  CheckList.Free;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  CheckBox: TCheckBox;
begin
  ...
  CheckList.Clear;                      // this will free all check boxes    
  for I := 0 to RecordCount - 1 do      // iterate over your recordset
  begin
    CheckBox := TCheckBox.Create(nil);  // be sure to use nil as an owner
    CheckBox.Parent := Self;            // where will be laying (Self = Form)
    CheckBox.Caption := IntToStr(I);    // caption by the iterator value
    CheckBox.Top := 8;                  // fixed top position
    CheckBox.Left := (I * 38) + 8;      // iterator value * 38 shifted by 8
    CheckBox.Width := 30;               // fixed width
    CheckList.Add(CheckBox);            // add the check box to the list
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // this will check the first check box from the list (be careful to indexes)
  TCheckBox(CheckList.Items[0]).Checked := True;
  // this will delete 3rd check box from the list (using Clear will delete all)
  CheckList.Delete(2);
end;
于 2012-08-23T06:41:57.390 に答える
5

forここではループを使用することをお勧めしますが、疑似コードはほぼ文字通り Delphi コードに変換されます。

for I := 0 to qry.RecordCount-1 do
  begin
  CheckBox := TCheckBox.Create (Self);            // the form owns the checkbox
  CheckBox.Parent := Self;                        // checkbox is displayed on the form
  CheckBox.Caption := IntToStr (I);
  CheckBox.Top := Top;
  CheckBox.Left := 81 + I*38;
  end;

ところで、VCL に組み込まれた所有メカニズムのおかげで、作成したチェックボックスを解放する必要はありません。

于 2012-08-23T06:39:29.677 に答える