Home

Applications Menus:
Checked 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 © 2007 FunctionX, Inc. Next