Showing and Hiding Forms


Part III, "Making Things HappenProgramming," is devoted to programming in Visual C# 2005, 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. Visual C# 2005 can display a single form automatically only when a program starts. To display other forms, you have to write code.

Showing Forms

In Visual C# 2005, everything is an object, and objects are based on classes (see Hour 16, "Designing Objects Using 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, 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 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 objectyour form class. If you have a form class named frmLoginDialog, for example, you could create a new Form object using the following code:

frmLoginDialog frmLogin = new frmLoginDialog();


Thereafter, for as long as the object variable remains in scope (scope is discussed in Hour 11, "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:

frmLogin.Show();


or

frmLogin.Visible = true;


Follow these steps to enable the Picture Viewer project to show the Options form:

1.

Display the main form by double-clicking frmViewer.cs in the Solution Explorer.

2.

Add a new button to the frmViewer form by double-clicking the Button item on the toolbox. Set the button's properties as follows:

Property

Value

Name

btnOptions

Location

301,156

Size

85, 23

Text

Options


3.

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

frmOptions frmOptionsDialog = new frmOptions(); frmOptionsDialog.Show();


The first statement creates a new object variable called frmOptionsDialog and instantiates an instance of the frmOptions 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 Options button. (If the button doesn't appear on the form, you might 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 modeless forms. A non-modal window is one that doesn't cause other windows to be disabled (when you used Show() to display the Options form, you displayed it as a modeless form, which is why you were able to click over to the main Picture Viewer form while the Options form remained displayed). Another example of a modeless window is the Find and Replace window in Word (and in Visual C# 2005, as well). When the Find and Replace window is visible, the user can still access other Windows.

On the other hand, when a form is displayed as a modal form, 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 with only the modal form. After the modal form is closed, the user is free to work with other visible forms within the program. If the form was displayed by another modal form, that form retains the focus until closed, and so on. 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. The Print dialog box of Microsoft Word, for example, is a modal dialog box. When the Print dialog box is displayed, you can't 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.

By the Way

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


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).

You already learned that to show a form as a modeless window, you use the Show() method of the form. To show a form as a modal form, you call the form's ShowDialog() method instead. Display the form in the designer and then double-click the Options button to access its Click event. Next, change the code in your button's Click event to read:

frmOptions frmOptionsDialog = new frmOptions(); frmOptionsDialog.ShowDialog();


When your code looks like this, press F5 to run the project. Click the Options button to display your Options form. Drag the form away from the main form just a bit, and then try to click the main Picture Viewer form or some control on it; you can't. Close the modal form now by clicking the Close button in the title bar. Now, the main Picture Viewer form is enabled again, and you can click the Options button once more (or any other button of your choosing). When you're finished testing this, stop the running project.

Did you Know?

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


Specifying the Initial Display Position of a Form

The location on the display (monitor) where a form first appears isn't random, but rather it is controlled by the form's StartPosition property. The StartPosition property can be set to one of the values in Table 5.1.

Table 5.1. Values for the StartPosition Property

Value

Description

Manual

The value of the Location property at design time determines where the form first appears.

CenterScreen

The form appears centered in the display.

WindowsDefaultLocation

The form appears in the Windows default location, which is toward the upper left of the display.

WindowsDefaultBounds

The form appears in the Windows default location with its bounds (size) set to the Windows default bounds.

CenterParent

The form is centered within the bounds of its parent form (the initial form that displayed the form in question).


By the Way

It's generally best to set the StartPosition property of all your forms to CenterParent unless you have a specific reason to do otherwise. For the first form that appears in your project, you might consider using the WindowsDefaultLocation (but I generally prefer CenterScreen).


To see how this property affects a form, try this:

1.

Press F5 to run the project.

2.

Move the Picture Viewer form and click the Options button. Notice where the Options form appears.

3.

Close the Options form.

4.

Move the Picture Viewer form to the upper-right corner and click the Options button again.

Did you notice that the Options form always appears in the same location, regardless of where the Picture Viewer form is placed when the Options button is clicked? I'm not fond of this behavior. Stop the running project now and change the StartPosition of the Options form to CenterParent now. Next, repeat the previous steps, and you'll see that the Options form always appears centered over the Picture Viewer form, regardless of where that form is positioned.

Displaying a Form in a Normal, Maximized, or Minimized State

Using the Size and Location properties of a form in conjunction with the StartPosition property enables you to display forms at any location and at any size. You can also force a form to appear minimized or maximized. Whether a form is maximized, minimized, or shown normally is known as the form's state and it's determined by the WindowState property of the form.

Click the frmOptions.cs [Design] tab to view the form designer. Look at your form's WindowState property now in the Properties window. New forms have their WindowState property set to Normal by default. When you run the project, as you have several times, the form displays in the same size as it appears in the form designer and at the location specified by the form's Location property. Now change the WindowState property to Minimized. Nothing happens in the Form Design view, but run your project by pressing F5 and then click the Options button. At first, you might think the form didn't get displayed, but it did. It just appeared minimized to the taskbar.

Stop the project and change the WindowState property to Maximized. Again, nothing happens in the Form Design view. Press F5 to run the project and then click the Options button. This time, the Options form fills the screen. Notice too how the image is tiled to fill the form, as explained when you added the image to the form (see Figure 5.11).

Figure 5.11. Images placed on a form are tiled if the BackgroundImag eLayout property of the form is set to Tiled.


By the Way

When a form is maximized, it fills the entire screen regardless of the current screen resolution being used in Windows.


Stop the project and change the WindowState property back to Normal. You'll rarely set a form's WindowState property to Minimize at design time (though you might specify Maximize), but you'll probably encounter situations in which you need to change (or determine) the WindowState at runtime. As with most properties, you can accomplish this using code. For example, the following statement would minimize the Options form (but it would have to appear in the form's class):

frmOptionsDialog.WindowState = FormWindowState.Minimized;


You don't have to remember the names of the values when entering code; you'll get an IntelliSense drop-down list when you type the equal sign.

Stop the project now and save your work.

Preventing a Form from Appearing in the Taskbar

Being able to display an icon for a minimized form is nice, but sometimes it's necessary to prevent a form from even appearing in the taskbar. If your application has a number of tool windows that float over a main window, such as the Solutions Explorer and Toolbox in Visual C# 2005 for example, it's unlikely that you'd want any but your main form to appear in the taskbar. To prevent a form from appearing in the taskbar, set the form's ShowInTaskbar property to False. If the user minimizes a form with its ShowInTaskbar property set to False, she can still get to the window by pressing Alt+Tab even though the program can't be accessed via the taskbar; Visual C# won't allow the application to become completely inaccessible to the user.

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, you set its Visible property to false or use the Hide method of the form. 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 if the form is displayed again, the form looks the same as it did when its Visible property was set to False.

The second method completely closes a form and release the resources it consumes. You should close a form when it's no longer needed so that 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 Visual C# not to simply hide the form but to destroy itcompletely.

Follow these steps to create a button to close the Options form:

1.

Select the frmOptions.cs [Design] tab to display the form designer for the Options form (if it isn't displayed already).

2.

Add a new button to the form and set its properties as follows:

Property

Value

Name

btnOK

Location

305,12

Text

OK


3.

Next, double-click the OK button in the designer to access its Click event and then enter the following statement:

this.Close();


4.

Finally, run the project by pressing F5. Click the Options button to display the Options form and then click OK to close the Options form. Again, the form isn't just hidden; the form is completely unloaded from memory and no longer exists.

By the Way

If you simply wanted to hide a form, but not unload it from memory, you would call the Hide() method of the form or set the form's Visible property to False. This would preserve the state of the form for the time you choose to show it again (by setting it's Visible property to True or by using a method to display the form).





Sams Teach Yourself Microsoft Visual C# 2005 in 24 Hours, Complete Starter Kit
Sams Teach Yourself Visual C# 2005 in 24 Hours, Complete Starter Kit
ISBN: 0672327406
EAN: 2147483647
Year: N/A
Pages: 248
Authors: James Foxall

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