Download presentation
Presentation is loading. Please wait.
Published byVivien King Modified over 9 years ago
1
Chapter 3 Introduction to Event Handling and Windows Forms Applications
2
Class 3: Windows Form Applications Procedural and Event-driven Applications Design Issues Visual Development Event Handling
3
Comparing Command-line and Form-based Interfaces User interfaces can be roughly categorized into two types –Command-line interfaces use textual input and output The end user interacts with an application by typing commands –Most Windows user interfaces are form-based visual interfaces The end user interacts with an application through its visual elements
4
Differences Between Console and Windows Application Projects Console projects are procedural Console projects have a textual (character- based) user interface Windows Application projects are event driven Windows Application projects have a visual user interface consisting of buttons and boxes
5
Figure 3-1: Comparing a Command-line Interface and a Form-based Interface
6
Principles of a User Interface Control – The end user should control the application User-friendliness – The interface should help the end user accomplish tasks Intuitiveness – The interface should follow a direct style that proceeds logically Consistency – The user interface should have consistent fonts, and buttons should have the same shape and size Feedback – The interface should provide clear and immediate feedback
7
Principles of a User Interface (continued) Graphics – Avoid the use of unnecessary graphics Input – Minimize transitions between the keyboard and mouse where possible Screen resolution – The user interface should adapt to different screen resolutions –Users may be visually impaired, requiring larger fonts
8
Designing a User Interface A user interface should be designed before it is implemented –Design the user interface using a tool such as Visio –A simple pencil and paper sketch will often do
9
Figure 3-2: Designing a User Interface
10
Principles of Control Design Alignment – Align control instances vertically or horizontally Balance – Distribute control instances evenly about the form Color – Use soft colors with adequate contrast between foreground and background colors –Users may be colorblind Function grouping – Group control instances based on their function Consisting sizing – Control instances should have the same size
11
Figure 3-3: A Poor User Interface
12
Figure 3-4: An Improved User Interface
13
Creating a Windows Application Project The steps to create a Console Application project and Windows Application project are nearly the same Use the New Project Dialog box –Use the Windows Application template The templates vary based on the Visual Studio edition –Assign a name to the solution, as necessary
14
The Solution Explorer The role of the Solution Explorer is the same for Console and Windows Application projects –It organizes the various parts of a solution The project and support files are the same The role of project references is the same –However, Windows Application projects reference different assemblies and namespaces Form files appear instead of module files
15
The Toolbox and Windows Forms Designer The Toolbox and Windows Forms Designer are used together to create an application ’ s visual interface The Toolbox contains controls Controls created on a form are called control instances View the Windows Forms Designer by –Selecting the form in the Solution Explorer and clicking View Designer –Clicking View, Designer on the menu bar
16
Characteristics of a Form A title bar appears along the top of a form An optional control box appears in the title bar –The control box contains Minimize, Maximize, Restore, and Close buttons –The buttons on the control box may be disabled or hidden An icon appears on the left side of the title bar Below the title bar appears an optional menu The region inside a form's border is called the client area
17
Figure 3-8: The Organization of Controls in the Toolbox
18
Figure 3-9: The Properties Window
19
Configuring Textual and Hierarchical Properties Properties such as Name and Text store textual values –Edit these values directly in the Value column A plus or minus sign appears next to hierarchical properties –Click plus to expand and minus to collapse Some properties display a drop-down list Some properties display a visual editor
20
Figure 3-14: Visual Editor for the TextAlign Property
21
Using Visual Studio to Create and Configure Control Instances To create a control instance –Click the control in the Toolbox to select it –Using the mouse, draw the region of the control instance on the form To delete a control instance, click the control instance to select it and press Delete
22
Moving and Resizing a Control Instance Move a control instance by dragging it on the form Resize a control instance by –Clicking the control instance to select it –Resize the control instance by dragging the sizing handles
23
Working with Multiple Control Instances Select multiple control instances by –Holding down the Shift key and clicking the desired control instance –Dragging a rectangle around the desired control instance with the Pointer tool Only part of the control instance needs to appear in the rectangle to be selected
24
Introduction to Visual Studio Controls Controls discussed in this chapter –The PictureBox displays graphical images –The Label control displays text –The Button control is used to perform a specific task when clicked –The OpenFileDialog control displays a dialog box from which the end user can select a file to open –The ToolTip control displays informational pop-up messages
25
Understanding the Name Property Every form and control instance has a name –Visual Studio assigns a default name to forms and control instances The value of the Name property is used to reference a form or control instance programmatically Assigning meaningful names creates more readable code –Use consistent prefixes for form and control instance names
26
Control Prefixes ControlPrefix Button btn Label lbl OpenFileDialog ofd PictureBox pic ToolTip tip
27
The OpenFileDialog Control The CommonDialog class is the base class for other classes –These classes are used to display standard dialog boxes The OpenFileDialog control allows the end user to select a file to open The control instance appears in a resizable tray below the Windows Forms Designer at design time Call the ShowDialog method to display the control instance
28
Figure 3-20: Open Dialog Box
29
Using the OpenFileDialog Control The ShowDialog method displays the dialog box to the end user The FileName property contains the file name selected by the end user –The OpenFileDialog control does not actually open a file
30
ToolTips (Introduction) ToolTips appear as pop-up windows when the mouse hovers over a control instance –Use to display informational messages to the end user Create ToolTips with the ToolTip control –The ToolTip control is a provider control, meaning that it works with other control instances –Use one ToolTip control instance for all the other control instances on a form
31
Creating Code for a Windows Application Project Code executes as the end user interacts with the form ’ s control instances –This code is called the code behind the form The code behind the form is made up of two parts –One part is automatically generated by the Windows Forms Designer –The second part contains the code you write
32
Introduction to Class Blocks and Partial Classes A form is made up of two files –The Windows Forms Designer generated code appears in the file named frmName.Designer.vb –Developer created code appears in the file frmName.vb –A form is a class having the following structure Public Class frmMain ‘ statements End Class
33
The Syntax of a Class Block Class blocks and Module blocks have a similar syntax –The Class and End Class keywords mark the beginning and end of a Class block –A Class block contains procedures The procedure named New is the entry point, and is called the constructor A Class block can contain a Main() procedure, but it is not the entry point
34
Introduction to Partial Classes A partial class is a class whose code appears in multiple files –Visual Studio uses partial classes to store automatically generated code –Multiple developers can use partial classes to create code independently
35
Figure 3-25:Windows Forms Designer Generated Code
36
Introduction to Event Handlers An event hander is a procedure containing statements that execute when the end user performs an action –Windows executes different event handlers as different events fire –Windows fires a Click event when the end user clicks a button –Different types of controls support different events
37
Figure 3-26: Executing an Event Handler
38
Characteristics of an Event Handler An event handler is a form of Sub procedure All event procedures have two arguments –Arguments send information to a procedure The Handles clause marks the procedure as an event handler Use the Windows Forms Designer and Code Editor to create event handlers automatically –Double-click the control instance in the Windows Forms Designer –Use the Class Name and Method Name Combo Boxes to create and select event handlers
39
Figure 3-27: Class Name Combo Box
40
Figure 3-28: Method Name Combo Box
41
Assignment Statements (Introduction) Assignment statements are similar to algebraic statements –They contain a left side and a right side –An equals (=) sign separates the left side and the right side –The expression on the left side is evaluated and stored in the property appearing on the right side
42
Assignment Statements (Example 1) Store a literal value in the form ’ s Text property –String literal values are surrounded by double quotation marks –Me.Text is the form ’ s Text property Me.Text = "Chapter 3"
43
Assignment Statements (Example 2) Assignment statements can be used with numeric values Double quotation marks do not surround literal values Store 0 in the Top and Left properties of a PictureBox control instance named picCurrent picCurrent.Top = 0 picCurrent.Left = 0
44
Assignment Statements (Example 3) Boolean values can be used in assignment statement –True and False are Boolean values –The Visible and Enabled properties store Boolean values Set the Visible and Enabled properties for control instances btnDemo.Visible = True picCurrent.Enabled = False
45
Reading an Image Call the FromFile method of the System.Drawing.Image class The method accepts a file name as the argument
46
Reading an Image (Example) Read the Image named "C:\House1.jpg" into the PictureBox control instance named picDemo picDemo.Image = _ System.Drawing.Image.FromFile( _ "C:\House1.jpg")
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.