Opening, Closing, and Hiding Forms with Visual Basic .NET

Having multiple forms as part of your Visual Basic .NET program may be nice, but when your Visual Basic .NET program runs, it normally displays one form. To make the other forms of your program appear (or disappear), you have to use BASIC code to tell your program, "Okay, now put this particular form on the screen and hide this other form out of sight."



Opening a form


Before you can open (or close) a form, you need to know the specific name of the form you want to open or close. The Solution Explorer window lists the names of all the forms that make up your Visual Basic .NET program, such as a form named frmMain.vb.



After you know the name of the form that you want to display, you need to use BASIC command to open the form, such as the following:



Dim oForm As FormName
oForm = New FormName()
oForm.Show()
oForm = Nothing



In case the above four lines of BASIC code look confusing, here's a quick explanation on what they do:



1. The first line tells Visual Basic .NET, "Define an object named oForm, which will represent the form that you want to open, which is called FormName." So, if you wanted to open a form named frmMain, you would type: Dim oForm As frmMain


(The oForm name is arbitrary and can be any name you choose. The "o" is just shorthand for saying this is an object.)



2. The second line tells Visual Basic .NET, "Create a new object named oForm, which represents the form represented by the name FormName." (The main difference between the first and second lines is that the first line just told Visual Basic .NET to get ready to create an object to represent your form while the second line actually creates that object to represent your form.)


3. The third line tells Visual Basic .NET, "Show the form represented by the object oForm."


4. The fourth line tells Visual Basic .NET, "Set the object named oForm to nothing to free up the memory that it was taking up."


It's important to set the object to Nothing to free up memory because if you open up too many forms without releasing the memory they use, the computer could run out of memory and cause your program to freeze or crash.



Hiding (and showing) a form


If you want to temporarily make a form disappear, you can use the magic Hide command, such as:



FormName.Hide()



After you've hidden a form, you'll eventually want to make it visible again by using the Show command, such as:



FormName.Show()



Closing a form


Hiding a form just tucks it out of sight, but the form is still loaded in the computer's memory. To clear a form out of memory, you need to use the Close command, such as:



FormName.Close()



To make your program end, you have to shut down all your forms. At least one form of your program needs to have an exit command such as an Exit button or a File --> Exit command available from a pull-down menu. The BASIC code to close the last form of your program looks like this:



Me.Close()



If you look at the BASIC code that Visual Basic .NET automatically creates for each form, you'll see a command that looks like this:



Form1 = Me



This command just tells Visual Basic .NET, "The word Me represents the current form. So instead of having to type the form's complete name, such as frmMainWindow, you can just type Me instead."










dummies

Source:http://www.dummies.com/how-to/content/opening-closing-and-hiding-forms-with-visual-basic.html

No comments:

Post a Comment