Lecture Set 8
description
Transcript of Lecture Set 8
Lecture Set 8
Validating Data and Handling Exceptions
Part C – Exception Handling with Other VB Tools
Slide 2
Objectives
To understand how to us some additional VB tools in handling exceptions Masked textboxes Check boxes Scrolling tools later to be used with List
Boxes and Combo Boxes
8/13/2013 11:40 AM
Slide 3
Using Masked textboxes
Similar to what you can do with a typical database tool
Forces user entries to comply with a specific format User can’t leave the control until a valid
entry has been made (valid in terms of the mask)
Masks are best used with things like dates, phone numbers, zip codes and the like
8/13/2013 11:40 AM
Slide 4
Two Useful Properties of Mask Boxes
The Mask Property – You set the mask you want to force the
format for a given text box This then becomes a type validation
mechanism that you do not have to code The ValidatingType Property –
The code generated for this control allows you to set this property to a data type such as Date or Decimal to even further restrict what the user is allowed to enter
But YOU have to set the property by writing code to do so
8/13/2013 11:40 AM
Slide 5
You may STILL need to do more work …
You can do this by coding the TypeValidationComplete Handler for the control -- -- --
8/13/2013 11:40 AM
Slide 6
The Invoice Total form with two masked text boxes
8/13/2013 11:40 AM
Slide 7
Typical masks used in the Mask property of a masked text box
Mask property Mask when run Example Entry requirements
L _ C One letter
####.## ____.__ 0450.50 Six digits with two decimal places
00/00/0000 __/__/____ 06/08/2007 Six digits in date format
(999) 000-0000 (___) ___-____ 555-432-3291 Ten digits in phone number format
8/13/2013 11:40 AM
Slide 8
Code that sets the ValidatingType property for a masked text box (optional)
mskSubtotal.ValidatingType = GetType(System.Decimal);
Note If you set the ValidatingType property for the control, the entry is
tested to see whether it’s valid for that type. Then, you can use the TypeValidationCompleted event for further testing.
8/13/2013 11:40 AM
Slide 9
An event handler for the control’s TypeValidationCompleted event (VB) (optional) Private Sub mskSubtotal_TypeValidationCompleted( _ ByVal sender As System.Object, _ ByVal e As _ System.Windows.Forms.TypeValidationEventArgs) _ Handles mskSubtotal.TypeValidationCompleted If e.IsValidInput Then If CDec(mskSubtotal.Text) <= 0 Then MessageBox.Show( _ "Please enter a subtotal " _ & "that's greater than zero.", _ "Entry Error") e.Cancel = True End If Else e.Cancel = True End If End Sub
8/13/2013 11:40 AM
Slide 10
Murach’s Visual Basic 2005, C7
Two problems with using event handlers for Validating events (optional)
Validating event handlers are only triggered if the user moves the focus to the control.
There isn’t any way for the user to cancel an entry once the entry is started.
8/13/2013 11:40 AM
Slide 11
Are Textboxes a Pain in the Rear?
Validating input is painful in general, and it is even more so when you give the user free reign with regard to data entry (as we do when we use textboxes)
So what do we do? Start thinking about other controls
which the user can use to avoid typing entries
8/13/2013 11:40 AM
Slide 12
Controls that Rely on Decision-Making
Three controls are commonly used with decision-making The CheckBox control allows the end user
to select one of two possible values The HScrollBar control and VScrollBar
controls allow the user to select a value from a range of values
8/13/2013 11:40 AM
Slide 13
The CheckBox Control
The CheckBox control allows the end user to select from two possible values
The CheckAlign property defines where the check box appears
The Boolean Checked property indicates whether the box is checked
The Text property contains the visible text The TextAlign property controls the
alignment of the text The CheckedChanged event fires when the
value of the Checked property changes
8/13/2013 11:40 AM
Slide 14
The CheckBox Control (Example)
Determine whether the CheckBox named chkDemo is checked
if (chkDemo.Checked == true) txtState.Text = "Checked“;else txtState.Text = "Not checked“;
8/13/2013 11:40 AM
Slide 15
The HScrollBar and VScrollBar Controls (Introduction)
Use to select a value from a range of values
The two scroll bars work the same way The HScrollBar has a horizontal
orientation The VScrollBar has a vertical orientation
8/13/2013 11:40 AM
Slide 16
The HScrollBar and VScrollBar Controls (Syntax)
The Minimum and Maximum properties define the range of values
The current value is stored in the Value property
The SmallChange and LargeChange properties define the magnitude of change
The Scroll event fires while scrolling The ValueChanged event fires when
scrolling is complete and the value changes
8/13/2013 11:40 AM
Slide 17
Changing the Value Property of a Vertical Scroll Bar
8/13/2013 11:40 AM
Slide 18
The Maximum and Minimum Properties of a Vertical Scroll Bar
8/13/2013 11:40 AM
Slide 19
Scroll Event (Example)
Display scroll bar values
private void vsbDemo_Scroll( _ object sender, _
System.Windows.Forms.ScrollEventArgs e) { txtValue.Text = vsbDemo.Value.ToString; txtOldValue.Text = e.OldValue.ToString; txtNewValue.Text = e.NewValue.ToString;} // end vsbDemo
8/13/2013 11:40 AM