|
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 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;
}
}
}
|
- Return to the form
- On the form, click Show and double-click All
- Implement the event as follows:
private void mnuAll_Click(object sender, EventArgs e)
{
mnuShowAll_Click(sender, e);
}
|
- Return to the form, click Show and double-click Apartments
- Implement the event as follows:
private void mnuApartments_Click(object sender, EventArgs e)
{
mnuShowApartments_Click(sender, e);
}
|
- Return to the form, click Show double-click Townhouses
- Implement the event as follows:
private void mnuTownhouses_Click(object sender, EventArgs e)
{
mnuShowTownhouses_Click(sender, e);
}
|
- Under the form, click Show double-click Single Families
- Implement the event as follows:
private void mnuSingleFamilies_Click(object sender, EventArgs e)
{
mnuSingleFamilies_Click(sender, e);
}
|
- 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.
|
|