Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
321 views
in Technique[技术] by (71.8m points)

Delphi, adding digets

My textbook says I need to make a donation program with three buttons, each representing a certain amount of money. The "Amount donated" should be shown on a label after I press a button.

For instance if I press the $10 button, it will say The total amount raised so far is $10. And then if I press the $50 button, the label should caption The total amount raised so far is $60. I'm in need of 3 buttons, a $10, $20 and $50.

I don't even know where to start with the coding side!

Here's the starting code:

unit AIDSAWARENESS_U;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    btn10: TButton;
    btn50: TButton;
    btn20: TButton;
    lblOutput: TLabel;
  private
   {private declarations}
  public
   {public declarations}
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}


end.

If you wouldn't mind helping me, it would be much appreciated!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your application has to

  • contain 3 buttons with different values (let them name Value10_Button, Value20_Button and Value30_Button)

  • contain a label with the current amount (let it name Amount_Label)

  • RaiseAmount if one of the value button is pressed

  • UpdateAmount_Label if the amount value has changed

Following this we got straight to

type
  TForm1 = class( TForm )
    Value10_Button : TButton;
    Value20_Button : TButton;
    Value50_Button : TButton;
    Amount_Label : TLabel;
    procedure Value10_ButtonClick( Sender : TObject );
    procedure Value20_ButtonClick( Sender : TObject );
    procedure Value50_ButtonClick( Sender : TObject );
  private
    FAmount : Currency;
    procedure SetAmount( const Value : Currency );
    procedure UpdateAmount_Label;
    procedure RaiseAmount( const Value : Currency );
  public
    property Amount : Currency read FAmount write SetAmount;
  end;

procedure TForm1.RaiseAmount( const Value : Currency );
begin
  Amount := Amount + Value;
end;

procedure TForm1.SetAmount( const Value : Currency );
begin
  if FAmount <> Value then
  begin
    FAmount := Value;
    UpdateAmount_Label;
  end;
end;

procedure TForm1.UpdateAmount_Label;
begin
  Amount_Label.Caption := Format( 'The total amount raised so far is $%f', [Amount] );
end;

procedure TForm1.Value10_ButtonClick( Sender : TObject );
begin
  RaiseAmount( 10 );
end;

procedure TForm1.Value20_ButtonClick( Sender : TObject );
begin
  RaiseAmount( 20 );
end;

procedure TForm1.Value50_ButtonClick( Sender : TObject );
begin
  RaiseAmount( 50 );
end;

As suggested by Remy you can use the Tagproperty of the Buttons, but I prefer to use them in a different way

procedure TForm1.Value_ButtonClick( Sender : TObject );
begin
  case ( Sender as TComponent ).Tag of
    1 : RaiseAmount( 10 );
    2 : RaiseAmount( 20 );
    3 : RaiseAmount( 30 );
  end;
end;

In a real application I would have a lookup table to get the values from the tag property to get rid of that magic numbers

procedure TForm1.Value_ButtonClick( Sender : TObject );
begin
  RaiseAmount( GetAmountFromIndex( ( Sender as TComponent ).Tag ) );
end;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...