csci 3131.01 chapter 2 creating application with visual basic instructor: bindra shrestha

28
CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake

Upload: holmes-pate

Post on 30-Dec-2015

66 views

Category:

Documents


2 download

DESCRIPTION

CSCI 3131.01 Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha University of Houston – Clear Lake. Acknowledgement Dr. Xinhua Chen And Starting Out with Visual Basic 2010 by Tony Gaddis and Kip Irvine. Topics Building the Directions application - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

CSCI 3131.01

Chapter 2 Creating Application with Visual Basic

Instructor: Bindra Shrestha

University of Houston – Clear Lake

Page 2: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Acknowledgement

Dr. Xinhua Chen

And

Starting Out with Visual Basic 2010 by Tony Gaddis and Kip Irvine

Page 3: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Topics•Building the Directions application•Responding to events•Modifying properties of a control with code: Text, Autosize, BorderStyle, and TextAlign properties•Clickable images•Using Visual Studio Help•Debugging an application

Page 4: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Steps to Create an Application

1.Clearly define what the application is to do.2.Visualize the application running on the computer and design its user interface3.Make a list of the controls needed.4.Define the values of each control’s relevant properties.5.Start Visual Basic and create the forms and other controls

Page 5: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Clearly Define the Task of the Application

Purpose: Display a map to the Highlander HotelInput: NoneProcess: Display a formOutput: Display on the form a graphic image showing a map

Specifications of the Application

Page 6: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Visualize and Design the User Interface

Page 7: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Points to Watch while Designing User Interface

Follow the convention of Windows Graphic User Interface (GUI) design.

Most Windows application consists of (1) a main (primary) window, (2) possibly some other primary windows, and (3) one or more secondary windows, called dialog boxes.

The Windows Notepad application uses the primary window for editing the document; it uses a dialog box for selecting the font related properties.

Primary windows can be resized, minimized, maximized and closed by the user.

Page 8: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Using Graphics, Fonts and Color in User Interface

Using graphics, fonts and colors appropriately helps the information flow more efficiently from the user interface to the users.

Inappropriate use of graphics, fonts and colors distracts the users and may also offend some of the users.

Page 9: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

List the Controls Needed

Control Type Control Name Description

Form Form1 A small form that will serve as (Default Name) the window onto which the other

controls will be placed

Label Label1 Displays the message(Default Name) "Directions to the Highlander

Hotel"

PictureBox PictureBox1 Displays the graphic image(Default Name) showing the map to the hotel

Page 10: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Define Control Relevant Property Values

• Form– Name: Form1– Text: "Directions"

• Label– Name: Label1– Text: "Directions to the Highlander Hotel"– TextAlign: MiddleCenter– Font: Microsoft sans serif, bold, 18 point

• PictureBox– Name: PictureBox1– Picture: HotelMap.jpg– SizeMode: StretchImage

Page 11: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Use VB to Create the Application

• Establish the Form and set its Text property• Add a Label control

– Position and resize it on the form– Set Text, TextAlign, and Font properties

• Add a PictureBox control– Position and resize it on the form– Set Image property to display HotelMap.jpg

• Run the application• Close and save the application

Page 12: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Responding to Events

An Application Responds to Events, Such As •Mouse Clicks•Keyboard Input

The code that handles the events are called Event Handlers or Event Procedures

Write the Event Procedures for the Directions Application

Page 13: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Augment the Hotel Application

• Now the hotelowner wants toadd an optionto view writtendirections:

Page 14: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Controls to be Added

Control Type Control Name Description

Label lblDirections Displays written directions to the hotel

Button btnDisplayDirections When clicked, causes lblDisplayDirections text to appear on the form

Button btnExit Stops the applicationwhen clicked

Page 15: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Control Properties

• Label:

– Name: lblDirections– Text: “Traveling on I-89, take…”– Visible: False

• Button:

– Name: btnDisplayDirections– Text: “Display Directions”

• Button:

– Name: btnExit– Text: "Exit"

Page 16: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Method btnDisplayDirections_Click

Private Sub btnDisplayDirections_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnDisplayDirections.Click' Make the directions visiblelblDirections.Visible = True

End Sub

Line Continuation MarkName of the event the procedure responds to

Name of the control that owns the event procedure

Marks the beginning of this event procedure

Makes the control lblDirections visible:Assigns the value True to the Visible Propertyof the lblDirections control.

Event handled by this procedure

Page 17: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Syntax for Referring to the Value of a Control's Property• Specify the control name (lblDirections)

• Then a dot• Then the PropertyName (Visible)

• For example:– lblDirections.Visible– refers to the Visible property of the lblDirections

control– The visible property values may only be true or

false

Page 18: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Syntax for an Assignment Statement

• Specify the item to receive the value • Then the equal symbol• Then the value to be assigned • For example:

– lblDirections.Visible = True– Assigns the value True to the Visible property of

the lblDirections control– Causes the text of the lblDirections control to

become visible to the user

Page 19: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Method btnExit_Click

Private Sub btnExit_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnExit.Click' End the application by closing the windowMe.Close()

End Sub

Line Continuation MarkName of the event the procedure responds to

Name of the control that owns the event procedure

Marks the beginning of this event procedure

Closes the current form, referred to as Me, and ends the program

Event handled by this procedure

Page 20: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Modifying the Text Property in Code

Private Sub btnFeet_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnFeet.Click

' Display the conversion to feet.lblMessage.Text = "1 Kilometer = 3,281 feet"

End Sub

Assigns the string to the right of the equal sign to the text property of lblMessageThis replaces the previous text propertyof lblMessage with the new value shown

Page 21: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

AutoSize Property for Labels

• AutoSize is a Boolean (either True or False) Property of labels

• False (the default) means the box size will not change, regardless of the amount of text assigned to it

• True means the box will automatically resize itself to fit the amount of text assigned to it

Page 22: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

BorderStyle Property for Labels

• BorderStyle determines the look of the box– None (the default) means no border– FixedSingle results in a border one pixel wide– Fixed3D gives the border a recessed 3-dimensional

look

Page 23: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

TextAlign Property for Labels

• The value of TextAlign establishes the alignment (or justification) or the text: TopLeft TopCenter TopRight

• The assignment statement below forces the text of lblTitle to appear in the middle center of the label

lblTitle.TextAlign = ContentAlignment.MiddleCenter

MiddleLeft MiddleCenter MiddleRight

BottomLeft BottomCenter BottomRight

Page 24: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Clickable Images

Controls other than Buttons can have Click event procedures.

PictureBox controls can respond to mouse clicks

For example, if a national flag image is clicked, you may use a label to display the name of the country:

Private Sub picUSA_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles picUSA.Click' Display the country namelblMessage.Text = "United States of America"

End Sub

Page 25: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Microsoft Document Explorer

• The Visual Studio Help, also called Microsoft Document Explorer, contains these options:– How Do I – a task-based topics list by category– Search – find help topics using words/phrases– Contents – displays a table of contents for help– Index – Search using predefined keywords– Favorites help – lets you bookmark help topics– Dynamic help – help for current task performed

Page 26: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Context-Sensitive Help (F1 Key)

• Displays information about whatever feature the user is currently focused on

• For example:– Click on a Button control– Press F1– Help explains all about the Button control– Click on a Label control– Press F1– Help explains all about the Label control

Page 27: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Debugging: Compilation Errors

• These are errors in the syntax (form) of your program

• Visual Basic will inform you of these errors as soon as the code is entered

• The area of the error will be underlined with a jagged blue line

• A description of the error will be given in the Error List window

• Display this window by selecting Error List from the View menu option

Page 28: CSCI 3131.01  Chapter 2 Creating Application with Visual Basic Instructor: Bindra Shrestha

Debugging: Runtime Errors

• Some errors occur as your program runs• These are different from syntax errors which

occur as the code is entered by the programmer

• Runtime errors occur when Visual Basic attempts to perform an operation that cannot be executed