1.1 Getting Started: Xudong Jia & Shu Liu
1.1 Getting Started: Xudong Jia & Shu Liu
1.1 Getting Started: Xudong Jia & Shu Liu
Chapter 1
Visual Basic.Net Environment
Welcome to Introduction to Computer Programming and Numerical Methods. The
objective of this chapter is to introduce Microsoft Visual Basic.Net (or VB.Net), the
most useful computer programming language. You will learn step-by-step the Integrated
Development Environment (IDE) inside VB.Net for creating, running and debugging
VB.Net programs.
1.1
Getting Started
Assume you have a user account and a password for use of computers in a university or
college lab. Now it is the time for you to enter the user account name and the password
into a lab computer. [If you have a home computer where VB.Net and Windows XP is
installed on it, you can also learn the VB.Net Integrated Development Environment
using your home computer.] The procedures described in this chapter apply both your
lab and home computers.
After you log on to a computer (lab or home), you are in the Windows XP operating
system. You are encouraged to change the password to a new one for security reasons.
Otherwise, someone may break into your account and delete your files accidentally or
with some purposes.
The steps to change your password and start VB.Net are as follows:
Press Alt+Del+Ctrl keys at the same time and follow the instructions provided
in the window dialog to change your password.
Click the Start button at the bottom of the Desktop using the left mouse button.
Select Programs-> Microsoft Visual Studio .Net 2003 -> Microsoft Visual
Studio .Net 2003. You will be in the .Net development environment (see Figure
1-1). It is noted that the Visual Studio .Net 2003 version is used in this book. All
the programs listed in this book are tested under this version and saved in the CD
at the back of this book.
Inside the .Net development environment, you will see three tabs listed in the
middle of the environment. These three tabs are Projects, Online Resources, and
My Profile. Click the Projects tab and New Project button.
Highlight Visual Basic Projects in the Project Type window and Windows
Application in the Templates window.
Chapter 1
The integrated development environment (IDE) is important in helping you create, run
and debug VB.Net programs or applications. You can consider VB.Net IDE as
Microsoft Word and VB.Net programs as Word documents. A Word document is
created in Microsoft Word, while a VB.Net program is created in the VB.Net IDE.
The VB.Net IDE consists of a set of Graphical User Interfaces (GUIs) and components
(see Figure 1-2). You need to understand this IDE because all VB.Net programs or
applications are developed within this environment.
Let us look at this environment in detail. On the top of the window, you can see the
following components:
Chapter 1
It shows the title of the VB.Net project you are currently working on.
The default project title is the project name you have specified when you
create a new project. If you would like to change the project name or title
to other name, you can change it through Project -> Project Properties.
Menu Bar
Tool Bar
Two built-in tool bars are provided when you open the VB.Net IDE. You
can add more toolbars. The steps to do that are
Select Views->Toolbars
Check on the names of the toolbars.
In addition to the above components, you should be aware of the five windows in the
IDE when you are developing a VB.Net project or application. These five windows are
as follows:
Toolbox window
Chapter 1
The window is a blank window (or form) on which you can place
controls. The form and its controls constitute the graphical user
interface (GUI) of a VB.Net program. They are the visual part of
the program with which the user can interact.
Solution Explorer
Properties
Window
Chapter 1
_
_
Note that there are two arguments inside the parentheses. We will discuss these two
arguments later. Now you just understand two arguments are important in this event
procedure.
When you compare the above procedure to that created by your IDE, you see the above
procedure has two _ letters inserted between the two parentheses. Each _ letter
indicates that the line after the letter is the continuation of its previous line. For example,
the first _ letter indicates that ByVal e As System.EventArgs) Handles is
considered as part of the first line. With the use of the two _ letters, the first three
lines are considered as one line in VB.Net IDE.
Chapter 1
Type the following code between Private Sub and End Sub.
Msgbox Welcome to VB.Net
_
_
Provided that the code has been written for the click event of the aCommandButton
control, let us run the project and see what happens. You can find this simple
application as Example 1_1 in Chapter 1 of CD.
Select Debug -> Start
A window that represents the form you just have designed is prompted. Click the
command button control and see what the program will do for you. Surprisingly, you
will see another window. The window shows the message Welcome to VB.Net. This
message is created by the VB.Net statement Msgbox Welcome to VB.Net.
More discussions of VB.Net statements will be provided in later chapters.
From the above simple application, we can conclude that the VB.Net IDE takes the
following steps to run a VB.Net program:
Step 1:
Visual Basic.Net monitors the forms and the controls in each form for all
the possible events such as mouse movements, clicks, keystrokes and so
on.
Step 2:
Chapter 1
1.4
A VB.Net application development process involves five major steps, that is, 1)
understand the logic needed to solve the problem by a VB.Net program, 2) design
form(s) to implement the logic, 3) write program code to support the controls in the
form(s), 4) debug the program, and 5) deliver the program to users.
Example 1.2 Quadratic Equation VB. Net Application
Let us use an example to describe the development process. Suppose we need to
develop a VB.Net program to solve the following quadratic equation and find two real
roots if they exist:
f ( x ) = ax 2 + bx + c = 0
Where a 0 and b 0
Two logical flows can be developed for the program. These two flows are:
Logical Flow #1:
These two logic flows will make the development of a VB.Net application different.
Here we only concentrate on the VB.Net implementation of the simple logic and leave
the complex logic in later chapters. Example1_2 within Chapter 1 of the CD is the
VB.Net application for the simple logic.
The development process to implement the simple logic flow involves the following
steps:
Chapter 1
1)
2)
3)
4)
Form design
Program coding
Debugging and testing
Program packaging and delivery
Form Design
The form design is a process that implements the logic for the program. For the simple
logic flow, we need to develop the form as shown in Figure 1-3. Within this form, you
can find a form control, five label controls, five text box controls, and two command
controls.
SimpleLogic
Val_a
Root1
Val_b
Root2
Val_c
Calculate
Figure 1-3
Cancel
SimpleLogic
Textbox for a:
Textbox for b:
Textbox for c:
Textbox for root1:
Textbox for root2:
Calculate button:
Cancel button:
val_a
val_b
val_c
root1
root2
calculate
cancel
Label for a:
Label for b:
Label1
Label2
Chapter 1
Label for c:
Label for root1:
Label for root2:
Label3
Label4
label5
These names must follow the VB.Net rules and conventions listed below:
1)
Each control must have a unique name that can be no more than 40
characters long.
2)
Each control name must start with a letter. The remaining characters can be
any letters, numerical characters, or underscores. You cannot have spaces,
special symbols, or punctuation characters in a control name.
3)
Chapter 1
Step 2:
Program Coding
There are two event procedures to be developed. These two procedures will respond to
two click actions (or events), one on the Calculate button and the other on the Cancel
button.
Public Class quadradic_equation
Inherits System.Windows.Forms.Form
Windows Form Designer Generated Code
Private Sub cancel_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Handles cancel.Click
End
End Sub
_
_
10
Chapter 1
_
_
a = Val(val_a.Text)
b = Val(val_b.Text)
c = Val(val_c.Text)
chk = b ^ 2 - 4 * a * c
If chk < 0 Then
MsgBox("no real roots exist")
Exit Sub
Else
rt1 = (-b + Math.Sqrt(chk)) / (2 * a)
rt2 = (-b - Math.Sqrt(chk)) / (2 * a)
End If
root1.Text = Str(Rt1)
root2.Text = Str(rt2)
End Sub
End Class
As you can see the two procedures all begin with Private Sub and end at the End Sub.
Between Private Sub and End Sub, there are various VB.Net statements. We will learn
these statements one-by-one in later chapters. You do not need to know these
statements right now.
Enter the above two procedures into the code window and name the project
SimpleLogic.
Step 3:
Debugging and testing a VB.Net application is a tedious task that often involves a
systematic way of checking various potential errors in the application. You can read the
article entitled Testing of a Windows-Based Transportation Application: The LRSEdit
Experience. for more information on how to do the testing on Windows-based
applications. The article was published on the ITE Journal on the Web, February 1999
and can be found as testing.pdf in the CD.
The application developed in step 2 is a simple application. It does not involve a lot of
testing efforts. However you need to know tools provided in VB.Net for the debugging
and testing of programs. The tools can be accessed through the Debug menu in the
VB.Net IDE.
Introduction to Computer Programming & Numerical Methods
11
Chapter 1
It is noted that this simple application does have some errors. What happens if a nonnumeric value is entered into the Val_a, Val_b or Val_c textbox? When you enter abc
into the Val_a textbox, the program will be terminated abnormally. In later chapters, we
will add more VB.Net statements into this simple application to deal with various what
if cases.
Step 4:
Once the project is tested error free, it is the time to package the project and deliver it as
a software product to users. The software product contains not only the executable
program but also various documentations that guide users for installation and execution
of the product.
The professional product, once packaged, will have a setup program that allows users to
install the product into any computers, even the ones without VB.Net. After installation,
the product can be executed in a window environment.
1.5
Questions
Q1.
Explore Internet to see how many versions Visual Basic.Net has. Hint: Go to
Microsoft web site or use Google search engine. You may see that VB.Net has
Learning Version, Professional Version, and Enterprise Version.
Q2.
What is the difference between Visual Basic and Visual Basic .Net?
Q3.
Develop a VB.Net application that gets input from the user about the radius of a
circle, calculates the diameter, circumference, and area of the circle, and displays
the results in three textboxes. The three textboxes should be disabled for user
input. A logic flow and the form that implements the flow are required for this
problem.
Q4.
List the five windows inside the VB.Net IDE and describe the functions of each
window.
Q5.
List all the properties for a textbox control, a label control, and a command
button control.
Q6.
What are the steps for the development of a VB.Net Application? Explain each
step briefly?
Q7.
Why do we need the _ letter in VB.Net code? Hint: See the explanation in this
chapter.
12
Chapter 1
Q8.
List three VB.Net rules that should be considered when a control name is
defined.
Q9.
Q10.
Q11.
Assume you have a command button control named Clear created in the Form
window. When you double click on the button, you will see the empty event
procedure created in the Code window. Can you write down the first line and last
line of the empty procedure without looking at the procedure in the Code
window?
Q12
List steps required to create a VB.Net project called FirstProject and save the
project files into C:\temp folder.
Q13.
Assume you have already created a VB.Net project called SecondProject and
saved it in the C:\temp folder. How many files are there within the
SecondProject folder? What is the file type of each file in the SecondProject
folder?
Q14.
When you find a file in the Windows Explorer whose type is Visual Basic .Net
Project, what happens when you click on it? Hint: Assume you create a project
called SecondProject in Q13, you find the SecondProject file in the
C:\temp\SecondProject folder whose type is Visual Basic .Net Project.
Q15.
How many controls are there in Figure 1.3? List these controls and explain their
control type.
Q16.
Q17.
What is a GUI?
13