6.9. Serving Up Menus

Menus are very simple to insert into a form. Figure 6.14 illustrates the design of a three-level hierarchy of menu items under the title Edit. The steps to create such a menu are as follows:

Figure 6.14. Menu Design


1.
Click on MainMenu in the Windows Forms Toolbox window (on the left in Figure 6.14) and drag it onto the form. The menu is a nonvisible control, so the icon appears below the form. (In Figure 6.14, there are four nonvisible controls.)

2.
Click on the mainMenu1 icon; a Type Here box appears on the second line of the form.

3.
Click on the box. Two additional Type Here boxes appear—one below the current item and the other to its right.

4.
Enter the name of the menu item. Modify any additional characteristics of the item using its Properties box (on the right in Figure 6.14).

5.
Double-click on the menu item to add a click event handler. For example, here is the handler for the case in which a user clicks on the ForeColor menu item for the TextBox menu item; this example illustrates the use of the ColorDialog control:

protected void menuItem10_Click( object sender, EventArgs e )
{
      // instantiate a new ColorDialog object
      ColorDialog diag   = new ColorDialog();
      
   // don't allow user the custom color option
      diag.AllowFullOpen = false;

      // set the current text color as default for dialog
      diag.Color = textBox1.ForeColor ;

      // OK: show the dialog box and let the user select
      diag.ShowDialog();

      // set the new text color to what the user chose
      textBox1.ForeColor = diag.Color;
}

A context menu pops up when the user left-clicks over the control. It is created pretty much the same way as a main menu. Once completed, we need to attach it to the control. This is simple to do: Click on the ContextMenu entry of the control's Properties box. A down arrow appears to the right of the entry. Clicking on that arrow lists all the context menus currently defined. Click on the one you want to attach. That's it.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.227.9