MultiForm Applications - PowerPoint PPT Presentation

1 / 27
About This Presentation
Title:

MultiForm Applications

Description:

End Sub. Do not close the calling. form. IMS 3253: Multi-Form ... Dialogs Message Boxes. Message Boxes return a value indicating which ... Dialogs ... – PowerPoint PPT presentation

Number of Views:87
Avg rating:3.0/5.0
Slides: 28
Provided by: DrLawre6
Category:

less

Transcript and Presenter's Notes

Title: MultiForm Applications


1
Topics
  • Multi-Form Applications
  • Application Form Management
  • When the Application Ends
  • Opening Another Form and Returning
  • Communicating Between Forms
  • Dialogs

2
Multi-Form Applications
  • Most applications require multiple forms
  • More than one task performed under the
    application umbrella
  • Multi-step tasks
  • Too much information to fit on a single form
  • The user should almost never be able to switch
    between different concurrently open windows

3
Multi-Form Applications
  • You must take careful control over forms
  • What the user sees
  • When the application ends
  • Which form opens first
  • Which forms are open at any particular time
  • Which forms are open and visible
  • Which forms are open and invisible
  • You need to avoid leaving an application running
    but with no visible forms

4
Things You Need to Know
  • How to create a new form
  • How to change the startup form for a project
  • How closing forms can end the program
  • How closing forms can leave a program running
    with no visible form
  • How to set values on one form from another one
  • How to create an object variable referencing a
    form and use it to open and communicate with a
    form
  • How to use Dialog Boxes and predefined form types

5
Planning the Projects Navigation
  • Top-Down menu navigation
  • All other forms are reachedfrom the main menu
  • Navigation always returns tothe main menu
  • May have subordinate levels of menu
  • Consider only letting top level menu end the
    application
  • Other forms will Close
  • Return to the top menu

6
Planning the Projects Navigation (cont.)
  • Multi-step task requires navigating through a
    sequenceof forms
  • Return to top level menu from adifferent form
    than the one directlyreached

7
Planning the Projects Navigation (cont.)
  • Multiple Paths
  • Some forms may be reachedfrom different forms

8
Planning the Projects Navigation (cont.)
  • The navigation approaches needed have
    implications for how you open and close forms
  • Some needs may have multiple techniques
  • Some techniques may not be suitable for some
    navigation needs

9
Form File Names
  • When a new project is created it will
    automatically get a form called Form1 with a file
    name of Form1.vb
  • The only way to take control of the forms file
    name is to delete the default form and create a
    new one
  • Set the new forms name when it is created
  • This will also be the file name
  • Change the Project Startup Form
  • Choose Project Properties from the menu

10
Adding a New Form
1
2
  • Add a new form in the Solution Explorer
  • Right-click the project
  • Select Add
  • Select Windows Form
  • Set the forms name andfile name in the
    resulting dialog
  • (If the application is largeenough you should
    use folders to organize it)

3
11
Project Properties
  • Use the Project Properties menu to set
    project form properties
  • Start up form
  • Always set this if youremove Form1 asfirst form
  • Shutdown mode
  • Determines when form closing ends
    theapplication
  • I recommend When Last Form Closes choice

12
Showing a Form
  • Three techniques to make a form visible
  • FormName.Show
  • Any code in the procedure following this
    statement will continue to execute
  • User can click on either form
  • FormName.ShowDialog
  • Execution of code in the current procedure is
    paused until the called form is closed
  • User cannot address any other form
  • FromName.Visible True (do not use)

13
Navigation Techniques
  • You should almost never have more than one form
    active and available to the user at a time
  • If Form A calls Form B you can make Form A
    unavailable in four ways
  • Close Form A (FormA.Close)
  • Make Form A invisible (FormA.Visible False)
  • Disable Form A (FormA.Enabled False)
  • Show Form B modally in dialog
    formFormB.ShowDialog
  • Approaches 1 2 hide Form A while 3 4 leave
    Form A visible but unavailable

14
Navigation Techniques (cont.)
  • If you close the last open form you will end the
    application
  • If you set a calling form (Form A) to be
    invisible or disabled and you close Form B
  • You can leave the application running but with no
    visible form for the user to navigate from
  • User must use Task Manager to end a compiled
    program
  • Or stop debugging in development mode

15
Two Ways to Address a Form in Code
  • (A) Refer to the form by its name
  • May refer to any public property or method of the
    form using FormName.PublicPropertyOrMethod
  • FormName.lblCustomerID.Text
  • FormName.Show
  • FormName.Text
  • Any changes made to a property or value this way
  • Persist as long as the application is running
  • (unless explicitly changed elsewhere)

16
Two Ways to Address a Form in Code (cont.)
  • (B) Create an object variable instance of the
    form
  • The copy of the form created in the variable
    (theFormName in the example above) only lasts as
    long as the variable is in existence
  • Follows normal variable scoping rules
  • Any settings made to the copy expire when the
    object variable expires
  • New copies will revert to the forms definition
    from design view

Dim theFormName as New FormName theFormName.lblCus
tomerID.Text theFormName.Show
17
Passing Values Between Forms
  • Forms expose certain elements to code on other
    forms or in other classes or modules
  • All controls
  • Because controls are declared in the form
    designer scoped with Friend
  • Any variable or custom-written property declared
    with
  • Public
  • Available to any code
  • Friend
  • Available to any code in the same project

18
Passing Values Between Forms (cont.)
Public Class FormB Friend intCustomerID as
Integer
  • If the declaration above exists on Form B code on
    Form A can address intCustomerID directly
  • Any code on Form B can then read this value
  • Common to read the value in the Form Load event
    but it can be read anywhere
  • Code on other forms can also read the value
  • Friend cannot be used in a procedure

Code on Form A FormB.intCustomerID
Convert.ToInt32(txtCustID.Text)
19
Passing Values Between Forms (cont.)
  • Any form or control property on any form can be
    set or read by code on another form
  • FormName.PropertyName
  • FormName.ControlName.PropertyName
  • We can also write Friend scoped custom properties
    for forms
  • Covered in the Object Oriented classes later in
    the semester

20
Returning to The Correct Form
  • In a straight hierarchical navigation structure
    managing return to the correct form is
    straightforward
  • Form A can use FormB.ShowDialog to show Form B
  • Focus will be returned to Form A when Form B is
    released
  • Or Form B can explicitly show Form A because it
    is the only form that will ever open Form B

21
Returning to the Correct Form (cont.)
  • If both Form A and Form B (or any other form)
    both open Form C (Special Technique)
  • On Form C put this code

Public Class FormC Friend theCallingForm As
Form Private Sub btnClose_Click...
theCallingForm.Show() Me.Close() End
Sub End Class
Note object declared as type Form
22
Returning to the Correct Form (cont.)
  • Flexible form navigation (cont.)
  • On the calling form use this code
  • Form C now has a reference to the form that
    called it
  • Note that you cannot CLOSE the calling form or
    the called form will have no form reference

Public Class FormA/FormB Private Sub
btnFormC_Click... Dim theFormC As New FormC
theFormC.theCallingForm Me theFormC.Show()
Me.Visible False End Sub
Do not close the callingform
23
Dialogs
  • VB comes with a rich collection of dialogs with
    which to create
  • Interactive mini-forms
  • Input box
  • Message box

24
DialogsMessage Boxes
  • Message Boxes return a value indicating which
    button was pushed
  • Button combinations
  • AbortRetryIgnore
  • OK
  • OKCancel
  • RetryCancel
  • YesNo
  • YesNoCancel
  • Return Values
  • 1 OK
  • 2 Cancel
  • 3 Abort
  • 4 Retry
  • 5 Ignore
  • 6 Yes
  • 7 No

25
DialogsMessage Boxes (cont.)
  • Message boxes can be called as a statement or
    used in a function format to return a value
  • Experiment with the wide variety of icons
  • Avoid using message boxes excessively

lblMessageBoxResult.Text MessageBox.Show("Sho
w the message", _ "Click the Buttons", _
MessageBoxButtons.RetryCancel, MessageBoxIcon.Ques
tion, _ MessageBoxDefaultButton.Button1)
26
DialogsInput Boxes
  • An input box displays afixed size mini form
    withOK and Cancel buttonsand a single text
    input area
  • Returns the contents of the text area if the OK
    button is pressed
  • Returns an empty string
  • If the Cancel button is pressed
  • If the text input area is empty

27
Miscelaneous
  • Remember to always Open the next form before
    closing the current form
  • If the project is set to end after the last form
    closes you could inadvertently end the program
  • Avoid situations where you are leaving open forms
    behind you
  • Use Close or Menu to indicate this form will
    close but you will return to a higher form
  • Use Exit to indicate that the program will end
  • It is good practice to have only one place to end
    the program
Write a Comment
User Comments (0)
About PowerShow.com