Sub routine in visual basic

SUB ( A SHORT FORM OF SUBROUTINE)

Remember subs are just like functions only that they do not return a value.



Look at this example.

Me.BackColor = Color.Blue

This code changes the colour of a form to blue.

The me referring to the form and backcour reffering to its colour

Which is assigned color blue after equals sign. We can place this code inside a sub and then  call it in a normal way, the way we would call a function.

Exercise.

Design a new project and name it colour.

After the form loads add a button to and change its text property to change colour.

sub1

You should get something like this one above.

Now double click on the button and enter the code below just after public class.

Public Class Form1

Private Sub colour()

Me.BackColor = Color.Blue

End Sub

Run your project and click on the button. Did anything happen, the answer is no.

We have to call our sub inside the button so if your code window is still open add the code below.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

colour()

End Sub

Just one word

Your code window should now look like this

sub2

 

 

 

 

Run your project now it should work and this is the output.

sub3

We are going to look at more on functions later.