My.Forms


My.Forms provides properties that give references to an instance of each of the types of forms defined by the application. If the program begins with a startup form, then the corresponding My.Forms entry refers to that form. For example, suppose the program begins by displaying Form1. Then, My.Forms.Form1 refers to the startup instance of the Form1 class.

You can also refer to these forms directly. For example, the following two statements set the text and display the predefined instance of the Form2 class:

  My.Forms.Form2.Text = "Hello!" Form2.Show() 

Other forms that you create using the New keyword are separate instances from those provided by My.Forms.

If you know you will only want one instance of a particular form, for example if the form is a dialog box, you can use this instance instead of creating new instances of the class. If you will need to use more than one instance of the form at the same time, you must use New to create them.

You can set these properties to Nothing to dispose of the forms, but you can never set them to anything else. In particular, you cannot set them to new instances of their form classes later. When you destroy one of these instances, it is gone forever. If you will need to reuse the form later, set its Visible property to False rather than setting it equal to Nothing. Alternatively, you can just create new instances of the class when you need them and ignore the forms in My.Forms.




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net