On completion of this tutorial, you will be able to:
- Recite some examples of events that occur in VB
- Go into the Code Window
- Select Class Names and Method Names from the code window
- Expand and collapse code segments
- Change basic properties from code
- Place remarks in your code
- Execute basic methods from code
- Navigate around the code window using the keyboard and bookmarks
In VB nothing can happen with your applications until you write some code. Code will bring your applications to life. In VB, code is usually written in response to an event that happens to an object, an event could be a mouse click or a form being loaded. There are two types of event method:
- System Triggered Event Methods (e.g. Load event for a form)
- User Triggered Event Methods (e.g. Click event for a button)
Example Events in VB

Create Code for Event Methods
Before you create code for an event, you need to determine which object the code will refer to and then which event you are interested in.
To add code to an object
- Double-click on the object
OR
- Click on any class/object in the Solution Explorer, if there is code associated with this class/object then you can click on the View Code
button at the top of the Solution Explorer
OR
- From the View menu, select Code (SpeedKey: F7)
The Code Window
The Code Window is where all code is written. Code can seem very confusing when you first look at it, so there are a few things in the Code window that can help you.

To view the Code Window
- Double-click on any control drawn in a Windows Form class
This will open the code window and create some code stubs for you to type code between (Private Sub… End Sub). You can then type the code associated with that object.
OR
- Click on any class/object in the Solution Explorer
- If there is code associated with this class/object then you can click on the View Code
button at the top of the Solution Explorer
Colour coding
Different sections of code are coloured to make it easy to decide which pieces of code are reserved command words and which are not.
Automatically generated code
By default, on Windows Forms VB.NET will hide most of the code it creates for you automatically. This automatic code is created within the #Region and the #End Region stubs. It is not a good idea to tamper or change this code, when you change the position or properties of a control on a form then this code will change automatically.
To expand and collapse code segments
It is possible to collapse segments of code to make it easier to read. On the left-hand side of the code window are small grey plus and minus signs. Clicking on these will expand or collapse a section of code.
On the right-hand side of a collapsed code section will be some ellipses (...) this is a further indication that there is more code related to the current line that is hidden.
The Class Name combo box

On the left-hand side of the code window (at the top) you can see the Class Name combo box. This contains a list of all the available classes within the class/object that you are working on. For example: A form class would appear if you were working in a Windows Form, along with any controls that you had drawn on the form class.
The Method Name combo box

The code you will write is called a Method Procedure. The procedure's name consists of the name of the class (in the above example it is the ListBox1) followed by an underscore and then the name of the method (in the above case it is the SelectedIndexChanged event method).
You write the code underneath the Private Sub statement and before the End Sub statement.
The Class Stubs
On any kind of code class there will be a start class stub (e.g. Public Class Form1) and an end class stub (e.g. End Class). All code associated with a class (e.g. a form class) needs to go within these two stubs, if it does not then an error may occur in your code.