A typical large project can involve many forms and modules and its main form employs menus to invoke other forms.
The menu strip allows the programmer to easily create a menu bar on a form. You can conveniently enter the text for a menu item in a rectangle that the menu strip displays and set the item’s name property in its property window.
The context menu strip allows the programmer to create a context menu that pops up when the user right-clicks on a control. The context menu can be set up in a way similar to the menu strip. Each context menu can be associated with any number of controls, but a control can have only one context menu. To associate a control with a context menu, set the control’s Context Menu property in its properties window.
The user can access a menu item by clicking on the item or the use of an access key or shortcut key. You can set up an access key by placing an &before a letter in the item’s text. The user can then access the menu item by pressing Alt+<letter>. You can set up a shortcut key for a menu item by selecting a key in its Shortcut property in the properties window. The user can access the menu item by pressing the designated shortcut key.
When a menu item is clicked, or accessed through an access or shortcut key, its Click event is triggered.
In a multiple form application, you can choose a form as the startup object.
To invoke a modeless form, use the form Show method.
A form can also be invoked with the ShowDialog method. When invoked this way, the form stays on top of the application and is recognized as a modal form. Typically, a form invoked this way is used as a custom dialog box.
A form can be made to disappear by its Close or Hide method. A closed modeless form no longer exists in memory; a hidden form stays in the memory, and can be shown again with the Show method.
Controls and variables with Public or Friend scope in a form are accessible to other modules; however, the form must be still in memory—either appearing on the screen or being hidden with the Hide method. If you are not sure whether the form will still exist at time of access, assign the data to Public or Friend variables in a standard module. Other modules can then obtain the data by referencing the variables in the standard module.
You can declare the same Public (or Friend) variable names in different standard modules. In that case, to reference the variables, qualify their names with the module name.
When the startup form closes, all other forms are also closed without triggering their respective FormClosing event. In general, this is the event that you place code to perform finishing touches for each form, such as saving unsaved data. To ensure that the FormClosing event is triggered when the startup form closes, place the code to close all open forms in the application (Application.OpenForms) in the startup form’s FormClosing event.
The number guessing game example is used to illustrate how a multiple form project can be developed.
If an application can have many forms (windows) of the same nature appearing in a container form at one time, the application is a multiple document interface (MDI) application. The MDI application is technically different from the single document interface (SDI) application in that the former has a container form with one or more child forms, while the SDI does not have the parent-child relationship between forms.
To create an MDI application, set the container form’s IsMdiContainer property to True. Also by code, set the child form’s MdiParent property to the container form.
To reference the active MDI child form, use the syntax:
ParentForm.ActiveMdiChild
To cascade or tile the child windows in the MDI container, use the container form’s LayoutMdi method.
If you set a form’s AcceptButton property to a button, the button’s Click event will be triggered when the user presses the Enter key. If you set a form’s CancelButton property to a button, the button’s Click event will be triggered when the user presses the Esc key.
The MdiNotepad project is used to illustrate how to design and code an MDI project.