Chapter 6 Multiform Projects Brief description on how to navigate within this presentation (PPT) The first time a Key Term from the chapter is used in the PPT, it will display in blue. Gold colored text boxes display coding examples. Slides will be numbered (# of #) when multiple slides on same topic (Slide title). Speaker notes are included where appropriate for slides. (*)Denotes either a comment for page reference to textbook or slide reference in PPT.
Objectives Include multiple forms in an application. Use templates to create splash screens and About boxes. Use the Show, ShowDialog, and Hide methods to display and hide forms. Understand the various form events and select the best procedure for your code. Declare variables with the correct scope and access level for multiform projects.
Using Multiple Forms Projects can appear more professional when using different windows for different types of information. The first form a project displays is a summary form. Projects can contain as many forms as desired.
Creating New Forms Select Add Windows Form from the Project menu and select from many installed templates. The new form will display on the screen and be added to the Solution Explorer Window.
Switching Between Forms (1 of 2) In design time, there are several ways to switch between forms: Solution Explorer window — select a form name and click the View Designer button or the View Code button. Double-clicking a form name opens the form in designer. Easiest way is to use the tabs at the top of the Document window that appear when the form is displayed Each form is a separate file and a separate class.
Switching Between Forms (2 of 2) Selecting from drop-down list Using Tabs
Adding Existing Form Files Forms may be used in more than one project—an existing form maybe used in a new project. Each form and information for the form is saved as three separate files: Code procedures Visual interface Property settings for the controls To add an existing form to a project, use the Add Existing Item command on the Project menu, Select only one filename: FormName.vb; all three files are automatically copied into the project folder, The Solution Explorer window shows the files that are included in a project — you can add new files and remove files from a project. Follow the following steps: i) Select Add Existing Item from the Project menu. ii) In the Add Existing Item dialog box, locate the folder and file desired. iii) Click on Add.
Removing Forms from a Project Select the file name in the Solution Explorer of the file to be removed. Either click the Delete key or right-click on the filename to and choose Delete from the context menu. Additional option is to choose Exclude from project to remove the form from the project but not delete the files.
An About Box (1of 2) One popular type of form in a project is an About box — found in most Windows programs under Help/About Usually provides the name and version of the program and information about the programmer or company Users can create About boxes by creating a new form and entering the information in labels. Windows controls can be used on a new form. About boxes typically hold labels and perhaps an image or shape controls for a logo.
An About Box (2 of 2) A typical About box contains labels, a group box, a picture box, and a button —[suggestion: Have students identify each label, group box, picture box, and button.]
Using the About Box Template Visual Studio’s About Box template can be used to create a new About box. Choose Add Windows Form from the Project menu and select About Box. A new form is created with the About Box template. The form can be customized by setting properties of the controls, adding controls, or removing controls.
Setting Assembly Information Users can manually set the Text properties of the controls. --OR-- Open the Project Designer form. Project/ProjectName Properties or double-click My Project in the Solution Explorer Click the Assembly Information button and fill in the desired information in the Assembly Information dialog box. *The Assembly Information dialog box displays on the next slide.
Assembly Information Dialog Box Enter or modify the project’s information on the Assembly Information dialog box. Once the information is entered into the Assembly Information, it can be retrieved by using the My.Application object.
A Splash Screen The initial screen containing a logo or window that is seen while a program is loading Professional applications use splash screens to tell the user that the program is loading or starting. Makes a large application appear to load and run faster since something is displaying on the screen while the rest of the application loads *The next slide displays a custom splash screen created from a standard Windows form.
Splash Screen Example
Using the Splash Screen Template Visual Studio contains splash screen templates. Select Project/Add New Item to display the Add New Item dialog box and choose Splash Screen to add the new form. Modify the form to fit current needs. A splash form created using the Splash Screen template The predefined code in the splash screen template may be more complicated than is needed — it includes code to fill in the application title, version, and copyright information from the project’s assembly information.
Making the Splash Form Display First Display the Project Designer from the Project menu and set the Splash screen drop-down list to the created splash screen. Do not change the setting for Startup object or Shutdown mode. When the project is run, the splash screen should display while the startup form is loading, and then disappear. At times, the startup form loads so quickly that it is nearly impossible to see the splash screen. Whether you create your own splash screen or use the VB template, you must take one more step to make the splash screen appear before the startup form.
Setting the Splash Screen — Example Set the Splash screen drop-down list to the new form in the Project Designer.
Showing a Form New forms are displayed in response to a user clicking a button or a menu item. In the event procedure for the button or menu item use either the Show method or ShowDialog method to display the new form. In code, you can use several methods to show, hide, and close forms.
Modal versus Modeless Forms Show method displays a form as modeless — means that both forms are open and the user can navigate from one form to the other ShowDialog method displays a new form as modal — the user must respond to the form in some way, usually by clicking a button. No other program code can execute until the user responds to and hides or closes the modal form. With a modeless form, the user may switch to another form in the project without responding to the form. Even with a modal form, the user can switch to another application within Windows.
Show Method General Form Example FormName.Show () The Show method creates a form object from the specified class and displays it modelessly. The FormName is the name of the form to be displayed. FormName.Show () SummaryForm.Show ()
ShowDialog Method FormName.ShowDialog () SummaryForm.ShowDialog () General Form Example Use the ShowDialog method when you want the user to notice, respond to, and close the form before proceeding with the application. FormName.ShowDialog () SummaryForm.ShowDialog () This code is generally placed in a menu item or a button’s click even procedure. The default form object is not actually instantiated until one of the form’s objects is accessed (such as a text box) or a form method.
Hiding or Closing a Form The Close method behaves differently for a modeless form compared to a modal form. Modeless — Close destroys the form instance and removes it from memory. Modal — the form is only hidden. Choosing a form’s Hide method sets the form’s Visible property to False and keeps the form instance in memory. An example would be for a form with instructions or Help text VB automatically creates a form object for each of the form classes that you create. You can show, hide, and close forms without explicitly declaring a new object. This default form object is not actually instantiated until you access one of the form’s objects (such as a text box) or a form method (such as the Show method).
Hide Method General Form Example FormName.Hide() SummaryForm.Hide()
Responding to Form Events Two primary events that code might be needed for are: FormName.Load — form loaded into memory FormName.Activated — occurs when control is passed to form The first time a form is shown in an application, the form generates both the Load and Activated events. If a form is displayed multiple times, initializing steps can be placed into the Activated event procedure; same for setting the focus in a particular place on a new form To set focus in a particular place on the new form, place the Focus method in the FormName_Activated procedure.
The Sequence of Form Events Load Occurs before the form is displayed for the first time — usually happens only once Activated Occurs each time the form is shown Paint Occurs each time any portion of the form is redrawn Deactivate Occurs when the form is no longer the active form FormClosing Occurs as the form is about to close FormClosed Occurs after the form is closed It’s helpful to know the order in which the form events occur.
Writing Event Procedures From the Code Editor In the Editor, drop down the Class Name list and choose the entry that shows the events for the selected form. In the Method Name list, select the event for which to write a procedure—events already having a written procedure appear in bold. Double-clicking a control or form opens an event procedure called FormName.Load — there are two easy techniques in addition to double-clicking. Select the FormName Events entry to see the list of possible events for which to write a procedure. Select the event from the Method Name list to create an empty event procedure.
Writing Event Procedures From the Properties Window in the Designer Select an event using the Properties window. Click the form to show properties and click Events button. Double-click the event to create an empty event procedure.
Holding the Splash Screen Display If applications are very small, the splash screen disappears before it can be read. Code can be written to hold the splash screen longer-threading. A VB program can have multiple threads, which allows for multiple processes to execute simultaneously. The Sleep method pauses the current thread.
Variables/Constants in Multiform Projects For module-level variables to be available in more than one form in a project, it must be declared as Friend or Public and not as Private. Scope can be expanded for variable and constants and is the set of statements that can access a variable or constant without qualifying its name. Example: Friend GrandTotalDecimal As Decimal Scope is the area of the program that can “see” and “use” the variable or constant.
Access Level Specifies the permission required to make use of the variable or constant To make a variable available to other forms, use either the Public or Friend keywords. Friend — allows other forms in the project to access the variable Public — allows all other programs to access variables Private keyword allows access only in the class (form) in which it is declared. Only use access-level keywords for module-level variables. Public keyword is not considered a good programming practice. Local and block-level variables are declared inside a procedure and are always private. By default, variables and constants are Private.
Lifetime The period of time that a variable or constant remains in existence Module and namespace variables exist as long as the application runs.
Static Variables Use to declare local and block-level variables Retain their value for the life of the project If the value in a variable needs to be retained for multiple calls to a procedure such as running count, declare it as Static. If the variable is used in multiple procedures, declare it at the module level. Using a static local variable is better than using a module-level variable because it is always best to keep the scope of a variable as narrow as possible.
The Static Statement General Form Examples Static Identifier As DataType Static PersonCountInteger As Integer Static ReportTotalDecimal As Decimal The format of the Static statement is the same as of the Dim statement. Static statements can appear only inside procedures — they can never appear at the module level. Can never use access-level qualifiers on static variables since all static variables are local.
Namespaces VB projects are automatically assigned to a namespace. Namespaces can be viewed and modified, which is called the root namespace.
Declaring Variables/Constants Guidelines Place all local declarations at the top of a procedure. Use named constants for any value that doesn’t change during the program execution. Keep the scope of variables and constants narrow. Consider making variables local, if possible. Make variables Static, if needed, for multiple executions within a procedure. If variables are needed for more than one procedure, declare it as local and pass it as an argument. Use Private module-level variables if using a variable in multiple procedures and displaying in another. If using the value of a variable in more than one form, declare it as Friend. There are general guidelines for helping to decide where to place declarations. *Refer to the page for the Declaration Summary for Variables and Constants table.
Running a Program Outside the IDE The .exe file can be moved to another computer, placed on the system desktop, or used as a shortcut just like any other application. If copying the .exe file to another system make sure it has the Microsoft.NET Framework. Can download the framework for free from the Microsoft Web site Change the icon, if desired. To change the icon, open the Project Designer. On the Application tab, drop down the list for Icon and browse to find another file with an .ico extension. Recompile (build) the project again after setting the icon.