Showing and Hiding Forms

   

Part III of this book is devoted to programming in C#, and I've avoided going into much programming detail in this hour so that you can focus on the concepts at hand. However, knowing how to create forms does nothing for you if you don't have a way to show and hide them. Because C# can display a single form automatically only when a program starts, you have to write code to show and hide other forms.

Showing Forms

In C#, everything is an object, and objects are based on classes (see Hour 17, "Designing Objects with Classes," for information on creating classes). Because the definition of a form is a class, you have to create a new Form object using the class as a template. In Hour 3, "Understanding Objects and Collections," I discussed objects and object variables , and these principles apply to creating forms.

As discussed in Hour 3, the process of creating an object from a class (template) is called instantiation. The syntax you'll use most often to instantiate a form is the following:

 { formclassname }  {objectvariable}  = new {formclassname()} ; 

The parts of this declaration are as follows :

  • formclassname the name of the class that defines the form.

  • objectvariable This is the name for the form that you will use in code.

  • The keyword new Indicates that you want to instantiate a new object for the variable.

Last, you specify the name of the class used to derive the object ”your form class. If you have a form class named fclsLoginDialog, for example, you could create a new Form object using the following code:

 fclsLoginDialog frmLoginDialog = new fclsLoginDialog(); 

Thereafter, for as long as the object variable remains in scope (scope is discussed in Hour 12, "Using Constants, Data Types, Variables, and Arrays"), you can manipulate the Form object using the variable. For instance, to display the form, you call the Show method of the form or set the Visible property of the form to true using code such as this:

 frmLoginDialog.Show(); 

or

 frmLoginDialog.Visible = true; 

The easiest way to get the hang of this is to actually do it. To begin, choose Add Windows Form from the Project menu to display the Add New Item dialog box. Change the name of the form to fclsMyNewForm.cs (as shown in Figure 5.9), and click Open to create the new form.

Figure 5.9. When you change the default name of a form, remember to leave the .cs extension.

graphics/05fig09.jpg


Your project now has two forms, as you can see by viewing the Solution Explorer window. The new form is displayed in the form designer, but right now you need to work with the main form. At the top of the main design area is a set of tabs. Currently, the tab fclsMyNewForm.cs [Design] is selected. Click the tab titled Form1.cs [Design] to show the designer for the first form.

graphics/bookpencil.gif

Notice how the designer uses the filename rather than the object name on the tabs. You could right-click Form1.cs in the Solutions Explorer window and choose Rename to change the name of the file and therefore the name that appears on the tabs. You should actually do this in all your projects for the default forms. However, I haven't done this throughout the book because it adds yet another step to each example, and I don't want to complicate things too much.

Add a new button to your original form by double-clicking the Button item on the toolbox (be careful not to add the button to the new form by mistake). Set the button's properties as follows:

Property Value
Name btnShowForm
Location 112,112
Text Show Form

Double-click the button to access its Click event (double-clicking a control is a shortcut for accessing its default event) and enter the following code:

 fclsMyNewForm frmTest = new fclsMyNewForm(); frmTest.Show(); 

The first statement creates a new object variable and instantiates an instance of the fclsMyNewForm form. The second statement uses the object variable, now holding a reference to a Form object, to display the form. Press F5 to run the project and click the button. (If the button doesn't appear on the form, you may have accidentally added it to the wrong form). When you click the button, a new instance of the second form is created and displayed. Move this form and click the button again. Each time you click the button, a new form is created. Stop the project now and click Save All on the toolbar.

Understanding Form Modality

You can present two types of forms to the user : modal and nonmodal forms. The modality of a form is determined by how you show the form rather than by how you create the form (both modal and nonmodal forms are created the same way).

A nonmodal window is a window that doesn't cause other windows to be disabled. The forms you created in this example are nonmodal, which is why you were able to continue clicking the button on the first form even though the second form was displayed. Another example of a nonmodal window is the Find and Replace window in Word (and in C#, as well). When the Find and Replace window is visible, the user can still access other windows.

When a form is displayed as a modal form, on the other hand, all other forms in the same application become disabled until the modal form is closed; the other forms won't accept any keyboard or mouse input. The user is forced to deal only with the modal form. When the modal form is closed, the user is free to work with other visible forms within the program. Modal forms are most often used to create dialog boxes in which the user works with a specific set of data and controls before moving on. For instance, the Print dialog box of Microsoft Word is a modal dialog box. When the Print dialog box is displayed, the user cannot work with the document on the main Word window until the Print dialog box is closed. Most secondary windows in any given program are modal windows.

graphics/bookpencil.gif

You can display one modal form from another modal form, but you cannot display a nonmodal form from a modal form.

To show a form as a modal form, you call the form's ShowDialog method rather than its Show method. Change the code in your button's Click event to read:

 fclsMyNewForm frmTest = new fclsMyNewForm(); frmTest.ShowDialog(); 

When your code looks like this, press F5 to run the project. Click the button to create an instance of the second form. Then, move the second form away from the first window and try to click the button again. You can't ”because you've created a modal form. Close the modal form now by clicking the Close button in the title bar. Now, the first form is enabled again and you can click the button once more. When you are done testing this, stop the running project.

graphics/bookpencil.gif

You can test to see whether a form has been shown modally by testing the form's Modal property.

Unloading Forms

After a form has served its purpose, you'll want it to go away. However, "go away" can mean one of two things. First, you can make a form disappear without closing it or freeing its resources (this is called hiding ). To do so, set its Visible property to false. This hides the visual part of the form, but the form still resides in memory and can still be manipulated by code. In addition, all the variables and controls of the form retain their values when a form is hidden, so that if the form is displayed again, the form looks the same as it did when its Visible property was set to false.

Second, you can completely close a form and release the resources it consumes. You should close a form when it's no longer needed, so Windows can reclaim all resources used by the form. To do so, you invoke the Close method of the form like this:

 this.Close(); 

In Hour 3, you learned how this is used to reference the current Form object. Because this represents the current Form object, you can manipulate properties and call methods of the current form using this. ( this.Visible = false , and so forth).

The Close method tells C# to not simply hide the form, but to destroy it completely. If variables in other forms are holding a reference to the form you close, their references will be set to null and will no longer point to a valid Form object (refer to Hour 12 for information on null).

Select the fclsMyNewForm.cs [Design] tab to display the form designer for the second form, add a new button to the form, and set the button's properties as follows:

Property Value
Name btnCloseMe
Location 112,112
Text Close Me

Double-click the button to access its Click event and then enter the following statement:

 this.Close(); 

Next, run the project by pressing F5. Click the Show Form button to display the second form, and then click the second form's button. The form will disappear. Again, the form isn't just hidden; the form instance is unloaded from memory and no longer exists. You can create a new one by single-clicking the Show Form button on the first form. When you're finished, stop the running project and save your work.


   
Top


Sams Teach Yourself C# in 24 Hours
Sams Teach Yourself Visual Basic 2010 in 24 Hours Complete Starter Kit (Sams Teach Yourself -- Hours)
ISBN: 0672331136
EAN: 2147483647
Year: 2002
Pages: 253
Authors: James Foxall

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