Title: Desktop Applications: Core Concepts
1Chapter 2
- Desktop ApplicationsCore Concepts
2Objectives (1)
- Explore the types of VB .NET applications
- Understand the organization of a VB .NET
application - Examine the windows that comprise the Visual
Studio .NET IDE - Work with a form module
- Understand the basic operation of Windows forms
- Learn how to manage forms
3Objectives (2)
- Understand the purpose of VB .NET controls,
create control instances, and set properties for
those control instances - Set the tab order for controls
- Understand the operation of VB .NET event
handlers - Dynamically create event handlers and control
instances at runtime - Understand the collections of controls
4Previewing the Completed Solution
- Most chapters, including this one, present a
completed solution at the beginning of the
chapter - The completed solution has numerous comments
- Each chapter contains a startup solution
- By completing hands-on steps corresponding to the
startup solution, you end up with the completed
solution - For brevity, the startup solution does not
contain comments
5Types of Visual Basic .NET Applications (1)
- Chapter 1 introduced the Console Application
- Applications are called projects
- This chapter introduces the Windows Application
and other Visual Studio .NET project types - Project types
- Windows Application run directly by the end
user - Class Library contain components (namespaces)
intended for use by other developers - Windows Control Library contain custom controls
6Types of Visual Basic .NET Applications (2)
- Project types
- ASP.NET Web Application applications that run
on the Internet or an intranet - ASP.NET Web Service Web applications having
different clients other than a Web browser - Web Control Library contains custom controls
designed for use on the Web - Console Application run from a Command Prompt
window - Windows Service applications with which the
user has no interaction
7Creating a New Project
Project types pane
Application templates
Solution name
Solution folder
8Organization of a Visual Basic .NET Application
- Applications are organized into solutions
- Solution file has a file suffix of .sln
- Solution file is a text file
- All elements of a solution are stored in a
separate directory called a solution directory - Solution directory (folder) is created when the
solution is created - Cannot create a solution in an existing directory
9The Solution Explorer
Show All Files button
Click to expand or collapse folders
10Project Files
- Each solution contains one or more projects
- Project files are XML documents
- Project file contains
- The type of project
- References to namespaces and assemblies
- Compilation information specific to a project
- Module files make up the forms and other classes
in the project
11Solution, Project, and Module Relationships
12Summary of the IDE Windows
- The Visual Studio .NET IDE operates in two modes
- Tabbed documents mode is new to .NET and is used
in this book - MDI mode resembles previous versions
- Types of windows
- Document windows appear on tab groups
- Edit a form's visual interface with the Win Forms
Designer - Edit a form's corresponding code with the Code
Editor - Only one document window on a tab group is active
at a time - Tool windows can be docked, auto hidden, or float
on the desktop
13Document Windows
Document windows appear on a tab group
Solution Explorer appears as a tool window
14Tool Windows
- The Solution Explorer manages the solution, its
projects, and the files in those projects - The Properties window is used to set design-time
properties for the form and selected control
instances - The Toolbox contains controls
- Controls appear on different tabs based on their
purpose - Possible to create custom tabs
15Tool Window Appearance
- Auto Hide - tool window is hidden along an edge
of the desktop - Auto hidden windows must be docked along an edge
of the IDE - Dockable windows can be docked along an edge of
the IDE - Floating windows can appear anywhere on the
desktop - Note some tool windows can appear as document
windows
16The Properties Window
- Properties window contains two columns
- Left column lists property names
- Edit property value in right column
- Object list box displays selected control
instances - Displays nothing when multiple control instances
are selected - Properties containing an object or structure can
be expanded or collapsed - Some properties are set using a dialog box called
a property editor - Different property editors exists for properties
storing specific data types such as Color - Properties can be set for multiple control
instances simultaneously
17The Properties Window (Color property editor)
Object list box
Web tab active in Color property editor
18The Properties Window (Alignment property editor)
Middle-right justification
19The Win Forms Designer
- Always appears as a document window on a tab
group - Optional resizable tray appears for invisible
control instances - Create a control instance on a form by clicking a
control in the Toolbox and positioning it on the
form - Move or resize control instances by clicking them
to activate them and using sizing handles - Visual Studio .NET writes statements in the
corresponding module to create control instances
20The Code Editor
- The text editor used specifically to edit VB .NET
module files - Features
- Automatic statement indentation
- Statement completion of If, Select Case, and
looping statements - Clipboard ring supports multiple clipboards
similar to Office - Collapse and expand procedures and regions as
necessary - Copy and paste rectangular code regions
21The Code Editor (Example)
Class Name list box
Method Name list box
Expand or collapse region
Windows Form Designer generated code is collapsed
22The Structure of a Win Forms Designer Generated
Class Module
- Win Forms Designer generates statements as
control instances are created on a form - Statements are hidden inside of a region block
Public Class frmSplash Inherits
System.Windows.Forms.Form Region " Windows
Form Designer generated code " ' Statements
End Region End Class
23Constructors
- A constructor is a Sub procedure named New
- VB .NET calls a constructor (New procedure) when
the CLR creates an instance of a class - Example
- Public Sub New(ByVal AircraftType As String
- ByVal pstrOrigin As String, _
- ByVal pstrDestination As String
- ' Statements
- End Sub
24Destructors
- A destructor is called when a class instance gets
destroyed - Method is named Dispose
25Statements Generated by the Win Forms Designer
- Win Forms Designer declares variables to store
all control instances - Statements appear in a procedure named
InitializeComponent - Do not modify the contents of this procedure
- Declare a variable to store a Label control
instance - Friend WithEvents lblName As _
- System.Windows.Forms.Label
- Create the control instance
- Me.lblName New _
- System.Windows.Forms.Label()
26Configuring Control Instances
- Win Forms Designer generates statements to
configure control instances - Configure the Label control instance named
lblCompany - Me.lblCompany.ForeColor _
- System.Drawing.Color.Azure
- Me.lblCompany.Location New _
- System.Drawing.Point(40, 64)
- Me.lblCompany.Name "lblCompany"
27The Basics of Windows Forms
- Every Windows Application project contains one or
more forms - Each form is derived from the System.Windows.Forms
.Form class
28System.Windows.Forms.Form Class (1)
- Properties
- AcceptButton defines the button designated as the
AcceptButton - Click event handler fires when user presses Enter
- AutoScroll defines whether scroll bars will
appear when control instances do not fit in
form's visible region - Click event handler fires for CancelButton when
user presses Escape key - FormBorderStyle defines border characteristics
- StartPosition defines initial position on desktop
29System.Windows.Forms.Form Class (2)
- Events
- Enter event fires when form gets focus
- Leave event fires when form loses focus
- Methods
- Close method unloads form
- Hide method makes form invisible without
unloading it - Show method displays a modeless form
- ShowDialog method displays a modal form
30Setting the Startup Object
- Set the Startup object using the Project
Properties Pages dialog box
General tab selected
Startup object
31Displaying a Form
- Create an instance of the Form class
- New keyword creates class instance
- Dim frmSplashNew As New frmSplash()
- Call the ShowDialog method to display a modal
form - frmSplashNew.ShowDialog()
32Execution Flow of Displaying Modal Dialog Boxes
33Adding a Module
Select Module to add a module
Specify module name
34The Basics of Controls
- Controls appear in the Toolbox and are part of
the .NET Framework class library - Controls are derived from System.Windows.Forms.Con
trol - Control class supplies keyboard input and mouse
functionality - Control class updates display as necessary
35Common Controls
- Button, CheckBox, and RadioButton form clickable
buttons - ComboBox and ListBox are list controls
- Display items in a columnar list
- Horizontal and vertical scroll bars are
scrollable controls - Use to select a value from a range of values
- TextBox and RichTextBox used to edit text
- Label class displays text
- Panel control contains other controls
36Changes to Controls
- VB .NET no longer supports control arrays
- Controls no longer have a Caption property
- Text property replaces Caption
- ToolTips are implemented using ToolTip control
- Positional properties are implemented differently
37Control Hierarchy
38The Timer Control
- Tick event fires at regular intervals
- Tick event only fires when Enabled property is
True - Interval property stores 32 bit Integer
- Value is measured in milliseconds
- 1000 is equal to 1 second
39Menus and Menu Items
- MainMenu control is used to create a menu
- Typically one menu per form
- Create menus and menu items in place on the Win
Forms Designer - character in text defines hot key
- Each menu and menu item is an instance of the
MenuItem class
40The MenuItem Class
- Properties
- Checked property designates a checked menu item
- Boolean Enabled property defines whether control
instance is enabled or not - Shortcut and ShowShortcut properties define
menu's shortcut key - Text property contains text appearing on menu
- Visible property defines whether menu item is
visible or not - If a menu is not visible, its children are not
visible - Events
- Click event fires when user clicks menu item
41Creating a Menu
Menu titles and menu items are edited in place
MainMenu control instance appears in resizable
tray
42Editing Menu Names
43The CheckBox Control
- Contains a box the user checks and a textual
description - Operates as a two-state or three-state control
- Two-state control can be checked or unchecked
- Use Checked property and CheckedChanged event in
two-state mode - Three-state control can be checked, unchecked, or
indeterminate - Use CheckState property and CheckStateChanged
event in three-state mode
44The Label and TextBox Controls (1)
- The TextBox displays an editable text region
- Properties
- AcceptsReturn causes return character to be
embedded in text if True. If False, pressing
Enter fires Click event handler for default
button - AcceptsTab, if true, causes tab key to be
embedded in text - Lines contains an array of strings. Each string
represents 1 line - MaxLength defines maximum number of characters
the user can enter - ReadOnly property defines whether or not text can
be changed
45The Label and TextBox Controls (1)
- Methods
- Clear removes contents
- Cut, Copy, and Paste perform clipboard operations
- Focus method sets input focus
- Events
- Enter event fires when control instance gets
focus - Leave event fires when control instances loses
focus - Validating event fires before control instance
loses focus - Event may be cancelled
46The Button Control
- The Button control supplies a clickable button
- Create a Click event handler to execute when user
clicks the button - FlatStyle property defines 3-D appearance of the
button - Image property contains optional image
- ImageAlign property defines alignment of the
image - PerformClick method fires a Click event
47Scroll Bar Controls
- Vertical and horizontal scroll bars work the same
way - Value property stores current value
- Integer Minimum and Maximum properties define the
valid range for the Value property - Minimum must be less than Maximum
- SmallChange and LargeChange properties define
amount of change to the Value property when the
scroll arrows and scroll region are clicked - ValueChanged event fires when Value property
changes
48Radio Buttons
- A group of radio buttons is called a button group
- If multiple button groups exists on a form,
create each button group in a GroupBox or Panel
control instance
49List and Combo Boxes
- Properties
- Items property contains a reference to Items
collection - Each item represents one item appearing in the
list - Add items at design-time using the String
Collection Editor - SelectedIndex contains 0-based index of selected
item - Value is -1 if no item is selected
- SelectedItem contains a reference to the selected
item - Note data type is System.Object to a list box can
store anything - Methods
- ClearSelected deselects items
- Events
- SelectedIndexChanged event fires when the user
selects a different item
50Adding a List Item at Runtime
- Call the Add method of the Items
collectionlstDestination.Items.Add("Atlanta") - Call the AddRange method to add multiple items
- Assume that pstrCities is an array of strings
- lstDestination.Items.AddRange(pstrCities)
51The ToolTip Control
- Considered a provider control as it works in
conjunction with another control - Properties
- AutoPopupDelay defines how long the ToolTip is
displayed - InitialDelay property defines delay interval
before first displaying the ToolTip - Other control instances support a ToolTip on xxx
property - Contains text appearing to the user
52Understanding Tab Order
- Tab order representsorder in which control
instances get focus - Click View, Tab Order to set the tab order
53Introduction to Event Handlers
- Characteristics
- Always accept two arguments
- The first argument named sender contains a
reference to the object firing the event - Second argument derives from System.EventArgs
class - Use second argument to get information about
event - Handles clause causes a procedure to handle an
event
54Event Classes
- KeyEventArgs class has properties to get keyboard
character pressed - KeyPressEventArgs gets ASCII character that was
pressed - MouseEventArgs used to determine which mouse
button was pressed or released - CancelEventArgs used with Validating event
- Allows developer to cancel event when invalid
input is discovered
55Multicast Event Handlers
- One event handler handles an event for multiple
control instances - Include Handles class followed by a comma
separated list of control.event names - Handles rad737.CheckedChanged, _
rad777.CheckedChanged
56Casts
- CType function converts an object from one type
to another - First argument contains object to convert
- Second argument contains type of destination
object - Example Convert to RadioButton
- radCurrent Ctype(sender, _
System.Windows.Forms.RadioButton)
57Dynamic Event Handlers
- AddHandler statement creates a new event handler
- RemoveHandler removes a dynamic event handler
- AddressOf keyword contains name of procedure
registered as event handler - AddHandler chkSeats.CheckedChanged,_
- AddressOf _ chkSeats_CheckedChanged
58Collections of Controls
- Controls collection contains a reference to each
control instance created directly on a form - Container controls have their own Controls
collection - Enumerate Controls collection with a For Each
loop - ExampleDim ctlCurrent As Control
- For Each ctlCurrent In Me.Controls
- ' Statements
- Next