47
Chapter 8: Writing Graphical User Interfaces Visual Basic .NET Programming: From Problem Analysis to Program Design

Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Embed Size (px)

Citation preview

Page 1: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Chapter 8: Writing Graphical User Interfaces

Visual Basic .NET Programming:

From Problem Analysis to Program Design

Page 2: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 2

Objectives

• Learn about the GUI classes in VB .NET

• Understand the code generated by the Windows Form Designer

• Explore GUI design principles

• Review forms, buttons, and labels

• Handle events

• Work with additional GUI controls

Page 3: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 3

Introducing the GUI Classes in VB .NET

• Form class

– Member of System.Windows.Forms namespace

– Used to implement a window

• GUI classes take advantage of inheritance

– Most are subclasses of Control class

• Inheritance

– Class of objects takes on characteristics of another class and extends them

Page 4: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 4

Page 5: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 5

Page 6: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 6

Page 7: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 7

Understanding the Code Generated by the Windows

Form Designer

• Use drag-and-drop features of Windows Form Designer to create GUIs

• Visual programming consists of:– Creating a form– Setting its properties– Adding components– Setting component properties– Adding event handling code

Page 8: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 8

Understanding the Code Generated by the Windows Form Designer (continued)

• VB .NET generates code that handles details of form design

– Called class definition

– Specifies attributes and methods that make form work

Page 9: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 9

Exploring the FormDemo Program

• Class definition – Begins with class header

– Ends with End Class statement

• Class keyword– Identifies first line of code as class header

– Followed by class name

• Public keyword – Class can be used by anyone

Page 10: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 10

Exploring the FormDemo Program (continued)

• Inherits keyword – Indicates name of superclass

• When working in IDE:– Most generated code collapsed under heading

“Windows Form Designer generated code”• Begins with #Region directive

• Ends with #End Region directive

– View by clicking expand node

– Generated code is complex

Page 11: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 11

Exploring the FormDemo Program (continued)

• Constructor

– Special method

• Automatically invoked when instance of class is created

– Windows Form Designer provides default constructor for each form

– Method is named New

Page 12: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 12

Exploring the FormDemo Program (continued)

• MyBase keyword – Refers to superclass of current object

– MyBase.New( ) calls superclass constructor

• Dispose method – Destructor

– Releases system resources

• Container– Object that holds other components

Page 13: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 13

Exploring the FormDemo Program (continued)

• Friend keyword

– Specifies that object is accessible only within files that compose application

• WithEvents keyword

– Control instance may be source of events

– Events will be handled by methods that include Handles controlName.eventName clause

Page 14: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 14

Exploring the FormDemo Program (continued)

• Private keyword

– Indicates that method is accessible only within own class

• Me keyword

– Refers to current object

• Parameter list

– Identifies variables that receive values when method invoked

Page 15: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 15

Exploring GUI Design Principles

• Many standards that guide creative process of user interface design

• Basic principles include:– Creating consistent look and feel

– Ensuring ease of use

– Minimizing data entry errors

– Providing feedback to users

– Adhering to standard naming conventions

Page 16: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 16

Creating a Consistent Look and Feel

• Look and feel

– Style and appearance of form

– Includes choice of colors and fonts for various controls

• Key design goal

– Provide consistent look and feel as user moves among various forms

Page 17: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 17

Ensuring Ease of Use and Minimizing Data-Entry Errors

• Purpose of each control and layout of form should be intuitive

• Placement and grouping of controls should be logical

• Form should appear uncluttered• Good practice:

– Design input forms to minimize required keystrokes

Page 18: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 18

Providing Feedback to the User

• Provide feedback to user

– When certain actions have been completed

– To inform user when data entry errors have occurred

• Present feedback in consistent manner

Page 19: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 19

Naming Conventions

• Adhering to set of naming conventions

– Improves program readability

– Facilitates program maintenance

• Assign meaningful variable names

– Change Name property for each control

• Variable name should reflect both

– Type of control

– Purpose of control

Page 20: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 20

Page 21: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 21

Reviewing Forms, Buttons, and Labels

• All GUI components with visual representation are subclasses of Control class

– Control class is a subclass of Component class

• Set many properties in Properties window

– Can also manipulate properties through code in event handlers

Page 22: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 22

Page 23: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 23

Page 24: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 24

Handling Events

• To create event handling procedure:

– Double-click control in Forms Designer window

– VB .NET places method header in Code window

– Add statements that handle event

– Code is added to most commonly used event handler for control

Page 25: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 25

Handling Events (continued)

• Alternate method for adding event-handling code:

– Press F7 to open Code window

– Select class name from Class Name list box

– Select event from Method Name list box

– Add statements that handle event

Page 26: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 26

Page 27: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 27

Page 28: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 28

Working with Additional GUI Controls

• Other commonly used controls:

– Text boxes

– Combo boxes

– Check boxes

– Radio buttons

– List boxes

Page 29: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 29

Using Text Boxes and Combo Boxes

• Text box

– Used to display textual information

– Enable inputting of text from keyboard

• Combo box

– Extends functionality of text box

– Provides ability to select item from predetermined list of values

• Can also type value

Page 30: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 30

Page 31: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 31

Page 32: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 32

Page 33: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 33

Using Check Boxes and Radio Buttons

• Check boxes and radio buttons provide ability to select from options

• States:– Checked/Selected

– Unchecked/Not selected

• Check box – Appears as small white box

– Usually includes label that identifies purpose

Page 34: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 34

Using Check Boxes and Radio Buttons (continued)

• Radio buttons

– Appear as small white circles

– Have captions that identify purpose

• Group of radio buttons

– Represents set of related options

– Options are mutually exclusive

Page 35: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 35

Page 36: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 36

Page 37: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 37

Page 38: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 38

Using List Boxes and Checked List Boxes

• List boxes and checked list boxes

– Provide ability to select one or more items from predetermined list of values

• List box

– Select one or more items from list

• Checked list boxes

– Include check box to left of each item in list

Page 39: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 39

Page 40: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 40

Page 41: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 41

Example 8-5: The Fast Food Menu Program

• For Each statement

– Similar to For Next

– Loops through a collection of items

– Specifies:

• Variable

• Collection

Page 42: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 42

Programming Example: ABC Computers

• Input – Type of processor

– Operating system

– Peripherals desired

– State of residence

• Output – Order summary

– Total cost

Page 43: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 43

Page 44: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 44

Programming Example: ABC Computers (continued)

• Main Algorithm1. Define cost variables at the class level

2. Create an event handler for each control that captures data from the user

3. When the user selects an option from one of the GUI controls, let the event handler for that control:a. Determine the price of that option

b. Calculate total cost and display the order summary

Page 45: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 45

Summary

• VB .NET generates a class definition – Specifies attributes and methods that make forms

work

– Generated code collapsed under heading “Windows Form Designer generated code”

– Generated code is complex

• Constructor – Special method automatically invoked when

instance of class is created

Page 46: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 46

Summary (continued)

• Container

– Object that holds other components

• Look and feel

– Style and appearance of form

– Use standards to ensure consistent look and feel

• Follow naming conventions for controls

Page 47: Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design

Visual Basic .NET Programming: From Problem Analysis to Program Design 47

Summary (continued)

• Form components include:

– Text box

– Combo box

– Check box

– Radio button

– List box

– Checked list box