vb1 revision - programming with vb - prac (0)

6
1 Revision Programming with Visual Basic The Practical Today’s Lab notes adapted from Pearson Education, Inc. Publishing as Pearson Addison-Wesley Visual Studio and the Visual Basic Environment Visual Studio Consists of Tools That You Use to Build Visual Basic Applications 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 The Visual Studio IDE 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 button’s purpose Slide 1- 4 The Visual Basic Environment

Upload: mogeni-jnr

Post on 07-Jul-2016

222 views

Category:

Documents


0 download

DESCRIPTION

VB1 Revision - Programming With VB - Practicals with guide

TRANSCRIPT

Page 1: VB1 Revision - Programming With VB - Prac (0)

1

RevisionProgramming with Visual Basic

The Practical

Today’s Lab notes adapted from Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Visual Studio and theVisual Basic Environment

Visual Studio Consists of Tools That You Use to Build Visual Basic

Applications

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

The Visual Studio IDE

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 button’s purpose

Slide 1- 4

The Visual Basic Environment

Page 2: VB1 Revision - Programming With VB - Prac (0)

2

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 selectGeneral. For the Visual Studio projects locationchoose the browse button [...], and selectDocuments| < VB Projects >

Tutorial 1-4: Elements of the IDE

Slide 1- 5

4. Still in the Tools | Options underProjects and Solutions select VBDefaults and change the settings to Option Explicit: On Option Strict: On Option Compare: Binary Option Infer: Off

5. Click the “OK” button

Tutorial 1-4: Elements of the IDE

Slide 1- 6

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

Tutorial 1-4: Elements of the IDE

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.

Tutorial 1-4: Elements of the IDE

Slide 1- 8

Page 3: VB1 Revision - Programming With VB - Prac (0)

3

As a Windows user you’re 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

Visual Basic Controls Tutorial 1-3, Visual Basic Controls

Slide 1- 10

Slide 1- 11

Examples of Names

btnCalcGrossPay btnClose

txtHoursWorkedtxtPayRatelblGrossPay

Label1Label2Label3

In the diagram below, the label controls use the default names (Label1, etc.)

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

Develop this form.

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

Slide 1- 12

Naming Conventions

Page 4: VB1 Revision - Programming With VB - Prac (0)

4

Slide 1- 13

Event Handler – Compute Gross PayNow 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

‘Define a variable to hold the gross pay.Dim sngGrossPay As Single

‘Convert the values in the text boxes to numbers,‘and calculate the gross pay.sngGrossPay = CSng(txtHoursWorked.Text) * CSng(txtPayRate.Text)

‘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- 14

Event Handler - Close

Private Sub btnClose_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles btnClose.Click

‘End the program by closing its window.

Me.Close()

End Sub

A statement is a reserved word Statements are instructions that are built into the

Visual Basic language

Some examples are: End Option Explicit Private Sub Dim

VB Statements

A comment statement is added to explain thepurpose of a program, or a statement For yourself and others

Any statement beginning with an apostrophe orREM is a comment

Comments can be added to end of statementsusing apostrophe

Comments

Page 5: VB1 Revision - Programming With VB - Prac (0)

5

There are three types of Help: Help menu option context-sensitive help and Auto Help

Help uses the familiar Internet Explorer browserinterface for the first two types of help.

You can seek help by selecting Contents, Index,or Search from the Help menu item

Visual Basic Help Context-Sensitive and Auto Help

With context-sensitive help, pressing the F1 keyprovides help on whatever item the cursor islocated.

With Auto Help, VB tries to help you with a codestatement 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

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.

Important! Save early. All three types can and should have same

names. Eliminate prefix (eg.frm)

Save files

Page 6: VB1 Revision - Programming With VB - Prac (0)

6

Retrieve the WageCalculator project from your disk

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

StretchImage (Stretch) property of the image/picture box control

Save the project

Add graphic

Use a meaningful name Eg. wageCalc.frm, wageCalc.vbp,

wageCalc.frx Copy files to the folder you created in

Documents.

Save it (Save As…)

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