We are working with Cambridge Assessment International Education towards endorsement of this title. Any references or material related to answers, grades, papers or examinations are based on the opinion of the author(s). 1 Visual Studio Community
The code window will contain only two lines of code. However, this code is important as it indicates the start and end of the code attached to the form. All additional code will be placed between these two indicators. Generally, code placed outside will not be part of the form and will cause an error. The Enter key can make additional lines.
Double clicking an object on the form to generate a subroutine can be done by mistake. This could mean that if you have accidentally double clicked on an object, your program will contain unused event subroutines. It is a common mistake to place the correct code in the wrong event subroutine. If your code does not run as expected, check you have used the correct event subroutine.
LE
In Visual Basic, user-controlled events – such as pressing the Enter key or selecting a button on a GUI – control the execution of the code. This is known as event driven programming. A common element of a Windows Forms application GUI is a button which can be selected by a user. When the button is selected it will trigger the execution of code held in a subroutine that is linked to the button.
TIP
In this first program, clicking the button on the form will trigger the execution of code held in a subroutine linked to the button that will deliver the message ‘Hello World’. Before writing code, the event subroutine linked to the button has to be created. The simplest way to create a subroutine is to double click the object on the interface you wish to use as a trigger (in this case, the button on the form). This will automatically create the code for the default subroutine attached to the object selected and show the code window.
P
Public Class Form1 P rivate Sub BTNShow_Click(sender As Object, e As EventArgs) Handles BTNShow.Click End Sub End Class
Let us examine the code to identify what each element achieves (Table 1.5).
M
Note: Although this makes use of object-oriented language and is outside of the scope of the syllabuses, it is useful to have some understanding of how the process works. Code Element Private Sub
Description
BTNShow_Click
The name of the subroutine. The automatic default is to name the routine after the object and event that will trigger the subroutine. However, it is possible to rename the subroutine.
A
The start of an individual subroutine. Private means that the subroutine is only accessible by this form.
S
(sender As Object, e As EventArgs)
The arguments, or data, that are associated with the event. As this is a button click event, the arguments are limited – either the button was clicked, or it was not. However, events associated with mouse activation, for example, will hold data about the location of the mouse on the form and which mouse button was clicked. You should not change or delete any of this data as your subroutine might not work. As you become a more advanced programmer, you will learn how you can manipulate these sections.
Handles BTNShow.Click
The event that will trigger the subroutine. In this case, clicking BTNShow will call the subroutine and execute the code it contains. It is possible to have a single subroutine triggered by multiple events.
End Sub
The end of the subroutine. All the code that is to be executed when the subroutine is called is placed between Sub and End Sub.
Table 1.5: Breakdown of code elements
17 Original material © Cambridge University Press 2021. This material is not final and is subject to further changes prior to publication.