440 likes | 573 Views
Chapter 3. Introduction to Event Handling and Windows Forms Applications. Class 3: Windows Form Applications. Procedural and Event-driven Applications Design Issues Visual Development Event Handling. Comparing Command-line and Form-based Interfaces.
E N D
Chapter 3 Introduction to Event Handling and Windows Forms Applications
Class 3: Windows Form Applications • Procedural and Event-driven Applications • Design Issues • Visual Development • Event Handling
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
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
Figure 3-1: Comparing a Command-line Interface and a Form-based Interface
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
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
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
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
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
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
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 controlinstances • View the Windows Forms Designer by • Selecting the form in the Solution Explorer and clicking View Designer • Clicking View, Designer on the menu bar
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 clientarea
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
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
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
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
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
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
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
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
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 providercontrol, meaning that it works with other control instances • Use one ToolTip control instance for all the other control instances on a form
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
Introduction to ClassBlocks 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
The Syntax of a Class Block • Class blocks and Module blocks have a similar syntax • The Class and EndClass 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
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
Introduction to Event Handlers • An event handeris 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
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
Assignment Statements (Introduction) • Assignmentstatements 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
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"
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
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
Reading an Image • Call the FromFile method of the System.Drawing.Image class • The method accepts a file name as the argument
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")