Provides properties for accessing an instance of each Windows form declared in the current project.
My.Forms object provides an instance of each form in the current project. The name of the property is the same as the name of the form that the property accesses. For information about adding forms to a project, see How to: Add Windows Forms to a Project.
You can access the forms provided by the
My.Forms object by using the name of the form, without qualification. Because the property name is the same as the form's type name, this allows you to access a form as if it had a default instance. For example,
My.Forms.Form1.Show is equivalent to
My.Forms object exposes only the forms associated with the current project. It does not provide access to forms declared in referenced DLLs. To access a form that a DLL provides, you must use the qualified name of the form, written as DllName.FormName.
You can use the OpenForms property to get a collection of all the application's open forms.
The object and its properties are available only for Windows applications.
Each property of the
My.Forms object provides access to an instance of a form in the current project. The name of the property is the same as the name of the form that the property accesses, and the property type is the same as the form's type.
If there is a name collision, the property name to access a form is RootNamespaceNamespace\FormName. For example, consider two forms named
Form1.If one of these forms is in the root namespace
WindowsApplication1 and in the namespace
Namespace1, you would access that form through
My.Forms object provides access to the instance of the application's main form that was created on startup. For all other forms, the
My.Forms object creates a new instance of the form when it is accessed and stores it. Subsequent attempts to access that property return that instance of the form.
You can dispose of a form by assigning
Nothing to the property for that form. The property setter calls the Close method of the form, and then assigns
Nothing to the stored value. If you assign any value other than
Nothing to the property, the setter throws an ArgumentException exception.
You can test whether a property of the
My.Forms object stores an instance of the form by using the
IsNot operator. You can use those operators to check if the value of the property is
IsNot operator has to read the value of the property to perform the comparison. However, if the property currently stores
Nothing, the property creates a new instance of the form and then returns that instance. However, the Visual Basic compiler treats the properties of the
My.Forms object differently and allows the
IsNot operator to check the status of the property without altering its value.
This example changes the title of the default
Sub ShowSidebarMenu(ByVal newTitle As String) If My.Forms.SidebarMenu IsNot Nothing Then My.Forms.SidebarMenu.Text = newTitle End If End Sub
For this example to work, your project must have a form named
SidebarMenu. For more information, see How to: Add Windows Forms to a Project.
This code will work only in a Windows Application project.
Availability by Project Type
|Windows Control Library||No|
|Web Control Library||No|