Sub routine in visual basic


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.


Design a new project and name it colour.

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


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


End Sub

Just one word

Your code window should now look like this






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


We are going to look at more on functions later.