创建新按钮单击动态按钮的过程

时间:2014-03-28 10:30:04

标签: delphi delphi-7

我在为动态按钮创建新程序时遇到问题。单击该按钮时,它应该将项目发送到RichEdit。

我已经宣布了我的所有物品和手续。

这是我的代码:

type
  TfrmSale = class(TForm)
    redOutput: TRichEdit;
    btnBuy: TButton;
    procedure btnBuyClick(Sender: TObject);
  private
    pnlSale : TPanel;
    edtQuantity : TEdit;
    rgpOptions : TRadioGroup;
    btnSelect : TButton;
    procedure btnSelectCick(Sender: TObject);
  public
    { Public declarations }
  end;

var
  frmSale: TfrmSale;

implementation

{$R *.dfm}

procedure TfrmSale.btnBuyClick(Sender: TObject);
begin
  pnlSale := TPanel.Create(frmSale);
  pnlSale.Parent := frmSale;
  pnlSale.Left := 10;
  pnlSale.Top := 10;
  pnlSale.Width := 350;
  pnlSale.Height := 250;
  pnlSale.Visible := True;

  edtQuantity := TEdit.Create(pnlSale);
  edtQuantity.Parent := pnlSale;
  edtQuantity.Left := 50;
  edtQuantity.Top := 60;
  edtQuantity.Width := 80;
  edtQuantity.Height := 20;
  edtQuantity.Text := 'Quantity';
  edtQuantity.Visible := True;

  rgpOptions := TRadioGroup.Create(pnlSale);
  rgpOptions.Parent := pnlSale;
  rgpOptions.Left := 30;
  rgpOptions.Top := 100;
  rgpOptions.Width := 300;
  rgpOptions.Height := 140;
  rgpOptions.Visible := True;
  rgpOptions.Caption := 'Options';
  rgpOptions.Items.Add('Screws 12mm');
  rgpOptions.Items.Add('Canned Peaches 250g');
  rgpOptions.Items.Add('Tomatoes');
  rgpOptions.Items.Add('Spaghetti 500g');
  rgpOptions.Items.Add('Twin Flex 5m');
  rgpOptions.Items.Add('Clear Glue 250ml');

  btnSelect := TButton.Create(frmSale);
  btnSelect.Parent := frmSale;
  btnSelect.Left := 130;
  btnSelect.Top := 260;
  btnSelect.Width := 80;
  btnSelect.Height := 40;
  btnSelect.Caption := 'Select';
  end;

procedure TfrmSale.btnSelectClick(Sender: TObject);
  case rgpOptions.ItemIndex of
      1 := redOutput.Lines.Add('Screws 12mm');
      2 := redOutput.Lines.Add('Canned Peaches 250g');
      3 := redOutput.Lines.Add('Refil Blue Pen');
      4 := redOutput.Lines.Add('Tomatoes');
      5 := redOutput.Lines.Add('Spaghetti 500g');
      6 := redOutput.Lines.Add('Twin Flex 5m');
      7 := redOutput.Lines.Add('Clear Glue 250ml');
  end

请有人帮我制作OnClick程序。

1 个答案:

答案 0 :(得分:6)

AS TLama说。如果您要在代码中创建按钮,则还需要在代码中分配OnClick处理程序。

type
  TfrmSale = class(TForm)
    redOutput: TRichEdit;
    btnBuy: TButton;
    procedure btnBuyClick(Sender: TObject);     <<--- link made in designer
  private                                      VVVVV-- from here on it's 
    pnlSale : TPanel;                                  your responsibility
    edtQuantity : TEdit;
    rgpOptions : TRadioGroup;
    btnSelect : TButton;
    procedure btnSelectCick(Sender: TObject);  <<-- link must be made in code
  public
    { Public declarations }
  end;

var
  frmSale: TfrmSale;

implementation

{$R *.dfm}

procedure TfrmSale.btnBuyClick(Sender: TOBject);
begin
  pnlSale := TPanel.Create(frmSale);
  pnlSale.Parent := frmSale;
  pnlSale.Left := 10;
  pnlSale.Top := 10;
  pnlSale.Width := 350;
  pnlSale.Height := 250;
  pnlSale.Visible := True;

  edtQuantity := TEdit.Create(pnlSale);
  edtQuantity.Parent := pnlSale;
  edtQuantity.Left := 50;
  edtQuantity.Top := 60;
  edtQuantity.Width := 80;
  edtQuantity.Height := 20;
  edtQuantity.Text := 'Quantity';
  edtQuantity.Visible := True;

  rgpOptions := TRadioGroup.Create(pnlSale);
  rgpOptions.Parent := pnlSale;
  rgpOptions.Left := 30;
  rgpOptions.Top := 100;
  rgpOptions.Width := 300;
  rgpOptions.Height := 140;
  rgpOptions.Visible := True;
  rgpOptions.Caption := 'Options';
  rgpOptions.Items.Add('Screws 12mm');
  rgpOptions.Items.Add('Canned Peaches 250g');
  rgpOptions.Items.Add('Tomatoes');
  rgpOptions.Items.Add('Spaghetti 500g');
  rgpOptions.Items.Add('Twin Flex 5m');
  rgpOptions.Items.Add('Clear Glue 250ml');

  btnSelect := TButton.Create(frmSale);
  btnSelect.OnClick:= btnSelectCick;   <<-- Assign the button handler
  btnSelect.Parent := frmSale;
  btnSelect.Left := 130;
  btnSelect.Top := 260;
  btnSelect.Width := 80;
  btnSelect.Height := 40;
  btnSelect.Caption := 'Select';
  end;

procedure TfrmSale.btnSelectClick(Sender: TOBject);
begin
  redOutput.Lines.Add(rgpOptions.Lines[rgpOptions.ItemIndex]);  <<-- DRY
end;

请注意,我已对btnSelectClick进行了调整,以便重新使用之前存储在rgpOptions中的数据。 另见:http://en.wikipedia.org/wiki/Don%27t_repeat_yourself