问题
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!
回答1:
Try this:
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;
procedure btnClick(Sender: TObject);
private
{private declarations}
Amount: Integer;
public
{public declarations}
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.btnClick(Sender: TObject);
begin
Inc(Amount, TButton(Sender).Tag);
lblOutput.Caption := Format('The total amount raised so far is $%d', [Amount]);
end;
end.
Then all you have to do is assign btnClick()
as the OnClick
event handler for all three buttons, and set each button's Tag
property to the desired amount.
回答2:
Your application has to
contain 3 buttons with different values (let them name
Value10_Button
,Value20_Button
andValue30_Button
)contain a label with the current amount (let it name
Amount_Label
)RaiseAmount
if one of the value button is pressedUpdateAmount_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 Tag
property 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;
回答3:
You need to
- Add a way to count the total value (hint field).
- Add event handlers for the buttons, they need to update the value and show the total.
If you don't know how to do that, check your textbook.
来源:https://stackoverflow.com/questions/21721799/delphi-adding-digets