Home

Visual Characteristics of Menu Items

 

Visual Assistance With Menu Items

 

Introduction

In the previous lesson, we saw how to create a menu. Here is an example:

using System;
using System.Drawing;
using System.Windows.Forms;

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;

    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuFileNew;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuFile = new ToolStripMenuItem("File");
        mnuFileNew = new ToolStripMenuItem("New");

        mnuFile.DropDownItems.Add(mnuFileNew);
        mnuMain.Items.Add(mnuFile);
    }
}
public class Program
{
    static int Main()
    {
        System.Windows.Forms.Application.Run(new Exercise());
        return 0;
    }
}

This would produce:

Main Menu

After creating a menu (main menu and contextual menu), there are various actions you can perform to improve it and there are many ways you can enhance the user's experience with your application. Menus provide various features such as access keys and shortcuts. There are also other things you can do such as grouping menus. Although some of these actions are not required to make an application useful, they can be necessary to make it more professional.

Practical LearningPractical Learning: Introducing Menu Appearance

  1. Start a new Windows Forms Application named SolasPropertyRental2
  2. Change the properties of the form as follows:
    Text: Solas Property Rental
    StartPosition: CenterScreen
  3. On the main menu, click Project -> Add Class...
  4. Set the Name to RentalProperty and click Add
  5. Change the file as follows:
     
    using System;
    
    namespace SolasPropertyRental2
    {
        public class RentalProperty
        {
            public string PropertyCode;
            public string PropertyType;
            public int Bedrooms;
            public float Bathrooms;
            public decimal MonthlyRent;
            public string Status;
        }
    }
  6. In the Solution Explorer, right-click Form1.cs and click Rename
  7. Type Central.cs and press Enter twice (to display the form)
  8. Double-click an unoccupied area of the form to generate its Load event
  9. Make the following changes:
     
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Collections;
    
    namespace SolasPropertyRental2
    {
        public partial class Central : Form
        {
            ArrayList lstRentalProperties;
    
            public Central()
            {
                InitializeComponent();
            }
    
            private void Central_Load(object sender, EventArgs e)
            {
                lstRentalProperties = new ArrayList();
            }
        }
    }
  10. Re-display the form
  11. In the Menus & Toolbars section of the Toolbox, click the MenuStrip button and click the form
  12. While the menu strip is still selected, in the Properties window, click (Name) and type mnuMain
  13. In the Common Controls section of the Toolbox, click ListView and click the form
  14. While the picture box is still selected, in the Properties window, change its characteristics as follows:
    Dock: Fill
    FullRowSelect: True
    GridLines: True
    (Name): lvwRentalProperties
    View: Details
    HeaderStyle: Nonclickable
  15. Still in the Properties window, click Columns and click its ellipsis button
  16. Create the columns as follows:
     
    (Name) Text TextAlign Width
    colPropertyCode Prop Code    
    colPropertyType Property Type Center 80
    colBedrooms Bedrooms Right  
    colBathrooms Bathrooms Right 62
    colMonthlyRent Monthly Rent Right 75
    colStatus Status    
  17. Click OK
  18. In the Menus & Toolbars section of the Toolbox, click the ContextMenuStrip button and click the form
  19. While the menu strip is still selected, in the Properties window, click (Name) and type cmsProperties
  20. On the form, click the list view
  21. In the Properties window, click ContextMenuStrip and select cmsProperties

Access Keys

You may notice that some menu items have a letter underlined. Using this letter allows the user to access the menu using a keyboard. For example, if the letter F is underline in a File menu as in File, the user can access the File menu by pressing the Alt, then the F keys. To create this functionality, choose a letter on the menu item and precede it with the & character. For example, &File would produce File. You can apply the same principle if you are programmatically creating the menu. Here are two examples:

void InitializeComponent()
{
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuFile = new ToolStripMenuItem("&File");
        mnuFileNew = new ToolStripMenuItem("&New");

        mnuFile.DropDownItems.Add(mnuFileNew);
        mnuMain.Items.Add(mnuFile);
}

After creating the menu, to use it, the user can press Alt or F10:

Access Keys

Practical LearningPractical Learning: Using Access Keys

  1. Under the form, click mnuMain
  2. In the Properties window, click Items and click its ellipsis button
  3. In the Items Collection Editor, make sure MenuItem is selected in the Select Item And Add To List Below combo box and click Add
  4. While toolStripMenuItem1 is selected in the Members combo box, in the right list, change the following characteristics:
    Text: &File
    (Name): mnuFile
  5. Still in the right list, click DropDownItems and click its ellipsis button
  6. In the Items Collection Editor, make sure MenuItem is selected in the Select Item And Add To List Below combo box and click Add
  7. While toolStripMenuItem1 is selected in the Members combo box, in the right list, change the following characteristics:
    Text: &New Property
    (Name): mnuFileNewProperty
  8. In the Items Collection Editor (mnuFile.DropDownItems), click OK
  9. In the Items Collection Editor, click OK

Shortcuts

A shortcut is a key or a combination of keys that the user can press to perform an action that can also be performed using a menu item. When creating a menu, to specify a shortcut, use the ShortcutKeys property.

To visually specify a shortcut, in the menu designer, click the menu item. In the Properties window, click ShortcutKeys and click the arrow of the field, a window would come up:

To specify just a letter for the shortcut, you can click the arrow of the combo box on the left side of the Reset button. A list would come up from which you can select the desired letter:

You are probably more familiar with shortcuts made of combinations of keys, such as Ctrl + N, Alt + F6, or Ctrl + Alt + Delete. To visually create such a shortcut, click the check box(es) and select the desired letter.

If you have used applications like Microsoft Word or Adobe Photoshop, you may know that they don't show all of their shortcuts on the menu. If you want to hide a shortcut, after specifying it, in the Properties window, set the ShowShortcutKeys property to False.

To programmatically specify a shortcut, assign a key or a combination of keys to the ShortcutKeys property of the ToolStripMenuItem class. The ShortcutKeys property is of type Keys, which is an enumeration of the various keys of a keyboard recognized by Microsoft Windows. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuFileNew;
    ToolStripMenuItem mnuFileExit;
    ToolStripMenuItem mnuFormat;
    ToolStripMenuItem mnuFormatFont;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuFile = new ToolStripMenuItem("&File");
        mnuFileNew = new ToolStripMenuItem("&New");

        mnuFileExit = new ToolStripMenuItem("E&xit");

        mnuFormat = new ToolStripMenuItem("For&mat");
        mnuFormatFont = new ToolStripMenuItem("Fo&nt");

        mnuFormatFont.ShortcutKeys = Keys.F4;

        mnuFile.DropDownItems.Add(mnuFileNew);
        mnuFile.DropDownItems.Add(mnuFileExit);
        mnuMain.Items.Add(mnuFile);

        mnuFormat.DropDownItems.Add(mnuFormatFont);
        mnuMain.Items.Add(mnuFormat);
    }
}

This would produce:

To create a shortcut that is a combination of keys, use the bit manipulation operator OR represented by |. Here is an example:

void InitializeComponent()
{
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuFile = new ToolStripMenuItem("&File");
        mnuFileNew = new ToolStripMenuItem("&New");

        mnuFileNew.ShortcutKeys = Keys.Control | Keys.N;

        mnuFileExit = new ToolStripMenuItem("E&xit");

        mnuFormat = new ToolStripMenuItem("For&mat");
        mnuFormatFont = new ToolStripMenuItem("Fo&nt");
        mnuFormatFont.ShortcutKeys = Keys.F4;

        mnuFile.DropDownItems.Add(mnuFileNew);
        mnuFile.DropDownItems.Add(mnuFileExit);
        mnuMain.Items.Add(mnuFile);

        mnuFormat.DropDownItems.Add(mnuFormatFont);
        mnuMain.Items.Add(mnuFormat);
}

This would produce:

Normally, when you have associated a shortcut with a menu item, when the user displays the menu, the shortcut would appear. In some applications, you may want to hide the shortcut. To support this, the ToolStripMenuItem class is equipped with the Boolean ShowShortcutKeys property. The default value of this property is true. If you want to hide the shortcut, you can set this property to false.

Practical LearningPractical Learning: Creating Shortcuts

  1. Under the form, click mnuMain
  2. In the Properties window, click Items and click its ellipsis button
  3. In the Members list of the Items Collection Editor, click mnuFile
  4. On the right side, click DropDownItems and click its ellipsis button
  5. In the Members list, click mnuFileNewProperty
  6. In the right list, click ShortcutKeys and click the arrow of its combo box
  7. In the window that appears, click the Ctrl check box
  8. Click the arrow of the combo box next to Reset, scroll down and select N
  9. In the Items Collection Editor (mnuTools.DropDownItems), click OK
  10. In the Items Collection Editor, click OK

Three Periods

When a user has clicked a menu item, an action is supposed to occur. In some cases, an intermediary action is necessary before performing or completing the action. To indicate that an intermediary action is needed for the action related to the menu item, Microsoft standards suggest that the menu's text be followed by three periods. For example, in WordPad, if you want to display the date or the time or both on a document, you must open a dialog box that would present various options for you to choose how the date/time should be displayed. To indicate that you will perform a primary action before displaying the value, the menu that leads to it shows three periods:

The 3-periods menu

In this case, when you click the menu item, a dialog box would come up for you to select the desired value.

There is no programmatic relationship between the application and the menu item that displays three periods. It is only a suggestion to show them. Therefore, when creating a menu item, if you know that an intermediary action will be used to perform or complete the actual action, add three periods on the right side of its text. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuSelect;
    ToolStripMenuItem mnuSelectColor;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuSelect = new ToolStripMenuItem("&Select");

        mnuSelectColor = new ToolStripMenuItem("Background Color...");

        mnuSelect.DropDownItems.Add(mnuSelectColor);
        mnuMain.Items.Add(mnuSelect);
    }
}

This would produce:

Because the three periods indicate to the user that an intermediary action will be performed, when implementing the code for the menu item, make sure you provide that intermediary action. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuSelect;
    ToolStripMenuItem mnuSelectColor;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuSelect = new ToolStripMenuItem("&Select");
        mnuSelectColor = new ToolStripMenuItem("Background Color...");
        mnuSelectColor.Click +=
            new EventHandler(SelectBackgroundColor);

        mnuSelect.DropDownItems.Add(mnuSelectColor);
        mnuMain.Items.Add(mnuSelect);
    }

    void SelectBackgroundColor(object sender, EventArgs e)
    {
        ColorDialog dlgColor = new ColorDialog();

        if (dlgColor.ShowDialog() == DialogResult.OK)
            BackColor = dlgColor.Color;
    }
}

Practical LearningPractical Learning: Creating an Intermediary Action

  1. Under the form, click cmsProperties
  2. In the Properties window, click Items and click its ellipsis button
  3. In the Select Items And Add To List Below combo box, make sure MenuItem is selected and click Add
  4. On the right side, click Text and type New Property...
  5. Click (Name) and type mnuProperty
  6. Click Shortcut and click the arrow of its combo box
  7. Click the Ctrl check box and click the arrow of the combo box to select N
  8. In the Items Collection Editor, click OK
  9. On the main menu, click Project -> Add Windows Form
  10. Set the Name to PropertyEditor and click Add
  11. Design the form as follows:
     
    Control Text Name Other Properties
    Label Property Code:    
    TextBox   txtPropertyCode  
    Button OK btnOK DialogResult: OK
    Label   Property Type:  
    ComboBox Unknown cbxPropertyTypes Items:
    Unknown
    Apartment
    Townhouse
    Single Family
    Button Cancel btnCalncel DialogResult: Cancel
    Label Bedrooms:    
    TextBox 0 txtBedrooms  
    Label Bathrooms:    
    TextBox 0.00 txtBathrooms  
    Label Monthly Rent:    
    TextBox 0.00 txtMonthlyRent  
    Label Occupancy Status:    
    ComboBox Unknown cbxStatus Unknown
    Available
    Occupied
    Needs Repair
    Form FormBorderStyle:  FixedDialog
    Text: Solas Property Rental - Property Editor
    StartPosition: CenterScreen
    AcceptButton: btnOK
    CancelButton: btnCancel
    MaximizeBox: False
    MinimizeBox: False
    ShowInTaskbar: False
  12. On the main menu, click Window -> Central.cs [Design]
  13. Under the form, click cmsProperties and, on the form, double-click New Property
  14. Implement the event as follows:
     
    private void mnuProperty_Click(object sender, EventArgs e)
    {
        RentalProperty SampleProperty = new RentalProperty();
        PropertyEditor editor = new PropertyEditor();
    
        Random rndCode = new Random();
        string strCode1 = rndCode.Next(100, 999).ToString();
        string strCode2 = rndCode.Next(100, 999).ToString();
        editor.txtPropertyCode.Text = strCode1 + "-" + strCode2;
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            SampleProperty = new RentalProperty();
            SampleProperty.PropertyCode = editor.txtPropertyCode.Text;
            SampleProperty.PropertyType = editor.cbxPropertyTypes.Text;
            SampleProperty.Bedrooms = int.Parse(editor.txtBedrooms.Text);
            SampleProperty.Bathrooms = float.Parse(editor.txtBathrooms.Text);
            SampleProperty.MonthlyRent = decimal.Parse(editor.txtMonthlyRent.Text);
            SampleProperty.Status = editor.cbxStatus.Text;
            lstRentalProperties.Add(SampleProperty);
        }
                
        lvwRentalProperties.Items.Clear();
    
        if (lstRentalProperties.Count > 0)
        {
            foreach (RentalProperty prop in lstRentalProperties)
            {
                ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                itmProperty.SubItems.Add(prop.PropertyType);
                itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                itmProperty.SubItems.Add(prop.Status);
                lvwRentalProperties.Items.Add(itmProperty);
            }
        }
    }
  15. Return to the (Central) form
  16. On the form, click File and click New Property
  17. In the Properties window, edit its Text property to display &New Property...
  18. On the form, click File and double-click New Property
  19. Implement the event as follows:
     
    private void mnuFileNewProperty_Click(object sender, EventArgs e)
    {
            mnuProperty_Click(sender, e);
    }
  20. Execute the application and try creating the following properties (let the computer generate the properties codes):
     
    Property Types Bedrooms Bathrooms Monthly Rent Status
    Apartment 1 1 925 Occupied
    Apartment 2 1 1150.50 Available
    Single Family 5 3.5 2250.85 Occupied
    Townhouse 3 2.5 1750 Occupied
    Townhouse 4 2.5 1920.50 Available
    Single Family 4 2.5 2140.50 Needs Repair
    Apartment 3 2 1250.25 Available
    Townhouse 3 1.5 1650.50 Occupied
  21. Close the form and return to your programming environment

Grouping Menu Items

 

Menu Separators

As we will see in later sections, there are various ways you can make a menu look good and you have many options to configure menu items. One of the ways you can manage menu items is to group them in small entities of your choice. You can do this either for the looks or for aesthetic reasons.

A menu separator is a horizontal line among some menu items to visually divide them. Here is an example:

There are two reasons you would use a separator. You can use a separator just for aesthetic reasons, to make your menu look good. Another, more valuable reason, is to create groups of menu items and show their belonging together by showing a line separating one group from another.

To visually specify a separator, when creating the menu item, set its string to a simple -.

To support menu separators, the .NET Framework provides the ToolStripSeparator class, which is derived from ToolStripItem. To programmatically create a separator, declare a handle to ToolStripSeparator, initialize it using the new operator, add it to the Items property of the ToolStripItem menu category that will hold the separator. Here is an example:

public class Exercise : System.Windows.Forms.Form
{
    MenuStrip mnuMain;
    ToolStripMenuItem mnuFile;
    ToolStripMenuItem mnuFileNew;
    ToolStripSeparator mnuSeparator;
    ToolStripMenuItem mnuFileExit;

    public Exercise()
    {
        InitializeComponent();
    }

    void InitializeComponent()
    {
        mnuMain = new MenuStrip();
        Controls.Add(mnuMain);

        mnuFile = new ToolStripMenuItem("&File");
        mnuFileNew = new ToolStripMenuItem("&New");
        mnuFileNew.ShortcutKeys = Keys.Control | Keys.N;

        mnuSeparator = new ToolStripSeparator();

        mnuFileExit = new ToolStripMenuItem("E&xit");

        mnuFile.DropDownItems.Add(mnuFileNew);
        mnuFile.DropDownItems.Add(mnuSeparator);
        mnuFile.DropDownItems.Add(mnuFileExit);
        mnuMain.Items.Add(mnuFile);
    }
}

This would produce:

Menu Separator

Practical LearningPractical Learning: Creating an Intermediary Action

  1. On the (Central) form, click File and, under New Property, click Type Here
  2. Type - and press Enter
  3. Under the new separator, click Type Here, type E&xit and press Enter
  4. On the form, click File and click Exit
  5. In the Properties window, change the (Name) to mnuFileExit
  6. On the form, click File and double-click Exit
  7. Implement the event as follows:
     
    private void mnuFileExit_Click(object sender, EventArgs e)
    {
            Close();
    }
  8. Execute the application to test it
  9. Close the form using its main menu and return to your programming environment
  10. Under the form, click cmsProperties
  11. In the Properties window, click Items and click the ellipsis button
  12. In the Select Item And Add To List Below combo box, select Separator and click Add
  13. In the Select Item And Add To List Below combo box, select MenuItem and click Add
  14. On the side, change the properties as follows:
    Text: Show
    (Name): mnuShow
  15. Click OK

Sub-Menus

If you have menu items that perform similar tasks, you can put them in a group, which you can do using line separators. Another option is to create the menu items in their own group. The group of menu items that are created as children of a parent menu is referred to as a sub-menu. Here is an example of a sub-menu in Microsoft Paint:

Sub Menu

To visually create a sub-menu, under the form, click the menu control that will hold the items. In the menu designer

  • If the sub-menu will be created for a main menu item, first click the menu category, then click the menu item that will hold the sub-menu
  • If the sub-menu will be created for a contextual menu, click the menu item that will hold the sub-menu

After selecting the eventual parent of the intended sub-menu, click the right Type Here box, type the desired caption and optionally give it a name.

To create another item for the sub-menu, you can click the Type Here box under the previous one. In the same way, you can add as many items as you judge necessary. Here is an example:

You can also create a sub-menu for a menu item that itself is a sub-menu. Here is an example:

To create a sub-menu for an item A that itself is a sub-menu, click that menu item A, click the Type Here box on the right side, and type its caption.

As another technique, after selecting the menu item that will act as the parent of the sub-menu, in the Properties window, click the ellipsis button of the DropDownItems field to open the Items Collection Editor dialog box. To create an item for the sub-menu, in the top combo box, select MenuItem and click Add. Then configure the menu item as see fit (Text, (Name), etc).

Like any menu item, each sub-menu item is an object of type ToolStripMenuItem. Therefore, to programmatically create a sub-menu, create each ToolStripMenuItem item and add it to the ToolStripMenuItem menu item that will act as its parent.

Practical LearningPractical Learning: Creating and Using Sub-Menus

  1. Under the form, click cmsProperties and, on the form, click Show
  2. On the right side of Show, click Type Here, type All and press Enter
  3. Under All, click Type Here and type Apartments
  4. Press the down arrow key and type Townhouses
  5. Press the down arrow key and type Single Families
  6. On the form, click All and, in the Properties window, change its Name to mnuShowAll
  7. On the form, click Apartments and, in the Properties window, change its Name to mnuShowApartments
  8. On the form, click Townhouses and, in the Properties window, change its Name to mnuShowTownhouses
  9. On the form, click Single Families and, in the Properties window, change its Name to mnuShowSingleFamilies
  10. On the form and in the menu designer, double-click All and implement the event as follows:
     
    private void mnuShowAll_Click(object sender, EventArgs e)
    {
            lvwRentalProperties.Items.Clear();
    
            if (lstRentalProperties.Count > 0)
            {
                foreach (RentalProperty prop in lstRentalProperties)
                {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                    itmProperty.SubItems.Add(prop.PropertyType);
                    itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                    itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                    itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                    itmProperty.SubItems.Add(prop.Status);
                    lvwRentalProperties.Items.Add(itmProperty);
                }
            }
    }
  11. Return to the form and, on the form, double-click Apartments
  12. Implement the event as follows:
     
    private void mnuShowApartments_Click(object sender, EventArgs e)
    {
        lvwRentalProperties.Items.Clear();
    
        if (lstRentalProperties.Count > 0)
        {
            foreach (RentalProperty prop in lstRentalProperties)
            {
                if (prop.PropertyType == "Apartment")
                {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                    itmProperty.SubItems.Add(prop.PropertyType);
                    itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                    itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                    itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                    itmProperty.SubItems.Add(prop.Status);
                    lvwRentalProperties.Items.Add(itmProperty);
                }
            }
        }
    }
  13. Return to the form and, on the form, double-click Townhouse
  14. Implement the event as follows:
     
    private void mnuShowTownhouses_Click(object sender, EventArgs e)
    {
        lvwRentalProperties.Items.Clear();
    
        if (lstRentalProperties.Count > 0)
        {
            foreach (RentalProperty prop in lstRentalProperties)
            {
                if (prop.PropertyType == "Townhouse")
                {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                    itmProperty.SubItems.Add(prop.PropertyType);
                    itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                    itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                    itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                    itmProperty.SubItems.Add(prop.Status);
                    lvwRentalProperties.Items.Add(itmProperty);
                }
            }
        }
    }
  15. Return to the form and, on the form, double-click Single Families
  16. Implement the event as follows:
     
    private void mnuShowSingleFamilies_Click(object sender, EventArgs e)
    {
        lvwRentalProperties.Items.Clear();
    
        if (lstRentalProperties.Count > 0)
        {
            foreach (RentalProperty prop in lstRentalProperties)
            {
                if (prop.PropertyType == "Single Family")
                {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                    itmProperty.SubItems.Add(prop.PropertyType);
                    itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                    itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                    itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                    itmProperty.SubItems.Add(prop.Status);
                    lvwRentalProperties.Items.Add(itmProperty);
                }
            }
        }
    }
  17. Execute the application to test it
  18. Create a few properties as earlier then change the types of properties to display
  19. Close the form and return to your programming environment

Checked Box Menu Items

 

Introduction

Some applications are meant to display more than one form at the same time, or to optionally display and dismiss some forms some time to time. With this type of application, you may need a menu "witness" that can indicate whether the optional form is currently displaying or not. Some other applications may change their view some time to time. For these reasons and others, you can use the menu to assist the user with identifying an option that is currently available or not. You can do this through a check box on a menu item.

Implementing Check Boxes

To assist you with displaying a check box on a menu item, the ToolStripMenuItem class is equipped with a property named Checked. If you are visually creating a menu, to show a check mark on a menu item, access its Properties window and get to its Checked field. The default value of this property is false, which means the menu item is not meant to display a check box. To show a check box, you can set this property to true. When the user has clicked the menu item, you can then programmatically change its value from true to false and vice-versa.

When the application is running, to put a check mark on it, the user can click the menu item. If an item is displaying a check mark and the user clicks it, the check mark disappears. In reality, this is not an automatic functionality and it doesn't happen at random: you must configure it.

As mentioned already, to support check marks, the ToolStripMenuItem class is equipped with the Boolean Checked  property. If you want a menu item to exhibit the appropriate functionality a check box, you must write code for it, which fortunately is particularly easy (at least easier than it is done in Win32).

Practical LearningPractical Learning: Using Checked Boxes on Menu Items

  1. Under the form, click mnuMain
  2. In the Properties window, click Items and click the ellipsis button
  3. In the Select Item And Add To List Below combo box, make sure MenuItem is selected and click Add
  4. On the right side, click Text and type &Show
  5. Click (Name) and press type mnuShowProperty
  6. Click DropDownItems and click its ellipsis button
  7. In the Select Item And Add To List Below combo box, make sure MenuItem is selected and click Add
  8. On the right side, change the following two properties
    Text: &All 
    (Name): mnuAll
    Shortcut: Ctrl+Shift+L
  9. In the Select Item And Add To List Below combo box, make sure MenuItem is selected and click Add
  10. On the right side, change the following two properties
    Text: &Apartments
    (Name): mnuApartments
    Shortcut: Ctrl+Shift+A
  11. In the Select Item And Add To List Below combo box, make sure MenuItem is selected and click Add
  12. On the right side, change the following two properties
    Text: &Townhouses
    (Name): mnuTownhouses
    Shortcut: Ctrl+Shift+T
  13. In the Select Item And Add To List Below combo box, make sure MenuItem is selected and click Add
  14. On the right side, change the following two properties
    Text: &Single Families
  15. (Name): mnuSingleFamilies
    Shortcut: Ctrl+Shift+S
  16. Click OK and click OK
  17. Under the form, click mnuMain
  18. On the form, click File and double-click New Property
  19. Change the codes of the Show menu items as follows:
     
    private void mnuProperty_Click(object sender, EventArgs e)
    {
        RentalProperty SampleProperty = new RentalProperty();
        PropertyEditor editor = new PropertyEditor();
    
        Random rndCode = new Random();
        string strCode1 = rndCode.Next(100, 999).ToString();
        string strCode2 = rndCode.Next(100, 999).ToString();
        editor.txtPropertyCode.Text = strCode1 + "-" + strCode2;
    
        if (editor.ShowDialog() == DialogResult.OK)
        {
            SampleProperty = new RentalProperty();
            SampleProperty.PropertyCode = editor.txtPropertyCode.Text;
            SampleProperty.PropertyType = editor.cbxPropertyTypes.Text;
            SampleProperty.Bedrooms = int.Parse(editor.txtBedrooms.Text);
            SampleProperty.Bathrooms = float.Parse(editor.txtBathrooms.Text);
            SampleProperty.MonthlyRent = decimal.Parse(editor.txtMonthlyRent.Text);
            SampleProperty.Status = editor.cbxStatus.Text;
            lstRentalProperties.Add(SampleProperty);
        }
    
            mnuShowAll_Click(sender, e);
    }
    
    private void mnuShowAll_Click(object sender, EventArgs e)
    {
            lvwRentalProperties.Items.Clear();
    
            if (lstRentalProperties.Count > 0)
            {
                    foreach (RentalProperty prop in lstRentalProperties)
                    {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                        itmProperty.SubItems.Add(prop.PropertyType);
                        itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                        itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                        itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                        itmProperty.SubItems.Add(prop.Status);
                        lvwRentalProperties.Items.Add(itmProperty);
                    }
    
                    mnuShowAll.Checked = true;
                    mnuAll.Checked = true;
                    mnuApartments.Checked = false;
                    mnuShowApartments.Checked = false;
                    mnuTownhouses.Checked = false;
                    mnuShowTownhouses.Checked = false;
                    mnuSingleFamilies.Checked = false;
                    mnuShowSingleFamilies.Checked = false;
            }
    }
    
    private void mnuShowApartments_Click(object sender, EventArgs e)
    {
            lvwRentalProperties.Items.Clear();
    
            if (lstRentalProperties.Count > 0)
            {
                    foreach (RentalProperty prop in lstRentalProperties)
                    {
                        if (prop.PropertyType == "Apartment")
                        {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                        itmProperty.SubItems.Add(prop.PropertyType);
                        itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                        itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                        itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                        itmProperty.SubItems.Add(prop.Status);
                        lvwRentalProperties.Items.Add(itmProperty);
                        }
    
                        mnuShowAll.Checked = false;
                        mnuAll.Checked = false;
                        mnuApartments.Checked = true;
                        mnuShowApartments.Checked = true;
                        mnuTownhouses.Checked = false;
                        mnuShowTownhouses.Checked = false;
                        mnuSingleFamilies.Checked = false;
                        mnuShowSingleFamilies.Checked = false;
                    }
            }
    }
    
    private void mnuShowTownhouses_Click(object sender, EventArgs e)
    {
            lvwRentalProperties.Items.Clear();
    
            if (lstRentalProperties.Count > 0)
            {
                    foreach (RentalProperty prop in lstRentalProperties)
                    {
                        if (prop.PropertyType == "Townhouse")
                        {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                        itmProperty.SubItems.Add(prop.PropertyType);
                        itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                        itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                        itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                        itmProperty.SubItems.Add(prop.Status);
                        lvwRentalProperties.Items.Add(itmProperty);
                        }
    
                        mnuShowAll.Checked = false;
                        mnuAll.Checked = false;
                        mnuApartments.Checked = false;
                        mnuShowApartments.Checked = false;
                        mnuTownhouses.Checked = true;
                        mnuShowTownhouses.Checked = true;
                        mnuSingleFamilies.Checked = false;
                        mnuShowSingleFamilies.Checked = false;
                    }
            }
    }
    
    private void mnuShowSingleFamilies_Click(object sender, EventArgs e)
    {
            lvwRentalProperties.Items.Clear();
    
            if (lstRentalProperties.Count > 0)
            {
                    foreach (RentalProperty prop in lstRentalProperties)
                    {
                        if (prop.PropertyType == "Single Family")
                        {
                    ListViewItem itmProperty = new ListViewItem(prop.PropertyCode);
                        itmProperty.SubItems.Add(prop.PropertyType);
                        itmProperty.SubItems.Add(prop.Bedrooms.ToString());
                        itmProperty.SubItems.Add(prop.Bathrooms.ToString("F"));
                        itmProperty.SubItems.Add(prop.MonthlyRent.ToString("F"));
                        itmProperty.SubItems.Add(prop.Status);
                        lvwRentalProperties.Items.Add(itmProperty);
                        }
    
                        mnuShowAll.Checked = false;
                        mnuAll.Checked = false;
                        mnuApartments.Checked = false;
                        mnuShowApartments.Checked = false;
                        mnuTownhouses.Checked = false;
                        mnuShowTownhouses.Checked = false;
                        mnuSingleFamilies.Checked = true;
                        mnuShowSingleFamilies.Checked = true;
                    }
            }
    }
  20. Return to the form
  21. On the form, click Show and double-click All
  22. Implement the event as follows:
     
    private void mnuAll_Click(object sender, EventArgs e)
    {
            mnuShowAll_Click(sender, e);
    }
  23. Return to the form, click Show and double-click Apartments
  24. Implement the event as follows:
     
    private void mnuApartments_Click(object sender, EventArgs e)
    {
            mnuShowApartments_Click(sender, e);
    }
  25. Return to the form, click Show double-click Townhouses
  26. Implement the event as follows:
     
    private void mnuTownhouses_Click(object sender, EventArgs e)
    {
            mnuShowTownhouses_Click(sender, e);
    }
  27. Under the form, click Show double-click Single Families
  28. Implement the event as follows:
     
    private void mnuSingleFamilies_Click(object sender, EventArgs e)
    {
            mnuSingleFamilies_Click(sender, e);
    }
  29. Execute the application to test it
  30. Create a few properties as done earlier
     
  31. Then change the types of properties to display
     
  32. Close the form and return to your programming environment

The Status of Checked Menu Item

When a menu item is checked it holds a status to indicate it. To assist you with getting this information, the ToolStripMenuItem class is equipped with a property named CheckState. This property allows you specify the type of check mark to put on a menu item or to find out the marked state of a menu item in terms of its check mark.

 
 

Previous Copyright 2008-2009, yevol.com Next