user15784194
user15784194

Reputation:

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

I'm currently working on an application for my end-of-year school project and have stumbled on an issue that I can't quite think up a solution for. My application uses a database that stores records of loans issued. In one of my forms, panels are added dynamically for each borrower in my borrowers database which has a unique loan ID. Each panel contains info retrieved from the respective record from the database along with a combo box containing amounts of dollars and a button directly next to it, allowing a lender to lend the money specified in the combo box to the borrower contained on that panel. Once that button is pressed, a new record is supposed to be inserted into another table that contains all the transactions made through the platform, which has a unique transaction ID, and is related to the table of borrowers through the loan ID.

What I'm thinking about doing is having each button of each panel have the property LoanID, which stores the LoanID of the loan on that panel, which then is able to take in the amount specified within the combo box on that particular panel; however, I don't know how to go about doing this.

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 $25 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;

enter image description here

Upvotes: 1

Views: 139

Answers (1)

Rob Lambden
Rob Lambden

Reputation: 2293

You are almost there.

You already create a TBorrower that includes the LoanID you need, but you are freeing this after you have created the panel.

Option 1 - use the Tag property of the TPanel to hold the LoandID (it's a NativeInt).

Option 2 - Create a custom TPanel class (which you may aready be doing in CreateNewPanel), but which includes a property for the TBorrower, and then save the TBorrower to the TPanel. Remember to free the TBorrower when you free the custom TPanel.

Option 3 - as fpiette has suggested, use a TFrame instead of a custom TPanel.

TFrame is similar to TForm, except it needs to be shown in a Form, you don't use it in isolation. So all of the methods you want to code can be part of the TFrame class.

Upvotes: 2

Related Questions