Chapter 13 Additional Topics in Visual Basic Programming

Chapter 13 Additional Topics in Visual Basic Programming In Visual Basic. NET © 2004 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Advanced Validation Techniques • . NET Error. Provider components – Share some characteristics with Web validation controls • Set Max. Length and/or Character. Casing properties of text boxes • Perform field-level validation using Validating event of input controls 2 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Error. Provider Component • Error. Provider component causes an error indication to appear next to the field in error • Generally, one Error. Provider can be used to validate all controls on a form • If data value is invalid, a blinking icon displays next to the field in error and a message displays in a pop-up (similar to a Tool. Tip) 3 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Error. Provider. Set. Error Method • General Form Error. Provider. Object. Set. Error(Control. Name, Message. String) • Examples Error. Provider 1. Set. Error(quantity. Text. Box, "Quantity must be numeric. ") Error. Provider 1. Set. Error(credit. Card. Text. Box, "Required field. ") 4 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Max. Length and Character. Casing Properties • Helps user to enter correct input data • Max. Length property – Set maximum number of characters that can be entered, beeps and holds insertion point to indicate error • Character. Casing property – Converts each character entered to Normal, Upper or Lower case (default is Normal) 5 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Field-Level Validation • Displays any error message as soon as the user attempts to leave a field with invalid data • To accomplish field-level validation use – Validating event – Causes. Validation property – Error. Provider components 6 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Using the Validating Event and Causes. Validation Property • Validation event is best location for validation code – Use Cancel. Events. Args argument to cancel the event and return focus to the control • Each control on a form has a Causes. Validation property set to True by default – When focus passes from one control to another, the validating event occurs for the control just left 7 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Multiple Document Interface • SDI = Single document interface – All we have created thus far – Each form in the project acts independently from the other forms (such as Splash form) • MDI = Multiple document interface – An example of an MDI application is Word – Word has a parent form (the main window) and child forms (each document window) 8 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Multiple-Document Interface (continued) • Child form always stays within boundaries of the parent window • Close the parent window and all child windows close automatically • Child form always appears inside parent’s area • Window menu displays list of open windows, allows movement from one active document to another 9 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

MDI Application Forms 10 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Creating an MDI Project • At design time designate a form as Parent – Is. Mdi. Container property = True • At run time designate Child forms – Before displaying the Child form, from the Parent set the Child's Mdi. Parent property to the current (parent) form 11 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Child Form Code Example Private Sub display. Child. One. Menu. Item_Click(By. Val sender As _ System. Object, By. Val e As System. Event. Args) Handles _ display. Child. One. Menu. Item. Click ' Display Child One form. Dim child. One. Form As New child. One. Form( ) child. One. Form. Mdi. Parent = Me child. One. Form. Show( ) End Sub 12 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Creating an MDI Project (continued) • If multiple child windows are displayed, the title bar of each child should be unique • Solution – Append a number to the title bar before displaying the form (like MS Word) 13 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

MDI Child Title Bar Example ' Module-level declarations. Dim child. One. Count. Integer As Integer Private Sub display. Child. One. Menu. Item_Click(By. Val sender As _ System. Object, By. Val e As System. Event. Args) Handles _ display. Child. One. Menu. Item. Click ' Display Child One form. Dim child. One. Form As New child. One. Form. Mdi. Parent = Me child. One. Count. Integer += 1 child. One. Form. Text = "Child. One Document " _ & child. One. Count. Integer. To. String( ) child. One. Form. Show( ) End Sub 14 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Adding a Window Menu • Parent form should include a Window menu to – List open Child forms (Set menu's Mdi. List property to True) – Allow the user to arrange multiple Child forms – Allow the user to switch between windows 15 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Layout Options • Use an argument of the Layout. Mdi method to set the type of layout • Examples Me. Layout. Mdi(Mdi. Layout. Tile. Horizontal) Me. Layout. Mdi(Mdi. Layout. Tile. Vertical) Me. Layout. Mdi(Mdi. Layout. Cascade) 16 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Toolbars and Status Bars • Enhance usability of programs • A toolbar requires a toolbar control and an image list component – Toolbars are an easy shortcut for menu items – Image list holds the graphics that appear on toolbar buttons • Status bars appear at bottom of screen and display information for the user 17 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Image Lists • Toolbar control requires its images to be stored in an image list • Add an Image. List component to the form • Locate the Images property, click the Builder (ellipses) button to open the Image Collection Editor • Click on the Add button to add images • An index is automatically assigned to each image in the collection • Use the index to assign the pictures to the toolbar buttons 18 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Image Collection Editor Image index 19 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Toolbars • Place the Tool. Bar object on the form • Assign the image list component for the form to the Image. List property of the toolbar • Add buttons to the toolbar using the Tool. Bar. Button Collection Editor • Set properties for new buttons in the right pane of the editor • A Toolbar has a single Button. Click event, use the event arguments to determine which button was clicked 20 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Tool. Bar. Button Collection Editor Contains two Buttons Assigning the Image for the Button Face 21 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Coding for the Toolbar • The toolbar has a single Button. Click event that occurs when the user clicks any of the buttons • Use the event arguments to determine which button was clicked • Each button has an Index • Example Tool. Bar 1. Buttons. Index. Of(e. Button) 22 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Toolbar Code Example Private Sub parent. Toolbar_Button. Click(By. Val sender As _ System. Object, By. Val e As _ System. Windows. Forms. Toolbar. Button. Click. Event. Args) _ Handles parent. Toolbar. Button. Click ' Execute appropriate procedure for button clicked. Select Case parent. Toolbar. Button. Index. Of(e. Button) Case 0 display. Child. One. Menu. Item_Click(sender, e) Case 1 display. Child. Two. Menu. Item_Click(sender, e) End Select End Sub 23 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Status Bars • Usually located at the bottom of a form to display date, time, status of Caps. Lock or Num. Lock, error or informational messages • Place the Status. Bar control on the form • Add Status. Bar. Panel objects to the Status Bar 24 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Status Bars (continued) • Show. Panels property – False by default, only displays Text property of Status Bar – Set to True to show Panels • Panels Collection – Click Add button in the Status. Bar. Panel Collection Editor to add a panel – Set Panel properties in the right pane of Editor window 25 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Status. Bar. Panel Collection Editor Contains two Panels 26 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Assigning Values to Panels • Assign values to the Text property at run time • Examples date. Status. Bar. Panel. Text = Now. To. Short. Date. String( ) time. Status. Bar. Panel. Text = Now. To. Long. Time. String( ) information. Status. Bar. Panel. Text = "It’s very late. " 27 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Displaying the Date and Time • Use the properties and methods of the Date. Time structure to retrieve and format the current date and time • Now property holds system date and time in numeric format • Generally set initial values in Form_load event and use a Timer component to update the time 28 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Displaying the Date and Time (continued) • Date. Time methods – To. Short. Date. String – To. Long. Date. String – To. Short. Time. String – To. Long. Time. String Actual display depends on the user's local system settings. 29 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Some Helpful Date Controls • Provide the ability to display calendars on your form • Date. Time. Picker – Takes less screen space – Displays only day and date unless user drops down the calendar • Month. Calendar – Displays calendar 30 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Calendar Controls Date. Time. Picker 31 Month. Calendar © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Date. Time. Picker Control • Value Property – Contains the date – Initially set to current date – User can select a date or you can assign a date value to the property • Value. Changed Event – Allows use of all the properties of the system time on the Value property of the control 32 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Crystal Reports • Gallery contains "experts" for guidance in creating standard reports, forms and mailing labels • May use an existing report or generate a new one • Publish reports on Windows Forms or on Web Forms Note: Visual Basic. NET Standard Edition does not include Crystal Reports. 33 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Create and Display a Report • Add a Report Designer • Design the Report Template which includes – Settings for connection to the database – Layout of the report • Add a Crystal. Report. Viewer control to a form • Connect the control to the template 34 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Adding a Report Designer • • Select Project menu, Add New Item Choose Crystal Report icon Name the report Choose type of report – Using the Report Expert (opens a wizard) – As a Blank Report – From an Existing Report 35 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Add New Crystal Report Item 36 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Crystal Report Gallery Often the easiest way to create a report 37 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Standard Report Expert Wizard 38 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Standard Report Expert Wizard – Select Table 39 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Standard Report Expert Wizard - Select Fields 40 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Display the Report • Add a Crystal. Report. Viewer control to the form • Report. Source Property – Set to Report file name using Browse option • Anchor Property – Set for all four edges to allow control to resize, if the form is resized 41 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Report Designer • Two new toolbars • Field Explorer window – Use to add new fields to report • Separate section in toolbox – Use items to add elements such as lines, boxes or text not bound to a data field • Template contains several bands for information (see Figure on next slide) 42 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Crystal Reports Layout in Report Designer 43 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Report Designer (continued) • Report Header – Information which appears on only the first page of a multipage report • Page Header – Information which appears at top of each page • Generally, report title and column headings • Details Section – Data for body of report • Fields which appear on each line of report (each record) 44 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

The Report Designer (continued) • Report Footer – Information which appears once at the end of the report • Page Footer – Information which appears at the bottom of each page 45 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Modifying Report Design • Move, resize and reformat fields in the designer – Resize by using sizing handles – Drag the field control to move it – To reformat a field, right-click and select Format from the shortcut menu • Select Report Expert from context menu to change the report design • Select Style Expert to choose a new style 46 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Adding a Report to a Web Form • Use the Crystal. Reports. Viewer control from the Web Forms section of the toolbox • Web version does not have printer options • Set the Report. Source property in the Web Form’s Page_Load event 47 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.

Moving a Crystal Reports Project • Change the data source for the report template – Browse to current location of database file • Change Report. Source property of the Cyrstal. Report. Viewer control – Browse to locate project’s current folder 48 © 2005 by The Mc. Graw-Hill Companies, Inc. All rights reserved.
- Slides: 48