Monday, September 13, 2010

A Calculator Project in VB NET

Page : 1 2 3 4 5 6 7 8 9 10 11 12 13 14

In the next few pages, you're going to create a Calculator. It won't be a very sophisticated calculator, and the only thing it can do is add up. What the project will give you is more confidence in using variables, and shifting values from one control to another. So create a new project, call it Calculator, and let's get started. Designing the Form

Let's design the form first. What does a calculator need? Well numbers, for one. A display area for the result. A plus sign button, an equals sign button, and a clear the display button. Here's how our calculator is going to work. We'll have 10 button for the numbers 0 to 9. When a button is clicked its value will be transferred to a display area, which will be a Textbox. Once a number is transferred to the Textbox we can click on the Plus button. Then we need to click back on another number. To get the answer, we'll click on the equals sign. To clear the display, we'll have a Clear button.
If you haven't already, create a new project. Save it as Calculator. To your new form, first add ten Buttons (You can add one, then copy and paste the rest). The Buttons should have the following Properties:

Name: btn Plus a Number (btnOne, btnTwo, btnThree, etc)

Text: A number from 0 to 9. A different one for each button, obviously

Next, add a Textbox. Set the following properties for the Textbox:
Textbox Name: txtDisplayFont: MS Sans Serif, Bold, 14Text: Erase the default, Textbox1, and leave it blank

Three more Command buttons need to be added

Plus Button Name cmdPlusFont MS Sans Serif, Bold, 14Text +

Equals Button Name cmdEqualsFont MS Sans Serif, Bold, 14Text =

Clear ButtonName cmdClearFont MS Sans Serif, Bold, 14Text Clear

When your form design is finished, it might look something like this




Calculator Project in VB.NET

Page : 1 2 3 4 5 6 7 8 9 10 11 12 13 14

Source Code for Calculator Project


Public Class Form1
Dim a As Double
Dim b As Double
Dim c As Double
Dim Opt As String



Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
T1.Text += Button1.Text
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
T1.Text += Button2.Text
End Sub

Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
T1.Text += Button4.Text
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
T1.Text += Button3.Text
End Sub

Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click
T1.Text += Button6.Text
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
T1.Text += Button5.Text
End Sub


Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
T1.Text += Button10.Text
End Sub

Private Sub Button9_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button9.Click
T1.Text += Button9.Text
End Sub

Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
T1.Text += Button8.Text
End Sub

Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button7.Click
T1.Text += Button7.Text
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub


Private Sub EqualButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EqualButt.Click
b = Val(T1.Text)
If Opt = "/" Then
T1.Text = a / b
ElseIf Opt = "+" Then
T1.Text = a + b
ElseIf Opt = "-" Then
T1.Text = a - b
ElseIf Opt = "*" Then
T1.Text = a * b
ElseIf Opt = "%" Then
T1.Text = a Mod b
End If
End Sub

Private Sub PlusButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PlusButt.Click
a = Val(T1.Text)
Opt = PlusButt.Text
T1.Text = ""
End Sub

Private Sub MinusButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MinusButt.Click
a = Val(T1.Text)
T1.Text = ""
Opt = MinusButt.Text
End Sub

Private Sub CEButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CEButt.Click
T1.Text = " "
End Sub

Private Sub CButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CButt.Click
T1.Text = " "
End Sub

Private Sub DivButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DivButt.Click
a = Val(T1.Text)
T1.Text = ""
Opt = DivButt.Text

End Sub

Private Sub DotButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DotButt.Click
T1.Text += DotButt.Text
End Sub

Private Sub ModButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModButt.Click
a = Val(T1.Text)
T1.Text = ""
Opt = ModButt.Text
End Sub

Private Sub StarButt_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StarButt.Click
a = Val(T1.Text)
T1.Text = ""
Opt = StarButt.Text
End Sub


End Class