15.02.2015 Views

C# 4 and .NET 4

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

1142 ❘ ChaPTer 39 windOws fOrms<br />

Each control must display a context menu. Several menu choices will be available for all of the controls,<br />

but each control will also have a couple of unique menu choices. The common choices can be defined on<br />

one ContextMenuStrip. Each of the unique menu items can be predefined or created at runtime. For<br />

each control that needs a context menu assigned to it, the common menu is cloned <strong>and</strong> the unique choices<br />

are merged with the common menu using the ToolStripManager.Merge method. The resulting menu is<br />

assigned to the ContextMenuStrip property of the control.<br />

ToolstripContainer<br />

The ToolStripContainer control is used for docking of ToolStrip-based controls. When you add a<br />

ToolStripContainer <strong>and</strong> set the Docked property to Fill, a ToolStripPanel is added to each side<br />

of the form, <strong>and</strong> a ToolStripContainerPanel is added to middle of the form. Any ToolStrip<br />

(ToolStrip, MenuStrip, or StatusStrip) can be added to any of the ToolStripPanels. The user can<br />

move a ToolStrip by grabbing it <strong>and</strong> dragging it to either side or bottom of the form. If you set the<br />

Visible property to false on any of the ToolStripPanels, a ToolStrip can no longer be placed in the<br />

panel. The ToolStripContainerPanel in the center of the form can be used to place the other<br />

controls the form may need.<br />

forms<br />

Earlier in this chapter, you learned how to create a simple Windows application. The example contained<br />

one class derived from the System.Windows.Forms.Form class. According to the .<strong>NET</strong> Framework<br />

documentation, “a Form is a representation of any window in your application.” If you come from a Visual<br />

Basic background, the term “form” will seem familiar. If your background is C++ using MFC, you’re<br />

probably used to calling a form a window, dialog box, or maybe a frame. Regardless, the form is the basic<br />

means of interacting with the user. Earlier, the chapter covered some of the more common <strong>and</strong> useful<br />

properties, methods, <strong>and</strong> events of the Control class, <strong>and</strong> because the Form class is a descendant of the<br />

Control class, all of the same properties, methods, <strong>and</strong> events exist in the Form class. The Form class adds<br />

considerable functionality to what the Control class provides, <strong>and</strong> that’s what this section discusses.<br />

form Class<br />

A Windows client application can contain one form or hundreds of forms. The forms can be an SDI-based<br />

(Single Document Interface) or MDI-based (Multiple Document Interface) application. Regardless, the<br />

System.Windows.Forms.Form class is the heart of the Windows client. The Form class is derived from<br />

ContainerControl, which is derived from ScrollableControl, which is derived from Control. Because<br />

of this, you can assume that a form is capable of being a container for other controls, is capable of scrolling<br />

when the contained controls do not fit the client area, <strong>and</strong> has many of the same properties, methods, <strong>and</strong><br />

events that other controls have. This also makes the Form class rather complex. This section looks at much<br />

of that functionality.<br />

form instantiation <strong>and</strong> Destruction<br />

The process of form creation is important to underst<strong>and</strong>. What you want to do depends on where you write<br />

the initialization code. For instantiation, the events occur in the following order:<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

➤<br />

Constructor<br />

Load<br />

Activated<br />

Closing<br />

Closed<br />

Deactivate<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!