visual Basic 2010 Lesson 5-Writing the Code

[Lesson 4]<< [CONTENTS] >> [Lesson 6]

In preceding lessons, you have learned some basic concepts of  Visual Basic 2010 programming. In this lesson, we learn to write actual programs. We will keep the theories short so that it would not be too difficult for you.

5.1 The event Procedure

Visual Basic 2010 is an object-oriented and also a event-driven programming language. In fact, all windows applications are event-driven. Event-driven means the user decides what actions to take, like clicking a command button or entering text in a text box. An event is related to an object, it is an incident that happens to the object due to the action of the user, such as clicking the mouse or depressing a key on the keyboard. A class has events as it creates an instant of a class or an object.



When we launch Visual Basic 2010, we will see a default form with the Form1 appears in its IDE, it is actually the Form1 Class that inherits from the Form class System.Windows.Forms.Form, as shown in the Form1 properties windows.
Visual Basic 2010
Figure 5.1

When we click on any part of the form, we will see the code window as shown below. The is the structure of an event procedure. In this case, the event procedure is to load Form1 and it starts with Private Sub and ends with End Sub. This procedure includes the Form1 class and the event Load, and they are bound together with an underscore, i.e. Form_Load. It does nothing other than loading an empty form. You don’t have to worry the rest of the stuff at the moment, they will be explained in later lessons.

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class

The are other events associated with the Form1 class, such as click, cursorChanged, DoubleClick, DragDrop, Enter and more as shown in the diagram below (It appears when you click on the upper right pane of the code window)

Visual Basic 2010
Figure 5.2

5.2 Writing the code

Now you are ready to write the code for the event procedure so that it will do something more than loading a blank form. The code must be entered between Private Sub…….End Sub. Let’s enter the following code:

Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
 Me.Text = "My First VB2010 Program"
 Me.ForeColor = Color.ForestGreen
 Me.BackColor = Color.Cyan
End Sub
End Classs

The first line of the code will change the title of the form to My First Visual Basic 2010 Program, the second line will change the foreground object to Forest Green( in this case, it is a label that you insert into the form and change its name to Foreground) and the last line changes the background to Cyan color.

The equal (=)in the code actually is used to assign something to the object, like assigning a yellow color to the foreground of the Form1 object (or an instance of Form1). Me is the name given to the Form1 class. We can also call those lines as Statements. So, the actions of the program will depend on the statements entered by the programmer.

The output is shown in Figure 5.1


Figure 5.1



Here is another example.

Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim name1, name2, name3 As String
 name1 = "John"
 name2 = "Georges"
 name3 = "Ali"
 MsgBox(" The names are " & name1 & " , " & name2 & " and " & name3)
End Sub

In this example, you insert one command button into the form and rename its caption as Show Hidden Names. The keyword Dim is to declare variables name1, name2 and name3 as a string, which means they can only handle text. The function MsgBox is to display the names in a message box that are joined together by the “&” signs. The output is shown in Figure 5.4

Visual Basic 2010
Figure 5.4





[Lesson 4]<< [CONTENTS] >> [Lesson 6]