Sie sind auf Seite 1von 6

Revision

Programming with Visual Basic

Visual Studio and the


Visual Basic Environment

The Practical
Visual Studio Consists of Tools That
You Use to Build Visual Basic
Applications
Todays Lab notes adapted from Pearson Education, Inc. Publishing as Pearson Addison-Wesley

The Visual Studio IDE

The Visual Basic Environment

Visual Studio is an integrated development


environment, often abbreviated as IDE
Provides everything needed to create, test, and
debug software including:
The Visual Basic language
Form design tools to create the user interface
Debugging tools to help find and correct
programming errors
Visual Studio supports other languages beside
Visual Basic such as C++ and C#
Slide 1- 3

Tutorial 1-4 introduces elements of the IDE:

Customizing the IDE


Design window a place to design and create a form
Solution Explorer window shows files in the solution
Properties window modify properties of an object
Dynamic Help window a handy reference tool
Toolbar contains icons for frequently used functions
Toolbox window objects used in form design
Tooltips a short description of buttons purpose

Slide 1- 4

Tutorial 1-4: Elements of the IDE

Tutorial 1-4: Elements of the IDE

Create a folder in Documents and call it VB Projects.


One-time setup steps:
1. Select Start | All Programs | Microsoft Visual
Studio 20** Express | VS Express for Desktop
2. Select Visual Basic Development Settings as the
default environment
3. To configure Visual Basic select Tools | Options
and under the Projects and Solutions select
General. For the Visual Studio projects location
choose the browse button [...], and select
Documents| < VB Projects >

4. Still in the Tools | Options under


Projects and Solutions select VB
Defaults and change the settings to
Option Explicit: On
Option Strict: On
Option Compare: Binary
Option Infer: Off
5. Click the OK button

Slide 1- 5

Tutorial 1-4: Elements of the IDE

Slide 1- 6

Tutorial 1-4: Elements of the IDE

Creating a new project:


1. Select File | New Project
2. In the Project types (right) pane select Visual Basic |
Windows (under Express skip this step)
3. In the Temples (left) pane select Windows Application
(under Express select Windows Forms Application)
4. Change the default WindowsApplication1 name to
Tutorial 1-4 and then click the OK button
5. Select File | Save All and check that the Name and
Location look correct. Modify if necessary and Save

Slide 1- 7

Set Visual Studio's Options:


1. Select Tools | Options and under Text Editor | Basic
make sure that all the options are checked
2. Still in Tools | Options but under Windows Forms
Designer set the
Grid Size to 8, 8,
LayoutMode to SnapLines,
ShowGrid to True, and
SnapToGrid to True.
3. Click OK
Now you can explore VB.
Slide 1- 8

Visual Basic Controls

Tutorial 1-3, Visual Basic Controls

As a Windows user youre already familiar with many


Visual Basic controls:
Label - displays text the user cannot change
TextBox - allows the user to enter text
Button performs an action when clicked
RadioButton - A round button that is selected or deselected
with a mouse click
CheckBox A box that is checked or unchecked with a
mouse click
Form - A window that contains these controls

Tutorial 1-3 demonstrates these controls. For a practical


feel, develop this form (just controls, no programming).

Slide 1- 9

Slide 1- 10

Examples of Names
Naming Conventions

In the diagram below, the label controls use the default

names (Label1, etc.)


Text boxes, buttons, and the Gross Pay label play an
active role in the program and have been changed.

Develop this form.

Label1
Label2

txtHoursWorked
txtPayRate
lblGrossPay

Label3

btnCalcGrossPay

Control names must start with a letter


Remaining characters may be letters, digits, or
underscore
1st 3 lowercase letters indicate the type of control
txt
for Text Boxes
lbl
for Labels
btn
for Buttons
After that, capitalize the first letter of each word
txtHoursWorked is clearer than txthoursworked

btnClose
Slide 1- 11

Slide 1- 12

Event Handler Compute Gross Pay

Event Handler - Close

Now write the following code in the relevant control:


Private Sub btnCalcGrossPay_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalcGrossPay.Click
Private Sub btnClose_Click(ByVal sender As System.Object, _
Define a variable to hold the gross pay.
Dim sngGrossPay As Single

ByVal e As System.EventArgs) Handles btnClose.Click

Convert the values in the text boxes to numbers,


and calculate the gross pay.
sngGrossPay = CSng(txtHoursWorked.Text) * CSng(txtPayRate.Text)

End the program by closing its window.


Me.Close()
End Sub

Format the gross pay for currency display and


assign it to the Text property of a label.
lblGrossPay.Text = FormatCurrency(sngGrossPay)
End Sub

Slide 1- 13

VB Statements

A statement is a reserved word


Statements are instructions that are built into the
Visual Basic language

Slide 1- 14

Comments

Some examples are:


End
Option Explicit
Private Sub
Dim

A comment statement is added to explain the


purpose of a program, or a statement
For yourself and others
Any statement beginning with an apostrophe or
REM is a comment
Comments can be added to end of statements
using apostrophe

Visual Basic Help

There are three types of Help:


Help menu option
context-sensitive help
and Auto Help
Help uses the familiar Internet Explorer browser
interface for the first two types of help.
You can seek help by selecting Contents, Index,
or Search from the Help menu item

Files in Visual Basic

All projects in VB have a .vbp (project) file and at


least one .frm (form file) file.
Always save .frm files first and then save project
files.
Use File|Save or File|Save as commands for
this purpose or click the Disk icon on toolbar.
Projects with graphics also have .frx (binary form)
files. They are saved automatically.
Module files have a .bas extension and are pure
code files.

Context-Sensitive and Auto Help

With context-sensitive help, pressing the F1 key


provides help on whatever item the cursor is
located.
With Auto Help, VB tries to help you with a code
statement by providing:
A list of items to complete the statement
Info on the statement you have started
Tips on the type of data you are working with

Save files

Important!
Save early.
All three types can and should have same
names.
Eliminate prefix (eg.frm)

Add graphic

Retrieve the WageCalculator project from your


disk
Insert a picture box (image) control.
Select a graphic
Change the size of graphic

Save it (Save As)

Use a meaningful name


Eg. wageCalc.frm, wageCalc.vbp,
wageCalc.frx
Copy files to the folder you created in
Documents.

StretchImage (Stretch) property of the image/picture


box control

Save the project

More Practice

Develop a small app for working with circles,


whose inputs, process and outputs are as follows:
I: radius
P: calculate circumference, calculate area
O: radius, circumference, area

Das könnte Ihnen auch gefallen