Wednesday, August 4, 2010

Make Simple Calculator In Delphi

OK, now I'll show you how to make simple calculator in Delphi (including Addition, Subtraction, Multiplication, Division).

1. Open Your Delphi (Delphi 7).

2. Click File -> New -> Application to create new application.

3. Input component into Form (in standard component palette) below :

1 Combo Box
2 Edit
1 Button
1 Label

4. Click ComboBox 1, and Click Properties (in Object Inspector) find Text and let it empty. Do the same step for Edit1 and Edit2.

5. Double-Click your form adn type syntax below :

procedure TForm1.FormCreate(Sender: TObject);
begin
//Adding Operator to Combo Box
ComboBox1.Items.Add('+');
ComboBox1.Items.Add('-');
ComboBox1.Items.Add('x');
ComboBox1.Items.Add('/');
end;

end.

6. Above syntax is for adding operator to combo box.

7. OK, now double click Button1. Then type syntax below :

procedure TForm1.Button1Click(Sender: TObject);
var
number1, number2 : integer;
begin
number1 := StrToInt(Edit1.Text);
number2 := StrToInt(Edit2.Text);
if ComboBox1.ItemIndex=0 then
Label1.Caption := IntToStr(number1+number2)
else if ComboBox1.ItemIndex=1 then
Label1.Caption := IntToStr(number1-number2)
else if ComboBox1.ItemIndex=2 then
Label1.Caption := IntToStr(number1*number2)
else if ComboBox1.ItemIndex=3 then
Label1.Caption := FloatToStr(number1/number2);
end;

end.

8. Yuph test your coding with press F9. Try to fill number and choose operator and then click Button. Woozaahhh, you will get the result in Label1.

Note : If you want to change the name of Button1, Click Button1, then in Properties Object Inspector, Find Caption and change.

No comments:

Post a Comment