- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
Set the version number of the application to 3.2.
REMOVE THE LOAD BUTTON
|
Action |
Result |
|
|
|
1 |
Remove the Load button from the |
Visual Studio automatically removes all generated code |
|
form. |
related to the button from the InitializeComponent |
|
How-to |
method of the MainForm.cs file. |
|
|
|
|
a. Display the MainForm.cs |
Note: When a control is deleted, the declaration of |
|
[Design] window. |
any event handlers are removed, but the actual event |
|
b. Right-click the Load button. |
handling code, in this case our btnLoad_Click |
|
c. Select the Delete option. |
method, must be removed manually. |
|
We will remove this code later in the chapter. |
|
|
Alternately |
|
|
|
|
|
Simply select the button and hit |
|
|
the Delete key. |
|
|
|
|
2 |
Display the properties for the |
The property values for this control are displayed. |
|
PictureBox control. |
|
|
How-to |
|
|
a. Right-click on the control. |
|
|
b. Select Properties. |
|
|
Alternately |
|
|
Click the control and use the |
|
|
keyboard shortcut Alt-Enter. |
|
|
|
|
3 |
Set the value of the Dock |
Clicking the center button as shown in |
|
property to Fill. |
the graphic sets the value of the Dock |
|
How-to |
property to Fill, so that the |
|
PictureBox control takes up the entire |
|
|
a. Locate the Dock property. |
|
|
display window of the form. |
|
|
b. Display the dropdown window |
|
|
|
|
|
for this property. |
Note: When the Dock property is set to a value |
|
c. Click the center button. |
|
|
other than None, the Anchor property is automati- |
|
|
|
|
|
|
cally set to its default value of Top and Left. |
|
|
|
With the Load button gone, our way is now clear to move this functionality into a menu bar. We continue the above steps and add a menu bar to our form.
|
CREATE THE MAIN MENU BAR |
|
|
|
|
4 |
Display the Toolbox window. |
A list of available controls is displayed. |
|
How-to |
|
|
a. Click the View menu in Visual |
|
|
Studio. |
|
|
b. Select the Toolbox option. |
|
|
Alternately |
|
|
Click the wrench and hammer |
|
|
icon on the left side of Visual |
|
|
Studio. |
|
|
|
|
MENU BARS |
75 |
CREATE THE MAIN MENU BAR (continued)
5Drag a MainMenu object from the Toolbox onto your form.
A MainMenu object called mainMenu1 is added to your form. This object is displayed in a new area called the component tray below the form where objects appear that may not have a physical presence in the window. Such objects include timers, database connections, and main menus.
Note: An example of the component tray showing the mainMenu1 object appears later in this chapter in figure 3.9, on page 99.
Let’s take a look at the source code generated by these actions in the MainForm.cs window. If this window is not shown, right-click the mainMenu1 object and select View Code. You will note that the Windows Forms Designer has added the mainMenu1 variable to the MainForm class.
private System.Windows.Forms.MainMenu mainMenu1;
The InitializeComponent method we discussed in chapter 2 initializes this variable and attaches it to the form. An object for this variable is created using the new keyword. As we mentioned in part 1, the this keyword refers to the current class instance, just as it does in C++.
this.mainMenu1 = new System.Windows.Forms.MainMenu();
At the end of the method, the MainMenu object is attached to the form using the Form.Menu property. This property sets or retrieves a MainMenu object to appear as the main menu bar for the application, and can be used to swap in and out different menu bars to customize how the menu looks for specific situations. We will only use a single MainMenu object in this chapter. See .NET Table 3.2 for additional details on the MainMenu class.
this.Menu = this.mainMenu1;
Also notice in the code how the Anchor property setting for the PictureBox control has been replaced by the Dock property.
this.pbxPhoto.Dock = System.Windows.Forms.DockStyle.Fill;
76 |
CHAPTER 3 MENUS |
.NET Table 3.2 MainMenu class
The MainMenu class is a container class that holds a collection of MenuItem objects to appear as a menu bar on a Windows form. This class is part of the System.Windows.Forms namespace, and inherits from the Menu class. A main menu is assigned to a specific window using the Menu property in the Form class. See the .NET Table 3.1 on page 72 for a list of members inherited from Menu.
|
RightToLeft |
Gets or sets whether text displayed by the menu |
Public Properties |
|
should use a right-to-left alignment. This is useful |
|
when displaying a language such as Hebrew or |
|
|
|
|
|
|
Arabic which reads from right to left. |
|
|
|
|
CloneMenu |
Returns a new MainMenu as a duplicate of the |
|
|
current menu. |
Public Methods |
|
|
|
GetForm |
Returns the Form object that contains this menu, or |
|
|
null if this menu is not contained by a Form. |
|
|
|
3.2.2ADDING THE FILE MENU
With a MainMenu on our form to act as the menu bar, we can now add the menus that should appear. Each menu is created using the MenuItem class. In this section we will create the top-level File menu only. In the next section we will create the dropdown menu that appears when the user clicks on this menu.
|
CREATE THE FILE MENU |
|
|
|
|
|
Action |
Result |
|
|
|
1 |
Edit the menu bar in the |
An empty menu bar appears at the top of the form. The |
|
MainMenu.cs [Design] window. |
space for the first top-level menu contains the words |
|
How-to |
“Type Here.” |
|
|
|
|
Click on the mainMenu1 variable |
|
|
that appears below the window. |
|
|
|
|
2 |
Type in a top-level File menu as |
A File menu appears on the form. |
|
“&File. |
|
|
|
Note: The ampersand (&) specifies the character, in |
|
|
this case F, to use as the access key for this menu. |
|
|
Such access keys are used with the Alt key. In our |
|
|
application the File menu can be displayed by click- |
|
|
ing on it or with the access key Alt-F. |
|
|
|
MENU BARS |
77 |
CREATE THE FILE MENU (continued)
3 |
Modify the (Name) property for |
The (Name) setting represents the variable name used |
|
this menu to be “menuFile.” |
for the object in the MainForm.cs source code. Changing |
|
How-to |
this value automatically changes all generated instances |
|
of the variable for this control to the new name. |
|
|
a. Display the Properties window |
|
|
|
|
|
for the new File menu item. |
|
|
b. Click on the (Name) entry. |
|
|
c. Enter the text “menuFile.” |
|
|
|
Note: The string “&File” we entered for the menu |
|
|
appears in the Text property. |
|
|
|
Your application now contains a File menu on the menu bar. In the source code, the menuFile variable is created as a private MenuItem object within the class.
private System.Windows.Forms.MenuItem menuFile;
The InitializeComponent method now contains additional lines to initialize this menu and add it to our MainMenu object. The relevant lines are extracted here.
private void InitializeComponent()
{
. . .
this.menuFile = new System.Windows.Forms.MenuItem ();
. . .
//
//mainMenu1
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuFile } );
//menuFile
//
this.menuFile.Index = 0; this.menuFile.Text = "&File";
. . .
}
78 |
CHAPTER 3 MENUS |
Note in particular how the File menu is added to our mainMenu1 object by creating an array of MenuItem objects with menuFile as the only entry. This code also sets an Index property, which we will discuss in the next section.
3.2.3ADDING THE DROPDOWN MENU
So far, we have added the main menu and inserted a File menu in it. Next we will create the dropdown menu that appears when this menu is clicked.
CREATE THE FILE DROP-DOWN MENU
|
|
Action |
Result |
||
|
|
|
|
|
|
1 |
Create a Load menu item |
The Load menu appears as the first item in the drop-down list |
|||
|
within the File menu. Use |
for the File menu. |
|||
|
the text “&Load.” |
|
|||
|
How-to |
|
|
|
|
|
a. Make sure the designer |
|
|||
|
window is displayed. |
|
|||
|
b. Click on the File menu. |
|
|||
|
c. Type in “&Load” below |
|
|||
|
the File menu where it |
|
|||
|
says Type Here |
|
|||
|
|
|
|
|
|
2 |
Display the Properties |
The modified properties are displayed in the Properties |
|||
|
window for the Load menu |
window. |
|||
|
item and set the following |
Note: The Shortcut property defines a keyboard short- |
|||
|
property values: |
||||
|
cut, in this case Ctrl+L, that immediately invokes the |
||||
|
|
Settings |
|
||
|
|
menu as if it were clicked, without actually displaying the |
|||
|
|
Property |
Value |
menu. |
|
|
|
The access key Alt+L for this menu can be used to |
|||
|
|
(Name) |
menuLoad |
||
|
|
select this menu from the keyboard after the File menu |
|||
|
|
Shortcut |
CtrlL |
||
|
|
has been displayed. |
|||
|
|
Text |
&Load |
||
|
|
|
|||
|
|
|
|
|
|
3 |
Add a menu separator after |
A menu separator is added to the dropdown menu. |
|||
|
the Load menu. |
|
|||
|
How-to |
|
|
|
|
|
Enter a dash character ‘–’ |
|
|||
|
as the next menu item. |
|
|||
|
|
|
|
|
Note: By definition, a menu separator in .NET is a Menu- |
|
|
|
|
|
Item with its Text property set to a single dash. |
|
|
|
|
|
We will leave the (Name) of the separator as the |
|
|
|
|
|
default value. |
|
|
|
|
|
|
MENU BARS |
79 |
CREATE THE FILE DROP-DOWN MENU (continued)
|
|
Action |
Result |
||
|
|
|
|
|
|
4 |
Finally, add the Exit menu |
The File menu is now complete. |
|||
|
item. |
|
|
|
|
|
|
Settings |
|
||
|
|
|
|
|
|
|
|
Property |
Value |
|
|
|
|
(Name) |
menuExit |
|
|
|
|
Text |
E&xit |
|
|
|
|
|
|
|
Note: Of course, the Windows keyboard shortcut Alt-F4 |
|
|
|
|
|
can always be used to close the application. There is no |
|
|
|
|
|
need to add this keystroke to our menu as it is imposed |
|
|
|
|
|
by the operating system. |
|
|
|
|
|
|
As you might expect, the code generated for the MainForm.cs file uses MenuItem objects to add this dropdown list to the File menu, with the objects initialized in the InitializeComponent method. The relevant code from the source file is shown here.
private System.Windows.Forms.MenuItem menuLoad; private System.Windows.Forms.MenuItem menuItem1; private System.Windows.Forms.MenuItem menuExit;
. . .
private void InitializeComponent()
{
. . .
this.menuLoad = new System.Windows.Forms.MenuItem(); this.menuItem1 = new System.Windows.Forms.MenuItem(); this.menuExit = new System.Windows.Forms.MenuItem();
. . .
//
// menuFile
//
this.menuFile.Index = 0;
this.menuFile.MenuItems.AddRange(new System.Windows.Forms.MenuItem[]{
this.menuLoad, |
|
b Create File |
|
||
this.menuItem1, |
|
drop-down |
this.menuExit}); |
|
menu |
this.menuFile.Text = "&File"; |
|
|
// |
|
|
// menuLoad |
|
|
// |
|
|
this.menuLoad.Index = 0; |
|
|
this.menuLoad.Shortcut = System.Windows.Forms.Shortcut.CtrlL; |
|
|
this.menuLoad.Text = "&Load"; |
Define |
c |
// |
// menuItem1 |
keyboard |
|
shortcut |
||
// |
||
|
80 |
CHAPTER 3 MENUS |
this.menuItem1.Index = 1; |
Create menu |
this.menuItem1.Text = "-"; |
d separator |
// |
|
// menuExit |
|
// |
|
this.menuExit.Index = 2; |
e Set menu index |
this.menuExit.Text = "E&xit"; |
|
. . . |
|
}
Some aspects of this code are worth highlighting:
b As we saw for our main menu, the items to appear under the File menu are added by constructing an array of the desired MenuItem objects and assigning them to the menuFile.MenuItems property. Note that this array does not establish the order in which these items will appear. The display order is established by the menu index assigned to each object.
cThe Ctrl+L shortcut for the Load menu is defined through the use of the System.Windows.Forms.Shortcut enumeration.
d This line creates our separator menuItem1 by setting its Text property to a dash (-).
eThe Index property defines the zero-based position of the menu item within its parent menu. This position establishes the order in which menu items are displayed. In our code, the dropdown list for the File menu should display the Load menu, then a separator, and then the Exit menu. This is done by setting the Index property for these objects to 0, 1, and 2, respectively.
Our code uses a few of the properties provided by the MenuItem class. Other properties will be used as we progress through this and subsequent chapters. An overview of the MenuItem class appears in .NET Table 3.3.
If you wish to see the application so far, compile and run the code to view the File menu. You will notice that the menu bar contains only a single item, which is perhaps a bit boring. We do not want a boring application, so we will double the number of menus in our next section.
MENU BARS |
81 |
.
.NET Table 3.3 MenuItem class
The MenuItem class represents a menu within a MainMenu or ContextMenu object, or a submenu of another MenuItem object. MenuItem objects are displayed to the user, while MainMenu and ContextMenu objects simply establish a container in which MenuItem objects can appear. The MenuItem class is part of the System.Windows.Forms namespace, and inherits from the Menu class. See .NET Table 3.1 on page 72 for a list of members inherited from this base class.
|
Checked |
Gets or sets whether a check mark appears next to the |
|
|
|
text of the menu item. |
|
|
Enabled |
Gets or sets whether the menu item is enabled. A |
|
|
|
disabled menu is displayed in a gray color, cannot be |
|
|
|
selected, and does not display any child menu items. |
|
|
Index |
Gets or sets the position of the menu item within its |
|
|
|
parent menu. |
|
|
MergeOrder |
Gets or sets the value of the relative position for the |
|
|
|
menu when it is merged with another. |
|
|
OwnerDraw |
Gets or sets whether Windows draws the menu |
|
|
|
(false) or the application will draw the item (true). |
|
Public Properties |
|
Used to create custom menus. |
|
|
Parent |
Gets the Menu object that is the parent of this menu. |
|
|
RadioCheck |
If Checked is true, gets or sets whether to display a |
|
|
|
radio button next to the menu instead of a checkmark. |
|
|
Shortcut |
Gets or sets the shortcut key for this menu item. |
|
|
ShowShortcut |
Gets or sets whether to display the Shortcut setting |
|
|
|
when displaying the menu. |
|
|
Text |
Gets or sets the text to display for the menu. The |
|
|
|
character following an ampersand (&) is used as an |
|
|
|
access key. |
|
|
Visible |
Gets or sets whether to display the menu item. |
|
|
|
|
|
|
CloneMenu |
Creates a copy of the MenuItem. |
|
Public Methods |
MergeMenu |
Merges this menu with another MenuItem. |
|
PerformClick |
Generates a Click event for this item. |
||
|
|||
|
PerformSelect |
Generates a Select event for this item. |
|
|
|
|
|
|
Click |
Occurs when the user clicks the menu or accesses it |
|
|
|
via an accelerator or shortcut key. |
|
|
DrawItem |
Occurs when the OwnerDraw property is true and a |
|
|
|
request is made to draw the menu item. |
|
Public Events |
MeasureItem |
Occurs when the size of the menu item is required |
|
|
before drawing it. |
||
|
|
||
|
Popup |
Occurs before the menu item displays its list of child |
|
|
|
menus. |
|
|
Select |
Occurs when the menu is highlighted using the mouse |
|
|
|
or keyboard. |
|
|
|
|
82 |
CHAPTER 3 MENUS |