This is a blog that discusses anything related to Visual Basic and provides updates for VISUAL BASIC TUTORIAL website. Your comments and suggestions are welcome here. Thank you.
Sunday, July 26, 2009
New lesson for Excel VBA Tutorial
Sunday, July 19, 2009
The Trigonometric Functions
I have just Updated lesson 11 with some trigonometric functions.
The common trigonometric functions are Sin, Cos, Tan and Atn.
a) Sin is the function that computes the value of sine of an angle in radian.
b) Cos is the function that computes the value of cosine of an angle in radian.
c) Tan is the function that computes the value of tangent of an angle in radian.
d) Atn is the function that computes the value of arc tangent of an angle in radian.
An angle in degree has to be converted to radian before it can be calculated by the above trigonometric functions. From high school mathematics, we know that π radian is equivalent to 180°; which means 1 radian is equivalent to π divided by 180. Therefore, in order to convert an angle x from degree to radian, we have to multiply x by (π/180). However, there is a small problem because it is rather difficult to obtain the precise value of π, fortunately, there is a way to do it in VB. First of all, we know that an arc tangent of 1 will return the value of 45° which is π/4 radian. So, to obtain the value of π, just multiply the arc tangent of 1 with 4. Let’s examine how all the above calculations can be done in the following example:
In this example, the program will display the values of sine, cosine and tangent for various angles in degree between 0° and 360° in a table form . The value of π is obtained using the equation pi=4*Atn(1). The angle in degree is converted to radian by multiplying the angle by (π/180). Different angles are obtained through the use of For...Next Loop. The program is shown below
Private Sub Form_Activate ()
pi = 4 * Atn(1)
Print "angle", "Sin x", "Cos x", "Tan x"
For degree = 0 To 360 Step 30
angle = degree * (pi / 180)
Print degree, Round(Sin(angle), 4), Round(Cos(angle), 4), Round(Tan(angle), 4)
Next degree
End Sub
The Output
Saturday, July 11, 2009
Scientific Calculator
I 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 Interface:
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
Friday, July 3, 2009
New Project-Creating a Scientic Calculator
Right now, take a look at the simple interface.
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