Presentation is loading. Please wait.

Presentation is loading. Please wait.

Multi-Form Applications Things You Need to Know

Similar presentations


Presentation on theme: "Multi-Form Applications Things You Need to Know"— Presentation transcript:

1 Multi-Form Applications Things You Need to Know
ISM 3005 Course Introduction Topics Multi-Form Applications Things You Need to Know Application Form Management When the Application Ends Opening Another Form and Returning Communicating Between Forms Dialogs “Never let a computer know you’re in a hurry” Unknown 2

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 The difference between referencing an object variable version of a form and the form directly

5 Things You Need to Know (cont.)
How to use Dialog Boxes and predefined form types How to create a custom dialog box form How to call a custom dialog form from multiple forms and have the dialog update the proper calling form How to set up a splash screen

6 Planning the Project’s Navigation
Top-Down menu navigation All other forms are reached from the main menu Navigation always returns to the 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

7 Planning the Project’s Navigation (cont.)
Multi-step task requires navigating through a sequence of forms Return to top level menu from a different form than the one directly reached

8 Planning the Project’s Navigation (cont.)
Multiple Paths Some forms may be reached from different forms

9 Planning the Project’s 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

10 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 form’s file name is to delete the default form and create a new one Set the new form’s name when it is created This will also be the file name Change the Project Startup Form Choose Project | Properties from the menu

11 1 Adding a New Form 2 Add a new form in the Solution Explorer Right-click the project Select Add Select Windows Form… Set the form’s name and file name in the resulting dialog (If the application is large enough you should use folders to organize it) 3

12 Project Properties Use the Project | Properties … menu to set project form properties Start up form Always set this if you remove Form1 as first form Shutdown mode Determines when form closing ends the application I recommend “When Last Form Closes” choice

13 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)

14 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 form FormB.ShowDialog Approaches 1 & 2hide Form A while 3 & 4 leave Form A visible but unavailable

15 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 Some approaches can leave you with multiple copies of the same form open at once!!

16 Two Ways to Address a Form in Code
Refer to the form by it’s 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)

17 Two Ways to Address a Form in Code (cont.)
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 form’s definition from design view Dim theFormName as New FormName theFormName.lblCustomerID.Text = … theFormName.Show

18 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

19 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)

20 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

21 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

22 Returning to the Correct Form (cont.)
If both Form A and Form B (or any other form) both open Form C 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

23 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 Private Sub btnFormC_Click... Dim theFormC As New FormC theFormC.theCallingForm = Me theFormC.Show() Me.Visible = False End Sub Do not close the calling form

24 Dialogs VB comes with a rich collection of dialogs with which to create Interactive mini-forms Input box Message box Special purpose input forms Splash screens About box Explorer form

25 Dialogs—Message 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

26 Dialogs—Message 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("Show the message", _ "Click the Buttons", _ MessageBoxButtons.RetryCancel, MessageBoxIcon.Question, _ MessageBoxDefaultButton.Button1)

27 Dialogs—Input Boxes An input box displays a fixed size mini form with OK and Cancel buttons and 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

28 Dialogs—Custom Dialog Forms
Any form can work like a dialog box with any combination of controls Set the form’s .DialogResult property to one of the available DialogResult values Consider setting the MinimizeButton, MaximizeButton, and ControlBox properties to False and set the FormBorderStyle to Fixed Single Private Sub btnOK_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnOK.Click Me.DialogResult = Windows.Forms.DialogResult.OK Me.Close() End Sub

29 Dialogs—Custom Dialog Forms (cont.)
Read the custom dialog form’s .DialogResult property in the calling form and take appropriate action Other public or friend values on the custom dialog form are also available after it has closed Dim theCustomDialog As New CustomDialog theCustomDialog.ShowDialog() If theCustomDialog.DialogResult = Windows.Forms.DialogResult.OK Then lblCustomDialogResult.Text = theCustomDialog.cboColor.Text Else lblCustomDialogResult.Text = "Cancelled“ End If

30 Dialogs—Built in Dialogs
There are several built in dialogs available in VB Each returns a specialized value ColorDialog returns a Color FontDialog returns a Font FileDialog, OpenFileDialog, & SaveFileDialog return a file path See sample code for two examples We will use the file dialogs in the Sequential Data File classes

31 Splash Screens Many programs have splash screens VB provides a built-in splash screen but I prefer to make my own Requires use of the Timer control Make the splash screen the Start Up form Have the timer tick event open the next form (usually menu) and close the splash screen Hint: Set the timer interval to be very short during development

32 Timer Controls Timer controls appear in the resource area at the bottom of the form design area Tick property indicates how many clock ticks measured in milliseconds will count down before the timer’s Tick event executes Value of 1000 = one second The timer’s Start method must be called before the timer will start counting The Tick event contains code to run when the specified interval has passed

33 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


Download ppt "Multi-Form Applications Things You Need to Know"

Similar presentations


Ads by Google