55
Chapter 2 1 Creating Applications with Visual Basic .NET Chapter 2

Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Embed Size (px)

Citation preview

Page 1: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 1

Creating Applications with Visual Basic .NET

Chapter 2

Page 2: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 2

VB Modes

Design Build and modify the project:Place, arrange, and customize controls.

Write processing scripts.

Save the project.

Run Run project as a user:Test components during development.

Demonstrate program to user during development.

Break Temporarily suspend run mode:Examine status of processing.

Determine cause of problems.

Page 3: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 3

Example Modes

Design

Run (see error message)

Break (click Debug to see error location)

Stop (Ctrl+Break)

Page 4: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 4

VB Structure

Create and name a project. Folder created with same name so enter name

correctly the first time! Project

Forms and other necessary components to run a program

Form Individual window interface (GUI background) Contains 1 or more controls General declarations

Page 5: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 5

Controls Objects on a form Labels, command buttons, text boxes, etc. (each with

specific purpose and appearance)

Properties Attributes defining characteristics of control Visual appearance

Alignment Caption

Default behavior of control at run time

Page 6: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 6

Event Procedures Signal generated at run time indicating that something

(action) has occurred to which the application must respond.

Caused by user actions, such as clicking a command button, double-clicking, or mouse over.

Based on type of control. Event Procedure: script of detailed instructions.

Page 7: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 7

Control Planning Grid

Page 8: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 8

Open Chapter1GrossPay project

Open project file (*.sln). See pp. 57-58 for more information.

Page 9: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 9

Form Properties

Text (appears in title bar) Weekly Pay Calculator

StartPosition (location of format Run time) Default: WindowsDefaultLocation Change to: CenterScreen (explore other options on your own)

Page 10: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 10

Page 11: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 11

Controls (Review)

Refer to Table 1-2 on page 9 for a list of controls. Input from User (text box, check box, etc.) Output to User (label, image, etc.) Trigger processing (command button, menu,

etc.)

Page 12: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 12

Control Naming Conventions

Prefix Control Example

btn Button btnExit

lbl Label lblName

txt Text txtName

Page 13: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 13

Insert Controls

Page 14: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 14

Form Properties Review

Text Displays text in title bar

StartPosition Controls location of form at run time

Size Controls height and width of the form

Icon Displays icon image in top left corner of title bar

BackColor Specifies the background color of the form

Page 15: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 15

FormBorderStyle

Controls the border for the form

Value Description

Sizable (default) Enables the minimize, maximize, and close buttons.

None Displays form without a border, title bar, and sizing buttons.

Fixed3D Displays the form at specific size; can minimize and maximize, but can’t manually resize the form.

Page 16: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 16

Properties Window (p. 60)

Categorized Alphabetized

Page 17: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 17

Control Properties

TextAlign Location of Text value (MiddleCenter) (p. 49)

BackColor Background color (Light Yellow)

ForeColor Text color (Blue)

Page 18: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 18

Control Size & Alignment

Click, Ctrl, Click to select. Choose Format>Align. Choose Format>Make Same Size>Both

Click + tosee details,such as specific widthand height

Page 19: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 19

Control Sizes

Select multiple controls. Choose Format > Make Same Size >

HeightWidthBoth

Look at Width and Height property settings.

Page 20: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 20

Equal Spacing between Controls

Select controls of similar type. Format > Vertical Spacing > Make Equal. Format > Horizontal Spacing >

Make Equal.

Page 21: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 21

Control Properties Labels

Name: lbl prefix Text: text to display during Run Time TextAlign: Alignment of text within label control (p. 49) Font options Visible: Boolean can hold only True or False

(Discuss reasons for setting to False. See pp. 62-63.) Text Boxes

Name: txt prefix Text: empty (delete existing text) Font options

Buttons Name: btn prefix Enabled: whether can respond to user Text: text displayed within button control 3rd Button Style Property: 1-Graphical 3rd Button Picture: image selected

Page 22: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 22

AutoSize Control Property

Specifies control’s sizing abilities False (default) maintains control’s design time size;

only partial Text value displayed if too much for size.

True enables control’s size to expand automatically to accommodate Text property value

Page 23: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 23

BorderStyle (for Labels)

None Descriptive label

Fixed 3D Output

FixedSingle Output

Page 24: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 24

Design After Initial Creation

Buttons:Format>Make Same Size>BothFormat>Horizontal Spacing>Make Equal

TextAlign property values(see Figure 2-13 on p. 49)

Page 25: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 25

MS Word Font Dialog Box

What control attributes do you see? Alignment Capitalization Tab Order Access Keys

Page 26: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 26

Focus (pp. 106-111)

Ability of a control to enable the user to interact with the program by using the keyboard. Text Box: Enter text Button: Press Enter to trigger event Label: Can’t directly receive focus; passes to

control with next TabIndex value. Only one control can have focus at a time. Control w/ focus is active control. Lose focus when go to another control.

Page 27: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 27

Focus Method

Example: txtFirstName.Focus()

Method action: Positions insertion point in a text box to

receive input. Displays dotted borer around button.

Useful in event procedure to set focus to first text box on a form

Page 28: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 28

TabIndex Property (pp. 107-111)

Specifies order of pressing Tab key to go from one control to another to receive focus. Set to avoid disorganized tab order due to

sequence of inserting controls. Choose View>Tab Order for visual tab order.

Starts with zero on a label (since text boxes don’t have captions). 0 for label (or check box or option button). 1 for text box relating to label Etc.

Page 29: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 29

Access Keys (pp. 110-111)

Enables users to access controls with the keyboard.

Displays underscore for keyboard shortcut, such as File on menu bar or Exit.

Uses standard guidelines: First letter (often, not always) Other appropriate letter See other apps for examples

Requires & to left of character in Text property, such as: E&xit for Exit.

Page 30: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 30

Access Key Considerations

Do not assign the same access key to more than 1 control on the form.

If you want to display & within the Text property, type 2 ampersand symbols: Pizza && Pasta becomes…Pizza & Pasta

Assign access key to a label that describes a text box b/c a text box can’t have access key. Assign Tab Order consecutively for label and

respective text box.

Page 31: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 31

Accept & Cancel Buttons (p. 113)

Accept Button Triggers Click event when user presses the

Enter key. OK button

Cancel Button Triggers Click event when user presses the

Escape key. Cancel button

Form property

Page 32: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 32

Modified Interface

0

2

4

1

3

5

Assign a high #, such as 100;VB leaves original #, which duplicatesanother TabIndex if you don’t

6 7 8

Page 33: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 33

Event Procedures (p. 62)

Button names Description (pseudocode) Procedural Flowcharts (p. 63)

Page 34: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 34

Pseudocode for Command Buttons

Enter Display contents of txtFirstName Display message “, you earned $”

Clear Empty contents of txtFirstName text box. Empty contents of txtHoursWorked text box. Empty contents of txtHourlyPayRate text box. Empty contents of lblGrossPay Place insertion point (set focus) in 1st box.

Exit Terminate the application.

Page 35: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 35

Close Button Event Procedure

Page 36: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 36

Code Window

Text-editing window to write code and remarks

Intellisense feature to automate coding (such as valid property values)

Vertical line: code region Plus sign: collapsed code for simplification Minus sign: expanded code (code you

typically create)

Page 37: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 37

Event Procedure Explanation

Private—keyword restricting event procedure’s use to form containing its definition

Sub—abbreviation for procedure (block of code that performs a task)

KeywordsIn blue

Control name Event

Page 38: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 38

Clear Button Event Pseudocode

Page 39: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 39

Clear Method

ControlName.Clear () Empties the Text property value Example: txtFirstName.Clear()

LabelName.Text = “” Copies empty string to label Clear method doesn’t work on labels Example: lblGrossPay.Text = “”

Page 40: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 40

Clear Button Event Procedure

Page 41: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 41

Calculate Button Pseudocode

Multiplies hours worked by hourly pay rate. Displays the following string in the

lblGrossPay label:FirstName contents

“, you earned $” <literal string>

Result of calculation

Page 42: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 42

Text Output

Output text property value from the text box to the label

Page 43: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 43

String Literal Output

Type literal in quotation marks

Page 44: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 44

Results of Calculation Output

Type expression.

Page 45: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 45

Concatenate (put together)

Type & to concatenate strings for output. Press Spacebar before and after & Type _ for line continuation character and

indent continued lines of code

Note: We will learn ways to validate input and store results in variables before displaying output as we continue throughout the semester.

Page 46: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 46

Design Guidelines

Labels close to text boxes Related options closer together Consistent space between controls Consistent space from controls to edges of

form Access keys for keyboard users Buttons

Right side with same width Bottom with same heights

Page 47: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 47

Review

Labels Descriptive captions for input controls Output showing results

Text Boxes Input text from user

Buttons Trigger event procedure to do something with

the inputs

Page 48: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 48

Remarks (p. 64)

Comments or explanations about code Part of program but ignored by Visual

Basic .NET Followed by an apostrophe

'Empties the text box for user input Program description

'Keith Mulbery'ISYS 1200'Assignment 1‘Gross Pay Calculating Program

Page 49: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 49

Reasons for Remarking

Documents code for maintenance Yourself Other programmers

Saves time in understanding event procedures.

Helps others understand your logic in coding long procedures.

Page 50: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 50

Assignment Statement

Assignment operator = Statement that copies the value on the right side

of the assignment operator to the item on the left side of the assignment operator.lblGrossPay.Text = txtFirstName.Text

Takes content of the Text property value of the txtFirstName control and copies it into the Text property of the lblGrossPay control. (p. 101)

Page 51: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 51

Assignment Examples

Syntax: ControlName.Property Name

Example: lblDirections.Visible = True

Invalid Assignment: True = lblDirections.Visible

Page 52: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 52

PictureBox Control (pp. 52-53)

AutoSize Adjusts automatically to fit image size; maintains proportions.

CenterImage If size is larger than image, image is centered. If image is larger than control, image displayed in center and clipped to fit.

Normal Aligns image with upper left corner of control; Clips images if too large for control size.

StretchImage Scales image to fit within the control’s size; Stretches if image is smaller than control; Shrinks if image is larger than control.

Page 53: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 53

Locked Controls

Prevents controls from being accidentally moved at design time.

(1)Right-click empty area of form.

(2)Choose Lock Controls.

Page 54: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 54

Types of Errors (p. 88)

Compile Error Syntax errors

Misspelled key words Incorrect use of operators Incorrect punctuation

Jagged blue underline and description displayed Run-Time Error

Not syntax Attempt to perform an operation that VB.NET can’t

carry out

Page 55: Chapter 21 Creating Applications with Visual Basic.NET Chapter 2

Chapter 2 55

Compile Errors

ToolTip stating compile error

Also incorrect use of .Text; should betxtState.Clear()