1
DESIGNING A CALCLUATOR
To be Presented On 5TH March, At 2PM
Step 1: Open Visual Studio
Step 2: Create a New Project
1. Open Visual Studio.
2. Click on "File" > "New" > "Project..."(Use Visual Basic)
3. Select "Windows Forms App (.NET Core)" as the project template.
4. Choose a name and location for your project (e.g., "CalculatorApp").
5. Click "Create."
Step 3: Design the Calculator GUI
In the Form Designer, create the graphical user interface for your calculator. You can add
buttons for numbers (0-9), arithmetic operations (+, -, *, /), an equals button, a clear button,
and a display textbox.
Here's a simple layout suggestion:
• Add a Textbox control for input and display (Name it "Display Textbox").
• Add Button controls for numbers (0-9) and operators (+, -, *, /).
• Add a Button for the equals operation (=).
• Add a Button for clearing the input (e.g., "C" for Clear).
Step 4: Write the Code
Write the code to handle the button clicks and perform calculations. Double-click on the
buttons and controls in the designer to create event handlers.
COMPUTER PROGRAMMING MR.FELKIN MASHA
2
DESIGNING A CALCLUATOR
To be Presented On 5TH March, At 2PM
Here's the code for the calculator:
Public Class CalculatorForm
Dim firstNumber As Double
Dim operatorSelected As String
Dim secondNumber As Double
Dim result As Double
Private Sub NumericButton_Click(sender As Object, e As EventArgs) Handles
Button0.Click, Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click,
Button6.Click, Button7.Click, Button8.Click, Button9.Click
Dim button As Button = DirectCast(sender, Button)
DisplayTextBox.Text &= button.Text
End Sub
Private Sub OperatorButton_Click(sender As Object, e As EventArgs) Handles
PlusButton.Click, MinusButton.Click, MultiplyButton.Click, DivideButton.Click
Dim button As Button = DirectCast(sender, Button)
operatorSelected = button.Text
If Double.TryParse(DisplayTextBox.Text, firstNumber) Then
DisplayTextBox.Clear()
Else
COMPUTER PROGRAMMING MR.FELKIN MASHA
3
DESIGNING A CALCLUATOR
To be Presented On 5TH March, At 2PM
DisplayTextBox.Text = "Invalid input"
End If
End Sub
Private Sub EqualsButton_Click(sender As Object, e As EventArgs) Handles
EqualsButton.Click
If Double.TryParse(DisplayTextBox.Text, secondNumber) Then
Select Case operatorSelected
Case "+"
result = firstNumber + secondNumber
Case "-"
result = firstNumber - secondNumber
Case "*"
result = firstNumber * secondNumber
Case "/"
If secondNumber = 0 Then
DisplayTextBox.Text = "Error"
Exit Sub
Else
result = firstNumber / secondNumber
COMPUTER PROGRAMMING MR.FELKIN MASHA
4
DESIGNING A CALCLUATOR
To be Presented On 5TH March, At 2PM
End If
End Select
DisplayTextBox.Text = result.ToString()
Else
DisplayTextBox.Text = "Invalid input"
End If
End Sub
Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles
ClearButton.Click
DisplayTextBox.Clear()
firstNumber = 0
secondNumber = 0
result = 0
operatorSelected = ""
End Sub
End Class
COMPUTER PROGRAMMING MR.FELKIN MASHA
5
DESIGNING A CALCLUATOR
To be Presented On 5TH March, At 2PM
Step 5: Build and Run
• Build your project by clicking "Build" > "Build Solution."
• Run the application by clicking "Debug" > "Start Debugging" or pressing F5.
Now, you have a detailed calculator application in Visual Basic. It supports basic arithmetic
operations and provides error handling for division by zero and invalid input. You can further
customize and enhance it as needed.
Step 6: Add a Picture to the Background
1. Locate or create the image you want to use as the background for your calculator.
Make sure the image is in a format that Windows Forms can handle, such as JPEG,
PNG, or BMP.
2. In Visual Studio, go to the Solution Explorer on the right side of the window.
3. Right-click on your project (e.g., "CalculatorApp") and select "Add" > "Existing
Item."
4. Browse to the location of your background image and select it. Click "Add" to add the
image to your project.
5. In the Solution Explorer, right-click on your project again and select "Properties."
6. In the Project Properties window, go to the "Resources" tab.
7. Click the "Add Existing File..." button and select your image. This will add the image
to your project's resources.
8. Close the Project Properties window.
COMPUTER PROGRAMMING MR.FELKIN MASHA
6
DESIGNING A CALCLUATOR
To be Presented On 5TH March, At 2PM
Step 7: Set the Background Image
Now that you've added the image to your project's resources, you can set it as the background
for your calculator form.
1. In the Form Designer, select the main form of your calculator (CalculatorForm).
2. In the Properties window, find the "BackgroundImage" property.
3. Click the drop-down arrow next to the "BackgroundImage" property.
4. In the drop-down menu, you should see your added image under the "Project
resources." Select the image from the list.
5. The selected image will now be set as the background for your calculator form.
6. You can also adjust the "BackgroundImageLayout" property to control how the image
is displayed (e.g., stretch, center, tile).
Step 8: Build and Run
Once you've added the picture as the background, build your project by clicking "Build" >
"Build Solution," and then run the application as described in the previous steps.
Your calculator application should now have the selected image as its background. You can
adjust the properties of the background image to achieve the desired look and feel for your
calculator application.
COMPUTER PROGRAMMING MR.FELKIN MASHA