Presentation is loading. Please wait.

Presentation is loading. Please wait.

Validating User Input Lesson 5.

Similar presentations


Presentation on theme: "Validating User Input Lesson 5."— Presentation transcript:

1 Validating User Input Lesson 5

2 Objectives

3 Windows Users input Model
Windows Forms and WPF applications are constantly receiving user inputs from the keyboard, mouse, and stylus. Knowing when to handle user input events will enable the application to maintain performance and provide a better user experience. Table 4-1 shows common Windows Form Events

4 Windows Users input Model
When analyzing the application requirements and designing your Form/Window UI, you need to consider at what level you want to handle each user input device action being generated. The three user input processing levels are: Device Action Control Action Form/Window Action

5 Input Device User Inputs
Windows applications currently support three types of input devices: Keyboard Mouse Stylus As a user interacts with an input device, actions are generated and your windows application can implement event handlers.

6 Keyboard User Inputs Although both Windows Forms and WPF applications support keyboard user inputs, they have different events to support keyboard user input processing. Common Keyboard User Input Events include: KeyDown KeyPress KeyUp GotKeyboardFocus PreviewKeyDown LostKeyboardFocus PreviewKeyUp

7 Mouse User Inputs Both Windows Forms and WPF applications support mouse user inputs. Although they both support mouse user inputs, they have different events to support mouse user input processing. Common Mouse User Inputs include: MouseUp MouseDown MouseEnter MouseLeave MouseMove GotMouseCapture LostMouseCapture WheelMove MouseClick MouseDoubleClick

8 Tablet Support New to WPF applications is the default support for the stylus input device. With the advent of the Tablet PC and touch screens, more applications are requiring the user to interact with stylus input devices. By default, Windows Forms do not support stylus input devices, but you can download the Table PC Software Development Kit (SDK) to integrate stylus support into your Windows Forms application.

9 Common Stylus User Inputs include:
Many of the same events for the keyboard and mouse are supported for the stylus. Common Stylus User Inputs include: StylusButtonUp StylusButtonDown StylusUp StylusDown StylusMove StylusInAirMove StylusOutOfRange StylusInRange

10 Control User Inputs Not all user input device events are generated by a specific type of input device. Some user input device events are generated by a variety of device types and handled by the Control and/or Form/Window. Common Control User Inputs include: Click DoubleClick GotFocus LostFocus Validating Validated

11 Form/Windows Level Control User Input
In addition to Control level user input events, you also have Form/Windows level user input events. The Form/Window level user input events are both reactions to user input devices and to validation processing.

12 Exceptions During the validation process, you will encounter situations where the user has entered text into a number field or entered a decimal number into an integer field. When you try to validate these types of situations, Visual Studio will encounter an error and throw an Exception. An Exception is a runtime error that will halt the execution of the application unless handled.

13 Exception Classes The Exception class is a part of the System namespace and handles all runtime error handling. When an Exception occurs, the error is handled either by the System Exception Handler or by a specific application Exception Handler. Exception handling can be categorized into two basic types: Common Language Runtime (CLR) Exceptions User-Defined Exceptions

14 Common Properties of the Exception Class
HelpLink Message Source StackTrace

15 Trapping Exceptions Allowing the default Visual Studio Exception handler to take care of all your Exceptions is not a best practice and will cause the application to exit execution any time an unhandled Exception is raised. It is a best practice to implement your own Exception handling routines.

16 Trapping Exceptions Exceptions are trapped in Visual Studio using the Try-Catch-Finally statement. try { // Try to perform some processing … } catch (Exception ex) // Process exception finally // No matter what, these are the things // I still want to perform

17 Exception Catch Block Once the Exception is raised, the Catch block begins to execute. In the Catch block, you will try to figure out which Exception has occurred and what to do with the Exception. There are several actions you can take when you encounter an Exception in the Catch block: Ignore User Determined Halt Execution

18 Throwing Exceptions You can respond to System Exceptions or you can raise your own custom Exceptions. Much like any other class in the .NET Framework, the Exception class can be derived and extended to perform custom Exception handling. Some derived classes like the OutOfMemoryException can also be inherited and extended, but other derived classes like the StackOverflowException cannot be inherited.

19 Summary You learned about the Windows User Input Model and the various device input action levels. You learned about Keyboard, Mouse, and Stylus events and their device input action levels. You learned how to create Keyboard, Mouse, and Stylus Event Handler methods. You learned about the Exception class and various derived Exception classes. You learned how to handle various Exceptions and how to raise a custom Exception.


Download ppt "Validating User Input Lesson 5."

Similar presentations


Ads by Google