Home

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 Sub mnuShowAll_Click(ByVal sender As Object, _
                                     ByVal e As System.EventArgs) _
                                     Handles mnuShowAll.Click
            lvwRentalProperties.Items.Clear()
    
            If lstRentalProperties.Count > 0 Then
                For Each prop As RentalProperty In lstRentalProperties
                    Dim itmProperty As ListViewItem = _
    		    New ListViewItem(prop.PropertyCode)
                    itmProperty.SubItems.Add(prop.PropertyType)
                    itmProperty.SubItems.Add(CStr(prop.Bedrooms))
                    itmProperty.SubItems.Add(FormatNumber(prop.Bathrooms))
                    itmProperty.SubItems.Add(FormatNumber(prop.MonthlyRent))
                    itmProperty.SubItems.Add(prop.OccupancyStatus)
                    lvwRentalProperties.Items.Add(itmProperty)
                Next
            End If
    
            mnuShowAll.Checked = True
            mnuAll.Checked = True
            mnuApartments.Checked = False
            mnuShowApartments.Checked = False
            mnuTownhouses.Checked = False
            mnuShowTownhouses.Checked = False
            mnuSingleFamilies.Checked = False
            mnuShowSingleFamilies.Checked = False
    End Sub
    
    Private Sub mnuShowApartments_Click(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles mnuShowApartments.Click
            lvwRentalProperties.Items.Clear()
    
            If lstRentalProperties.Count > 0 Then
                For Each prop As RentalProperty In lstRentalProperties
                    If prop.PropertyType = "Apartment" Then
    
                        Dim itmProperty As ListViewItem = _
    			New ListViewItem(prop.PropertyCode)
                        itmProperty.SubItems.Add(prop.PropertyType)
                        itmProperty.SubItems.Add(CStr(prop.Bedrooms))
                        itmProperty.SubItems.Add(FormatNumber(prop.Bathrooms))
                        itmProperty.SubItems.Add(FormatNumber(prop.MonthlyRent))
                        itmProperty.SubItems.Add(prop.OccupancyStatus)
                        lvwRentalProperties.Items.Add(itmProperty)
                    End If
                Next
            End If
    
            mnuShowAll.Checked = False
            mnuAll.Checked = False
            mnuApartments.Checked = True
            mnuShowApartments.Checked = True
            mnuTownhouses.Checked = False
            mnuShowTownhouses.Checked = False
            mnuSingleFamilies.Checked = False
            mnuShowSingleFamilies.Checked = False
    End Sub
    
    Private Sub mnuShowTownhouses_Click(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles mnuShowTownhouses.Click
            lvwRentalProperties.Items.Clear()
    
            If lstRentalProperties.Count > 0 Then
                For Each prop As RentalProperty In lstRentalProperties
                    If prop.PropertyType = "Townhouse" Then
    
                        Dim itmProperty As ListViewItem = _
    			New ListViewItem(prop.PropertyCode)
                        itmProperty.SubItems.Add(prop.PropertyType)
                        itmProperty.SubItems.Add(CStr(prop.Bedrooms))
                        itmProperty.SubItems.Add(FormatNumber(prop.Bathrooms))
                        itmProperty.SubItems.Add(FormatNumber(prop.MonthlyRent))
                        itmProperty.SubItems.Add(prop.OccupancyStatus)
                        lvwRentalProperties.Items.Add(itmProperty)
                    End If
                Next
            End If
    
            mnuShowAll.Checked = False
            mnuAll.Checked = False
            mnuApartments.Checked = False
            mnuShowApartments.Checked = False
            mnuTownhouses.Checked = True
            mnuShowTownhouses.Checked = True
            mnuSingleFamilies.Checked = False
            mnuShowSingleFamilies.Checked = False
    End Sub
    
    Private Sub mnuShowSingleFamilies_Click(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles mnuShowSingleFamilies.Click
            lvwRentalProperties.Items.Clear()
    
            If lstRentalProperties.Count > 0 Then
                For Each prop As RentalProperty In lstRentalProperties
                    If prop.PropertyType = "Single Family" Then
                        Dim itmProperty As ListViewItem = _
    			New ListViewItem(prop.PropertyCode)
                        itmProperty.SubItems.Add(prop.PropertyType)
                        itmProperty.SubItems.Add(CStr(prop.Bedrooms))
                        itmProperty.SubItems.Add(FormatNumber(prop.Bathrooms))
                        itmProperty.SubItems.Add(FormatNumber(prop.MonthlyRent))
                        itmProperty.SubItems.Add(prop.OccupancyStatus)
                        lvwRentalProperties.Items.Add(itmProperty)
                    End If
                Next
            End If
    
            mnuShowAll.Checked = False
            mnuAll.Checked = False
            mnuApartments.Checked = False
            mnuShowApartments.Checked = False
            mnuTownhouses.Checked = False
            mnuShowTownhouses.Checked = False
            mnuSingleFamilies.Checked = True
            mnuShowSingleFamilies.Checked = True
    End Sub
  20. In the Class Name combo box, select mnuAll
  21. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuAll_Click(ByVal sender As Object, _
                                 ByVal e As System.EventArgs) _
                                 Handles mnuAll.Click
            mnuShowAll_Click(sender, e)
    End Sub
  22. In the Class Name combo box, select mnuApartments
  23. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuApartments_Click(ByVal sender As Object, _
                                        ByVal e As System.EventArgs) _
                                        Handles mnuApartments.Click
            mnuShowApartments_Click(sender, e)
    End Sub
  24. In the Class Name combo box, select mnuTownhouses
  25. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuTownhouses_Click(ByVal sender As Object, _
                                        ByVal e As System.EventArgs) _
                                        Handles mnuTownhouses.Click
            mnuShowTownhouses_Click(sender, e)
    End Sub
  26. In the Class Name combo box, select mnuSingleFamilies
  27. In the Method Name combo box, select Click and implement the event as follows:
     
    Private Sub mnuSingleFamilies_Click(ByVal sender As Object, _
                                            ByVal e As System.EventArgs) _
                                            Handles mnuSingleFamilies.Click
            mnuSingleFamilies_Click(sender, e)
    End Sub
  28. Execute the application to test it
  29. Create a few properties as done earlier
     
  30. Then change the types of properties to display
     
  31. 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-2012 FunctionX Next