Vb2012me Preview PDF
Vb2012me Preview PDF
Dr.Liew
Disclaimer
Visual Basic 2012 Made Easy is an independent publication and is not affiliated
with, nor has it been authorized, sponsored, or otherwise approved by Microsoft
Corporation.
Trademarks
Microsoft, Visual Basic, Excel and Windows are either registered trademarks or
Liability
The purpose of this book is to provide basic guides for people interested in Visual
Basic 2012 programming. Although every effort and care has been taken to make
The information as accurate as possible, the author shall not be liable for any error,
Harm or damage arising from using the instructions given in this book.
All rights reserved. No Part of this e-book may be reproduced, in any form or by any
means, without
Table of Contents
Example 4.1 29
4.2 Label 31
Example 4.2 31
5.2 Loading an Image in a Picture Box using Open File Dialog Control 45
Example 6.1 52
Example 6.2 53
Example 6.3 54
Example 6.4 55
Example 6.5 56
Example 7.1 60
Example 7.2 61
Example 7.3 61
Example 7.4 62
Example 8.2 66
Example 9.1 68
Example 9.2 69
Example 9.3 71
Example 9.4 72
Example 9.5 73
Example 9.4 74
Example 10.1 79
Example 10.2 80
Example 10.3 81
Example 11.2 86
Example 11.3 87
Example 11.4 87
Example 12.1 a 90
Example 12.1b 90
Example 12.1c 90
Example 12.1d 91
12.2 Do Loop 91
Example 12.2(a) 92
Example 12.2(b) 92
Example 12.3 94
Example 13.1 95
Table 16.4 Formatting Date and time using user-defined formats 119
Example 20.8 : Drawing a pie that starts with 0 degree and sweep clockwise through 60
degree. 165
https://www.visualstudio.com/vs/older-downloads/
When you launch Visual Studio 2012 Express, you will be presented with the start
page, as shown in Figure 1.1
The Visual Studio 2012 start page comprises three sections, the start page, the
output section and the solution explorer. In the start page, you can either start a new
project, open a project or open a recent project. You can also check for the latest
news in Visual Studio 2012 Express for Windows Desktop. The start page also
consists of a menu bar and a tool bar where you can perform various tasks by
clicking the menu items.
To start a new Visual Studio Express 2012 project, click on New Project under the
Start section to launch the Visual Studio 2012 New Project page as shown in Figure
1.2. You can also choose to open a recent project:
The New Project Page comprises three templates, Visual Basic, Visual C# and
Visual C++. Since we are going to learn Visual Basic 2012, we shall select Visual
Basic. Visual Basic 2012 offers you four types of projects that you can create; they
are Windows Forms Application, WPF Application, Console Application and Class
Library. Since we are going to learn how to create windows desktop applications, we
shall select Windows Forms Application.
At the bottom of this dialog box, you can change the default project name
WindowsApplication1 to some other name you like, for example, ‘ My First VB2012
Program’. After renaming the project, click OK to launch the Visual Basic Express
2012 IDE, as shown in Figure 1.3. Visual Basic Express 2012 IDE comprises a few
windows, the Form window, the Solution Explorer window and the Properties
window. It also consists of a toolbox which contains many useful controls that allows
a programmer to develop his or her Visual Basic 2012 programs.
The Toolbox is not shown until you click on the Toolbox tab. When you click on the
Toolbox tab or use the shortcut keys Ctrl+w+x, the common controls Toolbox will
appear, as shown in Figure 1.4.
Figure 1.4: Visual Basic 2012 Express Tool Box
Now, we shall proceed to show you how to create your first program. First, change
the text of the form to My First VB2012 Program in the properties window; it will
appear as the title of the program. Next, insert a button and change its text to OK.
The design interface is shown in Figure 1.5
Now click on the OK button to bring up the code window and enter the following
statement between Private Sub and End Sub procedure, as shown in Figure 1.6:
Summary
● In section 1.1, you have learned about the evolution of Visual Basic.
● In section 1.2, you have learned how to launch Visual Basic Studio Express 2012
the start page
● In section 1.3, you have learned how to launch the new project dialog and the
Visual Basic Express 2012 IDE. You have also learned how to write your first
program.
Chapter 2
Designing the Interface
❖ Learning to Design the Interface
❖ Adding controls
❖ Setting Control Properties
When you start a new Visual Basic 2012 project, the IDE will display the default form
along with the Solution Explorer window and the Properties window for the form .
The name of the default form is Form1. The properties window displays all properties
related to Form1 and their corresponding attributes or values. You can change the
name of the form, the title of the form, the background colour, the foreground colour,
the size and more. Try changing the following properties:
Property Value
Name MyForm
Text My First VB2012 Program
BackColor Blue
MaximizeBox False
The output interface is shown in Figure 2.1. Notice that the title has been changed
from Form1 to My First Program, background changed to blue colour and the
window cannot be maximized.
Figure 2.1
You can also change the properties of the form at run-time by writing the relevant
codes. The default form is an object and an instant of the form can be denoted by
the name Me. The property of the object can be defined by specifying the object’s
name followed by a dot or period:
ObjectName.property
For example, we can set the background of the form to blue using the following code
Me.BackColor=Color.Blue
To achieve the same interface as shown in Figure 2.1, type in the following code by
clicking the form to enter the code window:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Me.Text = "My First VB2012 Program"
Me.BackColor = Color.Blue
Me.MaximizeBox=False
End Sub
Press F5 to run the program and you will get the exact interface as that shown in
Figure 2.1
In previous section, we have learned how to build an initial interface in Visual Basic
2012 by customizing the default form. Now we shall continue to build the interface by
adding controls to the form. There are numerous controls that we can add to the
form. Among the controls, the most common ones are button, label, textbox, listbox,
combobox, picture box, checkbox, radio button and more. The controls can be made
visible or invisible at runtime. However, some controls will only run in the background
and cannot be seen at runtime, one such control is the timer.
The Toolbox is usually hidden when you start Visual Basic 2012, you need to click
View on the menu bar and then select Toolbox to reveal the tool box, as shown in
Figure 2.2. You can also use shortcut keys Ctrl+w+x to bring out the tool box.
Figure 2.2: Toolbox
You can position the tool box by dragging it anywhere you like while its status is set
to float. You can also dock the toolbox by right-clicking on the tool box and choose
dock from the pop-up menu. The docked tool box that appears side by side with the
default form is as seen in Figure 2.2.
You can also dock the tool box at the bottom, below the default form, as shown in
Figure 2.3
Further, you may also pin the tool box to the side bar or the bottom bar by clicking on
the pin icon on the menu bar of the tool box.
How and where you want to position your tool box is entirely up to you but I strongly
suggest that you place the tool box alongside or at the bottom of the default form so
that it is easy for you to add controls from the tool box into the form. You should
never cover the form with the tool box because it will be difficult to add controls to the
form.
Figure 2.3
Adding a control to the form is an easy task, what you need to do is double click it or
drag it onto the form. You can drag the control around in the form and you can also
resize it.
To demonstrate how to add controls and then change their properties, we shall
design a picture viewer. First, change the title of the default form to Picture Viewer in
its properties window. Next, insert a picture box on the form and change its
background color to white. To do this, right click the picture box and select properties
in the popup menu, then look for the BackColor Property as shown in the properties
window in Figure 2.4.
Finally, add two buttons to the form and change the text to View and Close in their
respective properties windows. Now, we have designed a basic picture viewer. We
shall add more features later.
The picture viewer is not functional until we write code to response to events
triggered by the user. We will deal with the programming part in the coming lessons.
Figure 2.4
Summary
● In section 2.1, you have learned how to customize the form by changing the
values of its properties.
● In section 2.2, you have learned how to add controls to the form and change their
properties at design phase and at runtime.
Chapter 3
Writing the Code
❖ Learn the basics of code writing in Visual Basic 2012
In previous chapter, we have learned how to design the user interface by adding
controls to the form and by changing their properties. However, the user interface
alone will not work without adding code to them. In this chapter, we shall learn how
to write code for all the controls so that they can interact with events triggered by the
users. Before learning how to write Visual Basic 2012 code, let us dwell into the
concept of event-driven programming
Every form and every control you place on the form has a set of events related to
them. To view the events, double-click the control (object) on the form to enter the
code window. The default event will appear at the top part on the right side of the
code window. You need to click on the default event to view other events associated
with the control. The code appears on the left side is the event procedure associated
with the load event. Figure 3.1 illustrates the event procedure load associated with
the default form and Figure 3.2 shows the events associated with button.
Figure 3.1: Events associated with Form
To start writing code in Visual Basic 2012, click on any part of the form to go into the
code window as shown in Figure 3.1. This is the structure of an event procedure. In
this case, the event procedure is to load Form1 and it starts with Private Sub and
end with End Sub. This procedure includes the Form1 class and the event Load, and
they are bind together with an underscore, i.e. Form_Load. It does nothing other
than loading an empty form. To make the load event does something, insert the
statement
The Code
You will notice that above Private Sub structure there is a preceding keyword Public
Class Form1. This is the concept of an object oriented programming
language. When we start a windows application in Visual Basic 2012, we will see a
default form with the name Form1 appears in the IDE, it is actually the Form1 Class
that inherits from the Form class System.Windows.Forms.Form. A class has
events as it creates an instant of a class or an object.
You can also write code to perform arithmetic calculation. For example, you can use
the MsgBox and the arithmetic operator plus to perform addition of two numbers, as
shown below:
Figure 3.4
Summary
● In section 3.1, you have learned the concepts of event driven programming
● In section 3.2, you have learned how to write code for the controls
Chapter 4
Working with Controls
❖ Learn to work with a text box
❖ Learn to work with a label control
❖ Learn to work with a list box
❖ Learn to work with a combo box
In the preceding chapter, we have learned how to write simple Visual Basic 2012
code. In this lesson, we will learn how to work with some common controls and write
codes for them. Some of the commonly used controls are label, textbox, button, list
box and combo box. However, in this chapter, we shall only deal with text box , label,
list box and combo box. We shall deal with other controls later.
Example 4.1
In this program, you add two text boxes and a button on the form. The two text boxes
are used to accept inputs from the user. Besides, a button is also programmed to
calculate the sum of the two numbers using the plus operator. The value enter into a
text box is stored using the syntax Textbox1.Text , where Text is one of the
properties of text box.
The following program will add the value in textbox 1 and value in textbox 2 and
output the sum in a message box. The runtime interface is shown in Figure 4.1
Private Sub Button1_Click(sender As Object, e As EventArgs)
Handles Button1.Click
MsgBox(The sum is & Val(TextBox1.Text) +
Val(TextBox2.Text))
End Sub
Figure 4.1
After clicking Add button, you can get the answer in a message box, as shown in
Figure 4.2:
Figure 4.2
4.2 Label
Label is a very useful control for Visual Basic, as it is not only used to provide
instructions and guides to the users, it can also be used to display outputs. It is
different from text box because it can only display static text, which means the user
cannot change the text. Using the syntax Label.Text, it can display text and numeric
data . You can change its text in the properties window and also at runtime.
Example 4.2
Based on Example 4.1, you now add two labels, one is to display the text Sum= and
the other label is to display the answer of the Sum. For the first label, change the text
property of the label by typing Sum= over the default text Label1. Further, change its
font to bold and font size to 10. For the second label, delete the default text Label2
and change its font to bold and font size to 10. Besides that, change its background
colour to white.
In this program, instead of showing the sum in a message box, we wish to display
the sum on the label.
The Code
End Sub
*The function Val is to convert text to numeric value. Without using Val, you will see
that two numbers are joined together without adding them.
Figure 4.3
4.3 List Box
The function of the List Box is to present a list of items where the user can click and
select the items from the list. Items can be added at design time and at runtime. The
items can also be removed at design time and also at runtime.
To demonstrate how to add items at design time, start a new project and insert a list
box on the form then right-click on the list box to access the properties window. Next,
click on collection of the Item property, you will be presented with the String
Collection Editor whereby you can enter the items one by one by typing the text and
press the Enter key, as shown in Figure 4.4. After clicking on the OK button, the
items will be displayed in the text box, as shown in Figure 4.5
Figure 4.4
Figure 4.5
Items can also be added at runtime using the Add( ) method. Before we proceed
further, we need to know that Visual Basic 2012 is a full-fledged object oriented
programming language. Therefore, visual basic 2012 comprises objects. All objects
have methods and properties, and they can are differentiated and connected by
hierarchy. For a listbox, Item is an object subordinated to the object ListBox . Item
comprises a method call Add() that is used to add items to the list box. To add an
item to a list box, you can use the following syntax:
ListBox.Item.Add("Text")
For example, you can key-in the following statement
ListBox1.Items.Add("Nokia")
End Sub
The item “Nokia” will be added to the end of the list, as shown in Figure 4.6
Figure 4.6
You can also allow the user to add items using the InputBox function, as follows:
To remove items at design time, simply open the String Collection Editor and delete
the items line by line or all at once using the Delete key.
To remove the items at runtime, you can use the Remove method, as illustrated in
the following example. In this example, add a second button and label it “Remove
Items”. Click on this button and enter the following code:
To clear all the items at once, use the clear method, as illustrated in the following
example. In this example, add a button and label it “Clear Items”
In Visual Basic 2012, the function of the Combo Box is also to present a list of items
where the user can click and select the items from the list. However, the user needs
to click on the handle (small arrowhead) on the right of the combo box to see the
items which are presented in a drop-down list.
4.4.1 Adding Items to a Combobox
In order to add items to the list at design time, you can also use the String Collection
Editor. You will also need type an item under the text property in order to display the
default item at runtime.
Figure 4.7
After clicking the handle of the right side of the combo box, the user will be able to
view all the items, as shown in Figure 4.8
Figure 4.8
Besides, you may add items using the Add() method. For example, if you wish to add
a number of items to Combobox 1, you can key in the following statement. The
output is as shown in Figure 4.9
Figure 4.9
You can also allow the user to add items using the InputBox function, as follows: