一个 link 如何在 Delphi 中动态创建组件,以便可以在它们之间传输数据?

How can one link dynamically made components in Delphi so that data can be transferred between them?

我目前正在为我的年终学校项目申请,并且偶然发现了一个我无法完全想出解决方案的问题。 我的应用程序使用一个数据库来存储发放贷款的记录。在我的一个表单中,面板是为我的借款人数据库中的每个借款人动态添加的,该数据库具有唯一的贷款 ID。每个面板都包含从数据库的相应记录中检索到的信息,以及一个包含美元金额的组合框和一个紧挨着它的按钮,允许贷方将组合框中指定的钱借给该面板上包含的借款人。按下该按钮后,应该将一条新记录插入到另一个 table 中,其中包含通过该平台进行的所有交易,具有唯一的交易 ID,并且与借款人的 table 有关通过贷款 ID。

我想做的是让每个面板的每个按钮都有 属性 LoanID,它存储该面板上的贷款的 LoanID,然后它能够​​接受指定的金额该特定面板上的组合框;但是,我不知道该怎么做。

procedure TfrmLend.AddRecordsToForm;
var
  i, pnl_height : integer;
  new_panel, btnLend : TPanel;
  lblName, lblCitySector, lblReason, lblAmountLeft : TLabel;
  cbAmounts : TComboBox;
  gauge : TGauge;
  oBorrower : TBorrower;
begin
  with dmKiva do
    begin
      qry.SQL.Clear;
      qry.SQL.Text := 'SELECT * FROM tblBorrowers;';
      qry.Open;

      while not qry.eof do
        begin
          // Dynamically adding records to the form.

          oBorrower := TBorrower.Create(qry['FirstName'], qry['LastName'], qry['Country'],
            qry['City'], qry['Business'], qry['Sector'], qry['ReasonForLoan'], qry['LoanAmount'], qry['LoanID']);
          new_panel := CreateNewPanel;
          new_panel.ParentBackground := False;
          new_panel.Font.Name := 'Arial';
          new_panel.Font.Style := [];

          // Name of borrower.
          lblName := CreateNewLabel(new_panel);
          lblName.Font.Name := 'Arial Rounded MT';
          lblName.Font.Size := 12;
          if (oBorrower.GetFirstName <> '') AND (oBorrower.GetLastName <> '') then
            lblName.Caption := oBorrower.GetFirstName + ' ' + oBorrower.GetLastName
          else if oBorrower.GetFirstName <> '' then
            lblName.Caption := oBorrower.GetFirstName
          else
            lblName.Caption := oBorrower.GetBusiness;
          lblName.AutoSize := True;
          lblName.Top := 12;
          lblName.Left := 25;
          lblName.Width := 344;

          // Borrower's city and sector. Appears 'City / Sector'
          lblCitySector := CreateNewLabel(new_panel);
          lblCitySector.Font.Color := RGB(157, 157, 157);
          lblCitySector.Caption := oBorrower.GetCity + ' / ' + oBorrower.GetSector;
          lblCitySector.AutoSize := True;
          lblCitySector.Left := 25;
          lblCitySector.Top := lblName.Top + lblName.Height + 5;
          lblCitySector.Width := 344;

          // Borrower's reason for loan.
          lblReason := CreateNewLabel(new_panel);
          lblReason.Caption := oBorrower.GetReason;
          lblReason.AutoSize := True;
          lblReason.WordWrap := True;
          lblReason.Width := 344;
          lblReason.Height := 64;
          lblReason.Left := 25;
          lblReason.Top := lblCitySector.Top + lblCitySector.Height + 15;


          // Progress bar to indicate what portion of the loan has been met.
          gauge := TGauge.Create(Self);
          gauge.parent := new_panel;
          gauge.BackColor := RGB(216, 216, 216);
          gauge.ForeColor := RGB(79, 175, 78);
          gauge.Progress := 50;
          gauge.Width := 344;
          gauge.Height := 12;
          gauge.ShowText := False;
          gauge.Left := 25;
          gauge.Top := lblReason.Top + lblReason.Height + 10;

          // Label indicating the amount that still needs to be lent.
          lblAmountLeft := CreateNewLabel(new_panel);
          lblAmountLeft.Caption := '$' + FloatToStrF(gauge.Progress / 100 * oBorrower.GetAmount, ffFixed, 5, 2) + ' to go';
          lblAmountLeft.AutoSize := True;
          lblAmountLeft.Font.Color :=  RGB(79, 175, 78);
          lblAmountLeft.Font.Style := [fsItalic];
          lblAmountLeft.Font.Size := 9;
          lblAmountLeft.Left := 160;
          lblAmountLeft.Top := gauge.Top + gauge.Height + 2;

          // Combobox with loan amounts in  increments.
          cbAmounts := TComboBox.Create(Self);
          cbAmounts.Parent := new_panel;
          cbAmounts.Font.Size := 10;
          cbAmounts.Font.Name := 'Arial';
          cbAmounts.Font.Style := [];
          cbAmounts.Font.Color := RGB(72, 72, 72);

          for i := 1 to 20 do
            begin
              cbAmounts.Items.Add('$' + IntToStr(i * 25));
            end;
          cbAmounts.ItemIndex := 0;
          cbAmounts.Left := 25;
          cbAmounts.Top := lblAmountLeft.Top + lblAmountLeft.Height + 30;
          cbAmounts.Width := 80;
          cbAmounts.Style := csOwnerDrawFixed;

          btnLend := TPanel.Create(Self);
          with btnLend do
            begin
              Parent := new_panel;
              ParentBackground := False;
              Color := RGB(17, 138, 236);
              Font.Name := 'Arial Rounded MT';
              Font.Color := clWhite;
              Caption := 'Lend Now';
              Font.Size := 12;
              Left := 138;
              Width := 185;
              Height := 41;
              Top := cbAmounts.Top - 10;
              OnClick := btnSubmitClick;
            end;


          pnl_height := cbAmounts.Top + cbAmounts.Height + 25;

          new_panel.Height := pnl_height;
          y := new_panel.Top + new_panel.Height;
          oBorrower.Free;
          qry.Next;
        end;

    end;

end;

你快到了。

您已经创建了一个包含您需要的 LoanID 的 TBorrower,但是您在创建面板后要释放它。

选项 1 - 使用 TPanel 的标签 属性 来保存 LoandID(它是一个 NativeInt)。

选项 2 - 创建自定义 TPanel class(您可能已经在 CreateNewPanel 中执行此操作),但其中包含用于 TBorrower 的 属性,然后将 TBorrower 保存到 TPanel。请记住在释放自定义 TPanel 时释放 TBorrower。

选项 3 - 按照 fpiette 的建议,使用 TFrame 而不是自定义 TPanel。

TFrame与TForm类似,只是需要在Form中展示,不要孤立使用。因此,您要编码的所有方法都可以成为 TFrame class.

的一部分