Create a new project.
Go to the "Solution Explorer" window and click the icon "View Code"
We will now create a function and add two numbers. Then we will call this function inside our program.
Let's add this into the code:
Function a1()
Dim b1, b2 As Integer
b1 = 15
b2 = 30
b1 = b1 + b2
End function
- "a1" - is the name of our function. It could be called "addition" in this example or anything else.
- "b1" and "b2" - names of the variables used in our "a1" function. Both are declared as integers (whole numbers)
Go to design page and double click on the main field form. Inside the sub add this:
a1()
"a1()"
- this is how we call our created function. This call will happen right after the main form loads.
Code:
Start the program. The function does it's addition job but nothing happens. We need to display the result.Public Class Form1
Function a1()
Dim b1, b2 As Integer
b1 = 15
b2 = 30
b1 = b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a1()
End Sub
End Class
At the bottom of the a1 function we will add:
MsgBox(b1)
- MsgBox opens a small notification window with a confirmation button. MsgBox is a short version of "MessageBox.Show". So we could have used "MessageBox.Show(b1)" instead
The code will now look like this:
Start program. The result should appear in a small window.Public Class Form1
Function a1()
Dim b1, b2 As Integer
b1 = 15
b2 = 30
b1 = b1 + b2
MsgBox(b1)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a1()
End Sub
End Class
The program won't crash, but we will be receiving a warning that our function is not returning any values.
In order to make things shorter we will now erase this 2 lines:
b1 = b1 + b2
MsgBox(b1)
and replace them with:MsgBox(b1 + b2)
Same thing.
If we wanted to make this appear on the screen instead, we would have used quotation marks:
MsgBox("b1 + b2")
Also try using the "&" sign to have both:
MsgBox("b1 + b2 = " & b1 + b2)
Code:
Start program.Public Class Form1
Function a1()
Dim b1, b2 As Integer
b1 = 15
b2 = 30
MsgBox("b1 + b2 = " & b1 + b2)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a1()
End Sub
End Class
Our "a1()" requires no initial variables to begin with it's job and it always does the same thing - adds two variables with determined values. We will now proceed to create a program which can insert two variables into the "a1" function and add them together.
We will delete this line inside "a1":
Dim b1, b2 As Integer
And we will change:
Function a1()
into:
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
The "a1" is now declared with 2 starting variables. This means that now it requires 2 variables every time we call this function in our program. So, we have to go to our Form1 and change:
a1()
into:a1(0,0)
- ByVal - it's a modifier that helps us declare variables inside functions and subs. Stands for: by value.
(0,0) - "zero, zero" is just an example. We could put any numbers like (36,92). Writing it like this allows us to call this function, which we otherwise wouldn't be able (compiling error).
Code:
We still get the same result as before but now "a1" is looking for two variables.Public Class Form1
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
b1 = 15
b2 = 30
MsgBox("b1 + b2 = " & b1 + b2)
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
a1(0,0)
End Sub
End Class
We could erase: b1 = 15 and b2 = 30 and then change a1(0,0) to, for example, a1(18, 38). Then start the program. You will get 56 as the result. Function "a1" accepted 18 as a value for "b1" and 38 as a value for "b2".
Change the "a1" function so that it only has this line inside:
Return b1 + b2
The warning we had so far should disappear. What we "Return" is what is going to be the result of the "a1" function after it's called in the main form.
We have lost the notification box. Go to the sub of the main form and insert the call of the "a1" function into a message box:
MsgBox(a1(0, 0))
Code:
Start program. I hope everything makes sense.Public Class Form1
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MsgBox(a1(0,0))
End Sub
End Class
Go to the design page. Add two texboxes on your form and a label. No multilines are needed. One textbox should be above the other and the label should be below them.
Click on the label -> go to Properties window -> Text -> Change text to: 0
We should now have 2 textboxes named, by default, as: Textbox1 and Textbox2. We want to be able to take the values from those 2 textboxes (numeric values) and add them together with our "a1" function.
Go to the main form and change this line:
MsgBox(a1(0,0))
Into this:
MsgBox(a1(Textbox1.Text, TextBox2.text))
Before starting the form we must change the event of the form:
Handles MyBase.Load
into:Handles MyBase.DoubleClick
Code:
Start the program. Don't double click immediately or the program will crash. Insert numbers in the 2 textboxes then double click on the main form.Public Class Form1
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
MsgBox(a1(TextBox1.Text, TextBox2.Text))
End Sub
End Class
We will now change the program to display the result in the label itself.
Go to design tab, double click the label (should have text: 0) and open the sub for the label.
Change the event for the label to:
Handles Label1.MouseHover
Copy the message box code line from the sub of the main form into the sub of our label.
Code:
Now it's relatively easy to hover over the label with our mouse by mistake without having inserted any numbers into the 2 textboxes. Back to our main form:Public Class Form1
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
MsgBox(a1(TextBox1.Text, TextBox2.Text))
End Sub
End Class
Change this: Handles MyBase.DoubleClick back to: Handles MyBase.Load
Also we add this two lines:
TextBox1.Text = "0"
TextBox2.Text = "0"Now we have initial values for our textboxes loading right when the program starts. There shouldn't be errors hovering over the label right at the beginning.
Code:
Inserting letters or signs into the textboxes will obviously give you a program error.Public Class Form1
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"TextBox2.Text = "0"
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
MsgBox(a1(TextBox1.Text, TextBox2.Text))
End Sub
End Class
Let's do some small display changes. Go to the label sub and change this part:
MsgBox(a1(TextBox1.Text, TextBox2.Text))
into:
Label1.text = a1(TextBox1.Text, TextBox2.Text)
Final Code:
Public Class Form1
Function a1(ByVal b1 As Integer, ByVal b2 As Integer) As Integer
Return b1 + b2
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
TextBox1.Text = "0"TextBox2.Text = "0"End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label1.MouseHover
Label1.text = a1(TextBox1.Text, TextBox2.Text)
End Sub
End Class
It's very important to learn how to:
- change events of subs when necessary
- insert text into elements
- use a message box when needed
- create your own functions you can call whenever you like