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
Learning: Using Checked Boxes on Menu Items
|
|
- Under the form, click mnuMain
- In the Properties window, click Items and click the ellipsis button
- In the Select Item And Add To List Below combo box, make sure MenuItem
is selected and click Add
- On the right side, click Text and type &Show
- Click (Name) and press type mnuShowProperty
- Click DropDownItems and click its ellipsis button
- In the Select Item And Add To List Below combo box, make sure MenuItem
is selected and click Add
- On the right side, change the following two properties
Text: &All
(Name): mnuAll
Shortcut: Ctrl+Shift+L
- In the Select Item And Add To List Below combo box, make sure MenuItem
is selected and click Add
- On the right side, change the following two properties
Text: &Apartments
(Name): mnuApartments
Shortcut: Ctrl+Shift+A
- In the Select Item And Add To List Below combo box, make sure MenuItem
is selected and click Add
- On the right side, change the following two properties
Text: &Townhouses
(Name): mnuTownhouses
Shortcut: Ctrl+Shift+T
- In the Select Item And Add To List Below combo box, make sure MenuItem
is selected and click Add
- On the right side, change the following two properties
Text: &Single Families
- (Name): mnuSingleFamilies
Shortcut: Ctrl+Shift+S
- Click OK and click OK
- Under the form, click mnuMain
- On the form, click File and double-click New Property
- 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
|
- In the Class Name combo box, select mnuAll
- 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
|
- In the Class Name combo box, select mnuApartments
- 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
|
- In the Class Name combo box, select mnuTownhouses
- 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
|
- In the Class Name combo box, select mnuSingleFamilies
- 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
|
- Execute the application to test it
- Create a few properties as done earlier
- Then change the types of properties to display
- 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.
|
|