Presentation is loading. Please wait.

Presentation is loading. Please wait.

Interfacing with the User Advanced Programming Using Visual Basic 6.0.

Similar presentations

Presentation on theme: "Interfacing with the User Advanced Programming Using Visual Basic 6.0."— Presentation transcript:

1 Interfacing with the User Advanced Programming Using Visual Basic 6.0

2 Designing the interface Goal is to create programs that are easy to use  if users misuse a program, it’s the developer’s fault!! Make your projects look like Windows apps  Use controls like StatusBar and Toolbar  TreeView and ListView makes a project look like Windows explorer Tips (p26)

3 Object Collections Group of objects  Remember: option buttons, combos Reference using Index numbers  Usually 0 based Count property = # of objects in collection Default, built-in collections  Forms collection - all loaded forms  Controls Collection - all controls on a form

4 Form Events Initialize – created in memory before loading, occurs when an instance of a form is created Load – form is loaded into memory Activate – form becomes the active window or becomes visible with the Show method or Visible property When a form loads, the first event to occur is the Initialize, followed by the Load. When the form load is complete and the form displays or becomes the active window, the Activate event occurs. If all of the form's controls are disabled the GotFocus (e.g. Splash Screens) event is triggered instead of the Activate event.

5 Form Events Deactivate – another form becomes the active window QueryUnload – occurs before form is unloaded; used to check for saving changes Unload – form is unloaded from memory Terminate – all variables are set to nothing (e.g. deallocating the memory set aside for a collection)

6 Why??? Screen Resolution (pixels) Design for lowest, 640 by 480, unless sure user's screens are always higher Next most common resolution, 800 by 600 Tip: Use the form layout window (right-click on form display) to make sure your form will display in 640 X 480 resolution

7 MDI versus SDI SDI=Single-Document Interface  All we have created thus far  Each form is in a separate window MDI=Multiple-Document Interface  Like Word, Excel, etc.  Forms display within the window of other forms

8 MDI (Multiple-Document Interface) One Parent window which "contains" other windows Many Child windows "contained within" Parent window Parent and Child may share menus, toolbars With MDI, when you unload the main form, all child forms are also unloaded. An advantage of using MDI is that the child forms display within the boundaries of the parent, giving more control to the user resizing forms. Window menu  Tile, Cascade, and Window List

9 Creating MDI Projects Use wizard OR Add MDI form to project  Project menu, Add MDI form Designate all other forms(except Splash form) as Children (you can have only one MDI)  Form property MDIChild=True Add a Window menu

10 Window Menu If an MDI form has a menu, the menu displays only if no child form with it’s menu is displaying – if child form menu is displaying, it’s menu replaces the parent’s menu WindowList property=True Tiling and Cascading Windows  Include menu commands  Use Arrange method of the Parent form frmMain.Arrange vbCascade frmMain.Arrange vbTileHorizontal frmMain.Arrange vbTileVertical

11 Adding controls to your MDI form You need to add some components to your project first. Go to Project>Components Choose the "Controls" tab Check the box next to: Microsoft Windows Common Controls 6.0 Microsoft Common Dialog Controls 6.0 Microsoft Rich Textbox Control 6.0 This will bring up some new controls on your toolbox. Click and drag the "Toolbar" control on to the MDI form. You can now place cmd buttons etc. on the toolbar (tbMenu - tb is the prefix)

12 Private Sub Form_Unload(Cancel as Integer) Dim SingleForm as Form For Each SingleForm in Forms Unload SingleForm Next End Sub Ending an Application Do not use END any longer Unload all loaded forms instead  SDI - Unload Me for command button or menu  MDI - Use For Each Next to unload forms

13 Adding Controls at Run-Time Previously, we made controls visible and invisible during run-time How do we dynamically add controls at run-time?  Use Control Arrays  Use Controls Collection


15 Popup Menus Shortcut menus Respond to right mouse button down/up Can show an existing menu OR Create menu  Top level of this menu is Visible=False  Lower levels of this menu are Visible=True MouseUp event includes code to Popup menu at location of mouse click

16 Popup Menu Code Private Sub Form_MouseUp (Button as Integer, Shift as Integer, _ X as Single, Y as Single) If Button=vbRightButton Then PopupMenu mnuColor End If End Sub VB Statement VB Intrinsic Constant

17 Windows Common Controls 6.0 Image List (ils) Toolbar (tlb) Status Bar (sta) TabStrip (we will use the Tabbed Dialog instead) Progress Bar Slider ListView (lvw) TreeView (tre) ImageCombo

18 Windows Common Controls 6.0 Do not confuse with Common Dialog Control Add to toolbox  Project menu, Components, Microsoft Windows Common Controls 6.0 Most of these controls have custom properties  Access from Custom property in Property Window or Right Click on Control  Displayed as Property Pages All are actually collections, most are 1 based

19 Image List (ils) Container for pictures referenced by other controls  Toolbar, ListView, TreeView Add pictures - ico, bmp, gif  Set Key property to use as a reference when connecting to other controls  Key Property is case sensitive!!!

20 Toolbar (tlb) Connect to an existing Image List  Once connected you cannot change the Image List in any way without disconnecting the Image List  Disconnecting the Image List will require that you reset all references to the list in the Toolbar's property pages once you have finished modifying the Image List

21 Toolbar (tlb) Insert Buttons Assign Key property, case sensitive Set Button's Image property to the Key of the Image List picture you want for this button Styles  tbrSeparator - creates a separator area between buttons  tbrButtonGroup - buttons act like an option group

22 Coding a Toolbar (tlb) Use Select Case referencing Button's Key Private Sub tlbMain_ButtonClick(ByVal Button as _ MSComctLib.Button) Select Case Button.Key Case "Save" mnuFileSave_Click Case "Print" mnuFilePrint_Click End Select End Sub

23 Status Bar (sta) Insert Panels Assign Key property, case sensitive Styles  sbrCaps - Caps Lock  sbrNum - Num Lock  sbrTime - current time  sbrText - string entered at design time, can be modified at run time

24 Tabbed Dialog (sstab) Add to toolbox  Project menu, Components, Microsoft Tabbed Dialog Control 6.0 Tabs can appear on any edge Designate number of tabs and number of rows TabCaption Tab numbering begins with 0 Add other controls to the tabbed dialog (similar to adding controls to a frame)

25 TreeView (tre) Like left side of Windows Explorer Must be connected to an Image List Displays hierarchical information Parent Nodes which can be expanded and collapsed to display Child Nodes  Expand ==> +  Collapse ==> -

26 ListView (lvw) Like right side of Windows Explorer Must be connected to an Image List Display various views  Large Icons  Small Icons  Details  List

27 Error Handling Trap run-time errors rather than having user deal with them or program terminating Some problems cannot be avoided - BUT they must be anticipated  Drive or printer not functioning  Improperly formatted disk  File not found

28 Most Common Run-Time Errors 11Division by 0 13Type mismatch *** 482Printer error 53File not found 61Disk full 68Device unavailable 71Disk not ready 75Path/file access error 76Path not found

29 When a run-time error occurs… VB generates an error number VB checks the number against a table of known error codes  p63-64, Advanced Programming Using Visual Basic 6  p387, Programming In Visual Basic 6 Programmer can intercept the error code and take action before VB terminates the project

30 Error Trapping Steps Turn on the error-handling feature using On Error statement in subprocedure Create error handling code routines  Set them off from other code with line labels Write code to continue after the error is "handled"

31 On Error Statement Use this statement at the beginning of a procedure to activate error trapping Designate a line label in the same procedure to go to if an error occurs  Line labels - begin in column 1, end with colon: On Error GoTo ErrorHandler Refers to line label

32 Error Handling Code Precede with Exit Sub statement (or Exit Function) Check the error number(s)  Single error number -- If structure  Multiple error numbers -- Select Case Inform user if necessary Designate next line of code to execute  Resume  Resume Next  Resume line label

33 Resume What line of code should be executed after the error has been handled? Resume - line of code that caused error Resume Next - line of code that would logically be executed after the line of code that caused error Resume line label - line with indicated label

34 Err Object Intrinsic VB object (like Printer object you used in Programming in Visual Basic 6 book) Properties  Number - error number, 0 to 65,535  Source - object or application that caused error  Description Method  Raise - set an error number and/or cause it to occur

35 Error Handling Standards Use Resume if you identify the problem and the user could correct it Use Resume Next if you identify the problem and execution can proceed without running the error generating line of code Raise the error again (Err.Raise Err) if you cannot identify the problem so VB will handle it and generate a system error message

36 Error Handling Standards (cont.) Use Resume line label if you want to exit the procedure Call you exit procedure (perhaps, mnuFileExit) to end without displaying any error message Turn off error handling/trapping with: On Error GoTo 0

37 Private Sub Whatever( ) On Error GoTo ErrorHandler code to do whatever this subprocedure does Exit Sub ErrorHandler: If Err.Number=71 msgbox to inform user of error for correction Resume Else Err.Raise Err End If End Sub Handling One Error Number

38 Private Sub Whatever( ) On Error GoTo ErrorHandler code to do whatever this subprocedure does Exit Sub ErrorHandler: Select Case Err.Number Case 71 msgbox Case 53, 76 msgbox Case Else Err.Raise Err End Select Resume End Sub Handling Multiple Error Numbers

39 More Error Code Examples Advanced Programming Using Visual Basic 6  p 62  p 65  p 82-88 Programming in Visual Basic 6  p 386  p 389  p 391  p 392

Download ppt "Interfacing with the User Advanced Programming Using Visual Basic 6.0."

Similar presentations

Ads by Google