Chapter 13: Advanced GUI and Graphics Visual Basic.NET Programming: From Problem Analysis to Program...
-
Upload
megan-mccormick -
Category
Documents
-
view
229 -
download
4
Transcript of Chapter 13: Advanced GUI and Graphics Visual Basic.NET Programming: From Problem Analysis to Program...
Chapter 13: Advanced GUI and Graphics
Visual Basic .NET Programming:
From Problem Analysis to Program Design
Visual Basic .NET Programming: From Problem Analysis to Program Design 2
Objectives
• Work with additional GUI controls
• Draw lines, rectangles, and ovals
• Develop GUI classes that interact with a PD class
• Create applications with multiple forms
• Simulate a DA class
• Create an integrated system that simulates interaction with a database
Visual Basic .NET Programming: From Problem Analysis to Program Design 3
Working with Additional GUI Controls
• Other GUI components:
– Menus
– Group boxes
– Tab pages
• Namespace:
– System.Windows.Forms
• Control
– Superclass of other GUI classes
Visual Basic .NET Programming: From Problem Analysis to Program Design 5
Using Group Boxes and Panels• Containers
– Enable you to visually and logically organize groups of related controls
• Add components – By drawing them inside group box or panel
• Move panel or group box– All controls within it move together
• Common use of group boxes and panels – Group set of radio buttons
Visual Basic .NET Programming: From Problem Analysis to Program Design 9
Using Tree Nodes and Tree Views
• Tree view
– Displays group of hierarchically related items
– Each item is represented as instance of TreeNode class
– Useful for displaying hierarchical information
Visual Basic .NET Programming: From Problem Analysis to Program Design 11
Using Tree Nodes and Tree Views (continued)
• Tree node
– Has a value
– Types:
• Parent
• Child
• Sibling
Visual Basic .NET Programming: From Problem Analysis to Program Design 14
Example 13-2: Using Tree Views and Tree Nodes
• Declaring variables:
– Dim rootNode, profNode As TreeNode
• Adding to tree:
– control.Nodes.Add(myString)
• Finding selected node:
– myNode = trvCSC.SelectedNode
Visual Basic .NET Programming: From Problem Analysis to Program Design 15
Example 13-2: Using Tree Views and Tree Nodes
(continued)
• Accessing related nodes:
– myNode.Parent
– myNode.PrevNode
– myNode.NextNode
• Accessing node text:
– myNode.NextNode.Text
Visual Basic .NET Programming: From Problem Analysis to Program Design 16
Using Tab Controls and Tab Pages
• Tab control
– Provides functionality for set of tab pages
– Instance of TabPage class
– Useful when
• Form requires large number of controls
• Controls can be grouped into logical subsets
– User switches between subsets by clicking appropriate tab
Visual Basic .NET Programming: From Problem Analysis to Program Design 20
Using Main Menus and Menu Items
• MainMenu and MenuItem classes
– Create menus and submenus for a form
• Main menu control
– Serves as container for menu structure
• Menu items
– Represent individual menu choices within structure
Visual Basic .NET Programming: From Problem Analysis to Program Design 21
Using Main Menus and Menu Items (continued)
• Menu purposes:
– Provide functionality for common tasks, such as
• Opening and closing files
• Editing text
• Accessing help features
– Can use menus instead of buttons to perform many tasks
Visual Basic .NET Programming: From Problem Analysis to Program Design 26
Drawing Lines, Rectangles, and Ovals
• VB .NET includes a rich set of classes for drawing graphical elements
• System.Drawing namespace
– Contains classes and structures needed for drawing graphical objects
– Automatically available in Windows applications
• No need to import namespace
Visual Basic .NET Programming: From Problem Analysis to Program Design 27
Drawing Lines, Rectangles, and Ovals (continued)
• Structure
– Can include:
• Constructors
• Methods
• Properties
• Events
– Value type
• Variable contains actual data for structure
Visual Basic .NET Programming: From Problem Analysis to Program Design 30
Example 13-5: Using Graphics
• Generate paint event for form:
– Invalidate()
• Event handler parameters
– Sender
• Reference to object that raised event
– e
• Object specific to event being handled
Visual Basic .NET Programming: From Problem Analysis to Program Design 31
Example 13-5: Using Graphics (continued)
'declare variables needed for drawing
Dim instrumentColor As Color
Dim myBrush As SolidBrush
Dim myPen As Pen
Visual Basic .NET Programming: From Problem Analysis to Program Design 32
Example 13-5: Using Graphics (continued)
'draw a white rectangle to serve as drawing canvas
g.FillRectangle(New SolidBrush(Color.White), 18, 160, 492, 248)
'Define a brush using the selected Color
myBrush = New SolidBrush(instrumentColor)
'Define a pen using the selected Color
myPen = New Pen(instrumentColor)
Visual Basic .NET Programming: From Problem Analysis to Program Design 33
Developing GUI Classes that Interact With a PD Class
• Create GUIs that interact with problem domain (PD) class
• Customer PD class contains – Three attributes:
• Name
• Address
• PhoneNo
– Parameterized constructor,
– Get and set accessor methods
Visual Basic .NET Programming: From Problem Analysis to Program Design 34
Adding a Customer
• Visual programming process
– Generates variables necessary to create GUI
– Does not generate all variables needed to respond to events
• Variables must be added to source code through the Code Editor window
Visual Basic .NET Programming: From Problem Analysis to Program Design 35
Finding a Customer
• Use ArrayList to simulate interaction with database
– Customer instances created from hard-coded data
Visual Basic .NET Programming: From Problem Analysis to Program Design 36
Creating Applications with Multiple Forms
• Link multiple forms through GUI that allows you to navigate between forms
– Referred to as main menu GUI
– Uses buttons to navigate between forms
• Event handlers hide and show forms:
– frmAddCustomerGUI.ShowDialog()
– Me.Show()
Visual Basic .NET Programming: From Problem Analysis to Program Design 37
Creating Applications with Multiple Forms (continued)
• Modal dialog box
– Form must be dismissed before next statement in procedure will be executed
– Use ShowDialog method
• Data Access (DA) class
– Enables sharing of data among forms
Visual Basic .NET Programming: From Problem Analysis to Program Design 39
Simulating a DA Class
• Simulate interaction with database by using ArrayList of customers
• DA classes provide data storage and retrieval services
Visual Basic .NET Programming: From Problem Analysis to Program Design 40
Simulating a DA Class (continued)
• DA methods
– Initialize
– GetAll
– AddNew
– Find
– Update
Visual Basic .NET Programming: From Problem Analysis to Program Design 41
Creating an Integrated System that Simulates Interaction
With a Database
• Modify previously developed forms to use DA class
Visual Basic .NET Programming: From Problem Analysis to Program Design 42
Modifying the Main Menu Form
• Initialize simulated database
– By invoking Initialize method of CustomerDA class
– Invoked by event handler for Load event
Visual Basic .NET Programming: From Problem Analysis to Program Design 43
Modifying the Add Customer Form
• Event handler for Add Customer button
– Invokes AddNew method of DA class
– To add Customer instance to simulated database
Visual Basic .NET Programming: From Problem Analysis to Program Design 44
Modifying the Find Customer Form
• Does not include CreateCustomers method
• PopulateListBox method now invokes DA class method GetAll
– To obtain ArrayList
• Event handler for list box
– Invokes DA class method Find
• Event handler for Update button
– Invokes DA class method Update
Visual Basic .NET Programming: From Problem Analysis to Program Design 45
Programming Example: Interacting with Multiple PD
Classes• PD classes:
– Person– Student– Professor– Phone
• DA classes:– Student– professor
Visual Basic .NET Programming: From Problem Analysis to Program Design 47
Summary
• Group boxes and panels
– Containers that enable you to visually and logically organize groups of related controls
• Tree view
– Displays group of hierarchically related items as expandable outline
• Tab control
– Provides functionality for a set of tab pages
Visual Basic .NET Programming: From Problem Analysis to Program Design 48
Summary (continued)
• Main menu control
– Serves as container for menu structure
• Menu items
– Represent individual menu choices within that structure
• Graphics class contains methods for drawing shapes
• Structures are value types