 |
Visual Characteristics of Menu Items |
|
|
|
|
Visual Assistance With Menu Items |
|
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:

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
Learning: Introducing Menu Appearance
|
|
- Start a new Windows Forms Application named SolasPropertyRental2
- Change the properties of the form as follows:
Text: Solas Property Rental
StartPosition: CenterScreen
- On the main menu, click Project -> Add Class...
- Set the Name to RentalProperty and click Add
- 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;
}
}
|
- In the Solution Explorer, right-click Form1.cs and click Rename
- Type Central.cs and press Enter twice (to display the form)
- Double-click an unoccupied area of the form to generate its Load
event
- 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();
}
}
}
|
- Re-display the form
- In the Menus & Toolbars section of the Toolbox, click the MenuStrip button
and click the form
- While the menu strip is still selected, in the Properties window,
click (Name) and type mnuMain
- In the Common Controls section of the Toolbox, click ListView and
click the form
- 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
- Still in the Properties window, click Columns and click its ellipsis
button
- 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 |
|
|
- Click OK
- In the Menus & Toolbars section of the Toolbox, click the
ContextMenuStrip button
and click the form
- While the menu strip is still selected, in the Properties window,
click (Name) and type cmsProperties
- On the form, click the list view
- In the Properties window, click ContextMenuStrip and select
cmsProperties
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:

|
Practical
Learning: Using Access Keys
|
|
- Under the form, click mnuMain
- In the Properties window, click Items and click its ellipsis button
- In the Items Collection Editor, make sure MenuItem is selected in
the Select Item And Add To List Below combo box and click Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &File
(Name): mnuFile
- Still in the right list, click DropDownItems and click its ellipsis
button
- In the Items Collection Editor, make sure MenuItem is selected in
the Select Item And Add To List Below combo box and click Add
- While toolStripMenuItem1 is selected in the Members combo box, in
the right list, change the following characteristics:
Text: &New Property
(Name): mnuFileNewProperty
- In the Items Collection Editor (mnuFile.DropDownItems), click OK
- In the Items Collection Editor, click OK
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
Learning: Creating Shortcuts
|
|
- Under the form, click mnuMain
- In the Properties window, click Items and click its ellipsis
button
- In the Members list of the Items Collection Editor, click mnuFile
- On the right side, click DropDownItems and click its ellipsis button
- In the Members list, click mnuFileNewProperty
- In the right list, click ShortcutKeys and click the arrow of
its combo box
- In the window that appears, click the Ctrl check box
- Click the arrow of the combo box next to Reset, scroll down and
select N
- In the Items Collection Editor (mnuTools.DropDownItems), click OK
- In the Items Collection Editor, click OK
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:

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
Learning: Creating an Intermediary Action
|
|
- Under the form, click cmsProperties
- In the Properties window, click Items and click its ellipsis button
- In the Select Items And Add To List Below combo box, make sure MenuItem is
selected and click Add
- On the right side, click Text and type New Property...
- Click (Name) and type mnuProperty
- Click Shortcut and click the arrow of its combo box
- Click the Ctrl check box and click the arrow of the combo box to select N
- In the Items Collection Editor, click OK
- On the main menu, click Project -> Add Windows Form
- Set the Name to PropertyEditor and click Add
- 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 |
- On the main menu, click Window -> Central.cs [Design]
- Under the form, click cmsProperties and, on the form, double-click New
Property
- 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);
}
}
}
|
- Return to the (Central) form
- On the form, click File and click New Property
- In the Properties window, edit its Text property to display &New
Property...
- On the form, click File and double-click New Property
- Implement the event as follows:
private void mnuFileNewProperty_Click(object sender, EventArgs e)
{
mnuProperty_Click(sender, e);
}
|
- 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 |
- Close the form and return to your programming environment
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:

|
Practical
Learning: Creating an Intermediary Action
|
|
- On the (Central) form, click File and, under New Property, click Type Here
- Type - and press Enter
- Under the new separator, click Type Here, type E&xit and press
Enter
- On the form, click File and click Exit
- In the Properties window, change the (Name) to mnuFileExit
- On the form, click File and double-click Exit
- Implement the event as follows:
private void mnuFileExit_Click(object sender, EventArgs e)
{
Close();
}
|
- Execute the application to test it
- Close the form using its main menu and return to your programming
environment
- Under the form, click cmsProperties
- In the Properties window, click Items and click the ellipsis button
- In the Select Item And Add To List Below combo box, select Separator and
click Add
- In the Select Item And Add To List Below combo box, select MenuItem and
click Add
- On the side, change the properties as follows:
Text: Show
(Name): mnuShow
- Click OK
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:

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
Learning: Creating and Using Sub-Menus
|
|
- Under the form, click cmsProperties and, on the form, click Show
- On the right side of Show, click Type Here, type All and
press Enter
- Under All, click Type Here and type Apartments
- Press the down arrow key and type Townhouses
- Press the down arrow key and type Single Families
- On the form, click All and, in the Properties window, change its Name to mnuShowAll
- On the form, click Apartments and, in the Properties window, change its
Name to mnuShowApartments
- On the form, click Townhouses and, in the Properties window, change its
Name to mnuShowTownhouses
- On the form, click Single Families and, in the Properties window, change
its Name to mnuShowSingleFamilies
- 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);
}
}
}
|
- Return to the form and, on the form, double-click Apartments
- 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);
}
}
}
}
|
- Return to the form and, on the form, double-click Townhouse
- 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);
}
}
}
}
|
- Return to the form and, on the form, double-click Single Families
- 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);
}
}
}
}
|
- Execute the application to test it
- Create a few properties as earlier then change the types of properties to
display
- Close the form and return to your programming environment
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.
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.
|
|