1. Trang chủ
  2. » Giáo án - Bài giảng

Chapter 3 Error Handling for the User Interface

36 1,1K 0
Tài liệu đã được kiểm tra trùng lặp

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Tiêu đề Error Handling for the User Interface
Trường học Example University
Chuyên ngành User Interface Design
Thể loại Lecture Notes
Năm xuất bản 2023
Thành phố Sample City
Định dạng
Số trang 36
Dung lượng 280,5 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

Slide 139:06:37 AM HANDLING EXCEPTIONS – The finally block contains code that always executes, whether or not any exception occurs... This cancels the Validating event, leaving the foc

Trang 1

Chapter 3 :

Error Handling for the User

Interface

Trang 2

UNDERSTANDING EXCEPTIONS

• An exception occurs when a program

encounters any unexpected problems.

• Your program should be able to handle these

exceptional situations and, if possible,

gracefully recover from them This is called

exception handling

Trang 3

Slide 3

9:06:37 AM

STEP BY STEP 3_1

Trang 4

STEP BY STEP 3_1

Trang 5

Slide 5

9:06:37 AM

UNDERSTANDING EXCEPTIONS

• The FCL provides two categories of exceptions

thrown by the applications

the CLR

Trang 6

HANDLING EXCEPTIONS

Trang 7

Slide 7

9:06:37 AM

HANDLING EXCEPTIONS

DivideByZeroException ArithmeticException FormatException OverflowException

Trang 8

HANDLING EXCEPTIONS

Trang 9

Slide 9

9:06:37 AM

HANDLING EXCEPTIONS

Trang 10

HANDLING EXCEPTIONS

Trang 11

Slide 11

9:06:37 AM

The throw Statement

Trang 12

The throw Statement

Trang 13

Slide 13

9:06:37 AM

HANDLING EXCEPTIONS

– The finally block contains code that always

executes, whether or not any exception occurs

Ex: page _ _ _ See Demo

Trang 14

VALIDATING USER INPUT

• Field-Level Validation

– 1 Enter (Occurs when a control is entered.)

– 2 GotFocus (Occurs when a control receives focus.) – 3 Leave (Occurs when focus leaves a control.)

– 4 Validating (Occurs when a control is validating.)

– 5 Validated (Occurs when a control is finished

validating.)

– 6 LostFocus (Occurs when a control looses focus.)

Trang 15

Slide 15

9:06:37 AM

The Validating Event

• Inside the Validating event, you can write code to do

the following:

– Programmatically correct any errors or omissions made by

the user.

– Show error messages and alerts to the user so that the user

can fix the problem

– Use the Focus() method of the control to transfer the focus

back to the field.

– Set the Cancel property of CancelEventArgs to true This

cancels the Validating event, leaving the focus in the

control.

Trang 16

The CausesValidation Property

• The default value of the CausesValidation

property for a control is true for all controls

• When you want a control to respond,

regardless of the validation status of other

controls, you should set the CausesValidation property of that control to false

Trang 17

Slide 17

9:06:37 AM

The ErrorProvider Component

icon next to a field when it contains an error

• When the user moves the mouse pointer over

the icon, an error message pops up as a ToolTip

Trang 18

The ErrorProvider Component

Trang 19

Slide 19

9:06:37 AM

Using the ErrorProvider Component and

Other Validation Techniques

Trang 20

The Validating Event and Sticky Form

– The CausesValidation property of the btnExi t control

to false

– Declare the following variable outside a method

block in the class:

private bool closingFlag = false;

– Add the following code to the Click event handler of the Exit button :

Trang 21

Slide 21

9:06:37 AM

The Validating Event and Sticky Form

• Attach the following event handling code to the

Validating events of both the txt Mile controls

Trang 22

Methods

Trang 23

Slide 23

9:06:37 AM

Math Class Methods

• The Math class

– Allows the user to perform common math calculations

– Using methods

• ClassName.MethodName( argument1, arument2, … )

– Constants

•Math.PI = 3.1415926535…

•Math.E = 2.7182818285…

Trang 24

Math Class Methods

Max( -2.3, -12.7 ) is -2.3

Trang 25

Sqrt( x ) Sqrt( 900.0 ) is 30.0

Sqrt( 9.0 ) is 3.0 Tan( x ) Tan( 0.0 ) is 0.0

Trang 26

• Contains the code of what the method does

• Contains the return value if necessary

– For uses call elsewhere in program

• Pass parameters if needed

Trang 27

//Contains the code of what the method does

//Contains the return value

}

Trang 28

User-defined method Maximum (p 196)

public double Maximum( double x, double y, double z )

Trang 29

Slide 29

9:06:37 AM

User-defined method Maximum (p 195)

public void DetermineMaximum()

{

Console.WriteLine( "Enter three floating-point values,\n"

+ " pressing 'Enter' after each one: " );

double number1 = Convert.ToDouble( Console.ReadLine() );

double number2 = Convert.ToDouble( Console.ReadLine() );

double number3 = Convert.ToDouble( Console.ReadLine() );

double result = Maximum( number1, number2, number3 );

Console.WriteLine( "Maximum is: " + result );

}

Trang 30

Argument Promotion

• Implicit Conversion

– Object is converted to a needed type implicitly

– Only done if complier knows no data will be lost

• Explicit Conversion

– Object is manually converted

– Required if there could be a loss of data

– Widening

• Make an object that of a derived class and more complex

Trang 31

byte decimal, double, float, int, uint, long, ulong, object, short or ushort

sbyte decimal, double, float, int, long, object or short

char decimal, double, float, int, uint, long, ulong, object or ushort

decimal object

double object

float double or object

int decimal, double, float, long or object

uint decimal, double, float, long, ulong, or object

long decimal, double, float or object

ulong decimal, double, float or object

object None

short decimal, double, float, int, long or object

ushort decimal, double, float, int, uint, long, ulong or object

Trang 32

Passing Arguments: Call-By-Value vs

Call-By-Reference (p 219)

• Passing by value

– Send a method a copy of the object

– When returned are always returned by value

– Set by value by default

• Passing by reference

– Send a method the actual reference point

• Causes the variable to be changed throughout the program

– When returned are always returned by reference

Trang 33

Slide 33

9:06:37 AM

Reference and value parameters

void SquareRef( ref int x )

Value of z after Square: 5

Value of z after SquareRef: 25

Value of z after SquareOut: 25

Trang 34

Random Number Generation

Trang 35

Slide 35

9:06:37 AM

int value;

value = rand Next();// phát sinh 1 số trong [0;

2,147,483,647 value = rand Next( 6 );// phát sinh 1 số trong [0; 5] value = rand Next( 1, 7 );// phát sinh 1 số trong [1,6]

Trang 36

Finish

Ngày đăng: 13/05/2014, 11:39