Monday, July 11, 2022

Scientific Calculator

To get familiar with VCL form application development, let's create a simple scientific calculator. This example is based on How to Create a Calculator with Pascal in Dephi by DJ Oamen.

Calculator User Interface


The TButton control in Delphi Pascal does not allow the color to be changed. The buttons in the UI shown above are TPanel controls with MouseEnter and MouseLeave event handlers to change the panel color to a lighter gray to simulate a button.

procedure TForm2.PanelMouseEnter(Sender: TObject);
var
  ThePanel: TPanel;
begin
  ThePanel := Sender as TPanel;
  ThePanel.Color := clBtnShadow;
end;
procedure TForm2.PanelMouseLeave(Sender: TObject);
var
  ThePanel: TPanel;
begin
  ThePanel := Sender as TPanel;
  ThePanel.color := cl3DDkShadow;
end;

Calculator Button Handlers

Number Buttons

Typical click handler ('7' button)

procedure TForm2.btn7Click(Sender: TObject);
begin
if txtResult.Text = '0.0' then
  txtResult.Text := '7'
else
  txtResult.Text := txtResult.Text + '7';
end;

Math Operators Buttons

Typical click handler (+ button)

procedure TForm2.btnAddClick(Sender: TObject);
begin
  num1 :=  txtResult.Text;
  oper := '+';
  txtResult.Text := '';
end;

Equals Buton Handler

procedure TForm2.btnEqClick(Sender: TObject);
begin
  num2 := txtResult.Text;
  if oper = '+' then
    result := FloatToStr(StrToFloat(num1) + StrToFloat(num2));
    txtResult.Text := result;
  if oper = '-' then
    result := FloatToStr(StrToFloat(num1) - StrToFloat(num2));
    txtResult.Text := result;
  if oper = '*' then
    result := FloatToStr(StrToFloat(num1) * StrToFloat(num2));
    txtResult.Text := result;
  if oper = '/' then
    result := FloatToStr(StrToFloat(num1) / StrToFloat(num2));
    txtResult.Text := result;
end;

C and CE Button Handlers

procedure TForm2.btnCClick(Sender: TObject);
begin
    txtResult.Text := '0.0';
end;

procedure TForm2.btnCEClick(Sender: TObject);
var f, s: String;
begin
  txtResult.Text := '0.0';

  f := num1;
  s := num2;

  f := '';
  s := '';
end;

PI Button Handler

procedure TForm2.btnPiClick(Sender: TObject);
begin
   txtResult.Text := '3.14159265';
end;

Function Button Handlers

Typical function click handler (Sin button)

procedure TForm2.btnSinClick(Sender: TObject);
begin
  txtResult.Text := FloatToStr(Sin(StrToFloat(txtResult.Text)));
end;

Project Setup

Download and install the Delphi Pascal IDE. A free license is provided for the community edition.

Run the program and select "Create a new Windows VCL Application - Delphi" from the Welcome Page or select File > New > Windows Windows VCL Application - Delphi.

Hint: VCL is a the traditional desktop development library for Windows computers and is similar to WinForms in VisualStudio. Delphi supports a newer cross-platform windowing system called FMX. For this tutorial, I will use VCL.


In the projects windows, right click on "Target Platforms (Windows 32-bit)" > Add Platform > Ok to select Windows 64-bit (assuming you are woring on a 64-bit computer). Also, select "Release" in the Build Configuration for faster build times and small executable code. We will use Debug as needed to resolve development "bugs".

Save the Project Group, Project, and Source Files

Create a project directory such as Desktop > EngineeringTools-Pascal. Create a project directory called Desktop > EngineeringTools-Pascal > HelloPascalWindow. Select File > Save All. Save the .pas file as HelloWindow.pas in the HelloPascalWindow project directory. Save the project as HelloWorldProject.dproj in the HelloPascalWindow project directory. Right click on the project group name in the Projects window and select Save Project Group As... Save the project group as HelloWindowProject in the EngineeringTools-Pascal directory. The project directories should look like this:



Project Organization

  • Project Group - Collection of projects (similar to Solution in Visual Studio)
  • Project - Collection of pascal source code files
  • *.pas - Pascal source code file
  • *.dfm - Form source code file
Pallete window contains the graphical library of controls that can be used on the VCL form.
Object Inspector window provides access to control properties and events.
Structure window provides a list of controls.

Project Compilation
Select the Form, in the object inspector change the Caption to Hello Pascal Window. Select Run > Run Without Debugger or click on the green arrow in the Dephi toolbar. The project is compiled and the following window is displayed:


Congradulations! You created your first pascal window. Close the window by clicking on the close icon in the upper right.  

The executable for this project can be found in the Desktop\EngineeringTools-Pascal\HelloPascalWindow\Win64\Release directory. Run the executable by double clidking on it to see the bare window from a standalone executable.





Why pascal?

Modern "Object Pascal" is a cool language for desktop application development with its beginnings in Turbo Pascal of the 1980's. The executable code is self contained, small byte footprint, does not require .NET to be installed on the computer, and is highly portable. The language supports object oriented programming (OOP) or procedural programming.

I suggest using Dephi Pascal Community Edition for student and hobby programming since it is a free fully functional IDE. Note that the community edition can only support one language at a time so you have to uninstall C++ to use Pascal. Dephi supports cross-platform development on Windows, MacOS, Android, and IOS.

Let's give it a try!



Scientific Calculator

To get familiar with VCL form application development, let's create a simple scientific calculator. This example is based on  How to Cre...