## VB Project 1: Scientific Calculator

We intend to start a series of VB projects so that together we will make this tutorial website more interesting learning platform for all VB hobbyists in the world. We will first post the ideas of any project in our blog( http://vbtutorblog.blogspot.com/) and then gradually well work through the steps until it is completed. We wish that everyone will contribute your ides so that we can make better and more efficient VB programs.

Our first project is a scientific calculator. We have entered the second phase of the project and the details were posted in the vbtutor blog.

Phase 1

Creating a scientific calculator is not an easy task in Visual Basic 6. However, you don't have to worry. we will start with a simple interface and gradually we will progress to a more complex interface, and then a fully functional scientific calculator. So, please follow our tutorial or our blog as often as possible for updates on this project. Meanwhile, if you have any idea about the calculator, please share with us. The Interface for phase 1.

The code:

Private Sub Command1_Click(index As Integer)
Select Case index
Dim x As Double
Case 0
x = Val(Text1.Text)
Text1.Text = Round((Sin(x * 3.14159265 / 180)), 4)
Case 1
x = Val(Text1.Text)
Text1.Text = Round((Cos(x * 3.14159265 / 180)), 4)
Case 2
x = Val(Text1.Text)
Text1.Text = Round((Tan(x * 3.14159265 / 180)), 4)

End Select

End Sub

*In Visual Basic, we have to convert degree to radian before we can compute the values of the trigonometric functions. The conversion is based on the formula pi radian=180o , therefore 1 degree=pi/180.

 Phase 2 Now, we shall add the number buttons, a cancel button and a dot button. The number buttons are created as an array of buttons, and each button will be identified with its index, corresponding to its caption. We will also replace the display panel with a label so that the users won't be able to erase the number. They can only clear the number by clicking the cancel button. The alignment property of the label is set to right justified, so that the digit entered will start from the right position, similar to the conventional calculator. The value of pi is now obtained by using the Atn function (Arc Tangent). We know that Tan(pi/4)=1, so Atn(1)=pi/4, therefore pi=Atn(1)*4. By using this function, we will obtain a more accurate value of pi. In this project, we shall also limit the numbers to be less than 30 digit. If any user enters a number with more than 30 digits, an error message "data overflow" will be shown.

The Code:

Dim Num_of_digit As Integer
Private Sub buttonNum_Click(Index As Integer)
If Num_of_digit > 0 And Num_of_digit <> "0" Then
panel.Caption = panel.Caption + Right\$(Str\$(Index), 1)
ElseIf panel.Caption = "0" Then
panel.Caption = Str\$(Index)
End If

ElseIf Num_of_digit >= 30 Then
panel.Caption = "Data Overflow"

Else

panel.Caption = Right\$(Str\$(Index), 1)

End If

Num_of_digit = Num_of_digit + 1

End Sub

Private Sub Clear_Click()

panel.Caption = "0"
Num_of_digit = 0

End Sub

Private Sub Command1_Click(Index As Integer)
Dim x As Double
Dim pi As Double
pi = Atn(1) * 4
Select Case Index

Case 0
x = Val(panel.Caption)
panel.Caption = Round((Sin(x * pi / 180)), 4)
Num_of_digit = 0

Case 1
x = Val(panel.Caption)
panel.Caption = Round((Cos(x * pi / 180)), 4)
Num_of_digit = 0
Case 2
x = Val(panel.Caption)
panel.Caption = Round((Tan(x * pi / 180)), 4)
Num_of_digit = 0

End Select

End Sub