0% found this document useful (0 votes)
44 views2 pages

VB Tuto1

This code defines event handlers for buttons on a Windows form. When the OK button is clicked, it concatenates text from a text box onto itself. When the Exit button is clicked, it closes the form. When the Message button is clicked, it displays a message box with a greeting. When the Add Values button is clicked, it converts text boxes to numbers, sums them, handles errors, and displays the result. When the Divide Values button is clicked, it divides numbers from text boxes and displays the result.

Uploaded by

Amina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
44 views2 pages

VB Tuto1

This code defines event handlers for buttons on a Windows form. When the OK button is clicked, it concatenates text from a text box onto itself. When the Exit button is clicked, it closes the form. When the Message button is clicked, it displays a message box with a greeting. When the Add Values button is clicked, it converts text boxes to numbers, sums them, handles errors, and displays the result. When the Divide Values button is clicked, it divides numbers from text boxes and displays the result.

Uploaded by

Amina
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2/28/2019

1 ' This code defines what happens with your form


2 ' We define how to handle events like a mouse click on the button
3 Public Class Form1
4  
5     ' This is a subroutine that doesn't return a value
6     ' This code runs every time the button is clicked
7     Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
8  
9         ' & is used to join or concatenate strings
10         ' txtName.Text returns the value stored in the Text Box
11         ' You can also assign values using txtName.Text
12  
13         txtName.Text = "Hello " & txtName.Text
14     End Sub
15  
16     Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
17  
18         ' Will close the program
19         Me.Close()
20  
21     End Sub
22  
23     Private Sub btnMessage_Click(sender As Object, e As EventArgs) Handles btnMessage.Click
24  
25         ' Opens a message box and says hello to the user
26         ' The text after the , is shown in the message box title bar
27         MessageBox.Show("Hello " & txtName.Text,
28                         "Hello " & txtName.Text)
29  
30     End Sub
31  
32     Private Sub btnAddValues_Click(sender As Object, e As EventArgs) Handles btnAddValues.Click
33  
34         ' If an error could occur surround the error causing code
35         ' with a Try block
36         Try
37  
38             ' Create an Integer variable and store the value
39             ' Integers can hold values -2147483648 through +2147483647
40             ' A Long holds -9,223,372,036,854,775,808 through 9,223,372,036,854,775,807
41             ' Decimal holds +/-79,228,162,514,264,337,593,543,950,335
42             ' and is precise to 29 decimal places
43             ' Doubles hold +/-1.79769313486231570E+308 but are not precise
44             ' Booleans are True or False
45             ' Use CInt() to convert a String into an Integer
46             Dim firstNum As Integer = CInt(txtAddVal1.Text)
47             Dim secondNum As Integer = CInt(txtAddVal2.Text)
48  
49             ' Sum the values
50             Dim sum = firstNum + secondNum
51  
52             ' Convert the Integer into a String
53             ' You could also use sum.ToString
54             txtSumAnswer.Text = CStr(sum)
55  
56             ' Catch the likely error to avoid crashing your program
57         Catch Exc As System.InvalidCastException
58             MessageBox.Show("Please enter numbers to sum",
59                         "Error")
60  
61             ' Print to the Output console
62             Console.WriteLine("An error occurred")
63  
64         Catch Exc As Exception
65             MessageBox.Show("An unknown error occurred",
1/2
2/28/2019
66                         "Error")
67  
68         End Try
69  
70         ' Other common cast options
71         ' CBool : Convert to a Boolean
72         ' CDbl : Convert to Double
73         ' CDec : Convert to Decimal
74         ' CLng : Convert to Long
75  
76  
77     End Sub
78  
79     Private Sub btnDivideValues_Click(sender As Object, e As EventArgs) Handles btnDivideValues.Cli
80  
81         ' Create Decimals and convert from String
82         Dim firstNum As Decimal = CDec(txtDivideVal1.Text)
83         Dim secondNum As Decimal = CDec(txtDivideVal2.Text)
84  
85         ' Divide Decimals and then convert the answer into an Integer
86         Dim intSolution As Integer = CType(firstNum / secondNum, Integer)
87  
88         txtDivisionAnswer.Text = CStr(intSolution)
89  
90     End Sub
91 End Class

2/2

You might also like