Chapter 3 Customizing a Form and Writing Simple Programs

Document Sample
Chapter 3 Customizing a Form and Writing Simple Programs Powered By Docstoc
					Chapter 3: Customizing a Form and
    Writing Simple Programs

 Chapter 4: First Steps in Building
        the User Interface
        Program Design Process
1. Define the problem to be solved.
2. Work with the user to determine the inputs
   available to solve the problem and the required
   outputs.
3. Determine how the user wants to enter, retrieve,
   and inspect information.
4. (a) Design the user interface using forms and
   controls and (b) set the properties.

                       2
 Program Design Process (cont.)
5. Test this interface by asking the user to
   determine if it is acceptable. Return to
   Step 4 if the interface is not acceptable.
6. Write the code (event procedures and
   modules) for the design.
7. Test the completed design by asking the
   user if it is acceptable. If not, return to
   Step 4 or 6, depending on the type of
   design problem.
                       3
Rapid Application Design (RAD)
       User and Developer            Developer Only


                            Define
                                         Code


                 Modify

                                         Test


Acceptance                  Review


                               4
            Focus on
  Three Key Programming Steps
1. Layout the user interface (forms, controls).
2. Set properties.
3. Write the code (including modules).




                       5
    Example: Clock
   Add a label
   Change its properties:
        Caption = Time will go here
        Name = lblTime
        Alignment = 2 - Center
   Add a command button
   Change its properties
        Caption = Time
        Name = cmdTime
   Write Code
        lblTime.Caption = Now
   Save and run project
                  6
        VB Working Environment:
              Toolbox
   Add a control to a form
    –   Click on the control within the Toolbox
    –   Click the active form
    –   Drag to size the control
   Add a control (Alternate method)(Not for
    framed objects)
    –   Double click the control (appears in the middle
        of the form)
    –   Move and size the control as appropriate
                           7
    VB Working Environment:
        Form Window
 Where you create the windows, dialog
  boxes, and controls in your application.
 Draw and view controls on a form using
  the toolbox.




                   8
 VB Working Environment:
 Project Explorer Window

 Displays a hierarchical list of the
  projects and all of the items contained
  in a project (e.g. forms and modules)
 Use icons to quickly view form and
  view the code window



                   9
     VB Working Environment:
        Properties Window
 Lists the design-time characteristics for selected
  objects.
 Click object to make it active
 Caption or Text
 Name the object -- follow the control naming
  convention
 Set other properties...


                       10
    VB Working Environment:
      Form Layout Window
 Position the form where you want it to
  appear at run time.
 Show the form relative to the display
  dimensions.




                     11
        VB Working Environment:
            Code Window
 Double-click the object to see the Code Window
 Event Procedure labeled:
  ObjectName_EventName (e.g., cmdEnd_Click)
 Insert the event procedure that responds to an event
  between the Private Sub and End statements
 Able to switch among event procedures



                         12
Object Name Prefixes - Examples

        Form             frm
        Command button   cmd
        Label            lbl
        Text box         txt
        List             lst
        Radio button     opt

        Others...

                     13
 Saving the Form and the Project

   Create a Folder for your project

 File, Save Project, or
 Click: Save Project icon




                        14
          Concept of a VB Project

   Collection of files; saved as .VBP file (a
    text file consisting of pointers and settings)
    –   Forms or form modules (.FRM files),
    –   Coded modules (.BAS files)
    –   Binary files - icons, pictures (.FRX files)
   Save forms and the project containing the
    forms

                            15
File, Make .EXE Command

 Creates a standalone application
 Use the Icon property to specify image
  on minimized icon
 Run directly from Windows Explorer or
  as a Shortcut on your desktop
 May need a DLL to run:
   c:\windows\system\Msvbvm60.dll

                16
Printing Out Your Project
 File, Print
 Range = Current Project
 Print What = Form Image, Code
 Click OK




             17
       Example: Welcome to VB
  Add a command button
 Change its properties:
        Caption = Start
        Name =cmdStart
 Add the following code:
Private Sub cmdStart_Click()
   ' The Start command button is used to display a message
   Dim message as String
   Print
   message$="Welcome to VB"
   Print Spc(20); message$ 'Prints 20 spaces followed by the msg
End Sub
 Save and run project         18
    Coded Statements and Methods
 Private - procedure will only work in the
  current form
 Remark (') - nonexecutable statements for
  documentation
 Dim - create variable and allocates memory
 Print - display on the screen
 Assignment (=) - assigns a value to a variable
 Cls - clears objects from the form (add this
  functionality!)       19
       Review of Controls

 Command button - activates a procedure
 Image Control - holds a picture; has a
  stretch property to adjust the picture size
 Textboxes - for data entry
 Labels - displays text; cannot be altered
  by the user


                    20
                         Tab Order
   Order that VB highlights control when the user presses
     – Tab or shift-Tab
     – Arrow keys
   TabIndex Property in all controls
     – Control with TabIndex = 0 -- highlighted as soon as
       form opens
     – As user hits tab key, the control with the TabIndex = 1
       is highlighted
     – Default - TabIndex in order of the sequence the controls
       were created
   TabStop Property - remove a control from the tab order
                               21
                     MessageBox
   Use to display brief messages such as errors,
    warning, or alerts in a dialog box

   returnvalue = MsgBox(prompt, buttons, title)

    buttons = value specifying the layout of the dialog
    box (may use VB’s constants - pg. 127)

    returnvalue = value indicating the command button
    clicked by the user
                           22
       Message Box Example

Private Sub cmdError_Click()
  Dim Answer As String
  Answer = MsgBox("Error found. Continue?",
     vbYesNoCancel, "Error")
  Print Answer
End Sub


                    23