|
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
- In the Solution Explorer, right-click Form1.vb and click Rename
- Type Central.vb and press Enter twice (to display the form)
- 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:
Public Class RentalProperty
Private code As String
Private type As String
Private beds As Integer
Private baths As Single
Private rent As Double
Private status As String
Public Property PropertyCode() As String
Get
Return code
End Get
Set(ByVal value As String)
code = value
End Set
End Property
Public Property PropertyType() As String
Get
Return type
End Get
Set(ByVal value As String)
type = value
End Set
End Property
Public Property Bedrooms() As Integer
Get
Return beds
End Get
Set(ByVal value As Integer)
beds = value
End Set
End Property
Public Property Bathrooms() As Single
Get
Return baths
End Get
Set(ByVal value As Single)
baths = value
End Set
End Property
Public Property MonthlyRent() As Double
Get
Return rent
End Get
Set(ByVal value As Double)
rent = value
End Set
End Property
Public Property OccupancyStatus() As String
Get
Return status
End Get
Set(ByVal value As String)
status = value
End Set
End Property
End Class
|
- In the Solution Explorer, right-click Central.vb and click View Code
- Just above the first line, type Imports System.Collections
and, in the class, declare an ArrayList variable named lstRentalProperties
Imports System.Collections
Public Class Central
Private lstRentalProperties As ArrayList
End Class
|
- In the Class Name combo box, select (Central Events)
- In the Method Name combo box, select Load and initialize the
variable as follows:
Private Sub Central_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Me.Load
lstRentalProperties = New ArrayList
End Sub
|
- Re-display the Central 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:
Public Sub InitializeComponent()
mnuMain = New MenuStrip
Controls.Add(mnuMain)
mnuFile = New ToolStripMenuItem("&File")
mnuFileNew = New ToolStripMenuItem("&New")
mnuFile.DropDownItems.Add(mnuFileNew)
mnuMain.Items.Add(mnuFile)
End Sub
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:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private mnuMain As MenuStrip
Private mnuFile As ToolStripMenuItem
Private mnuFileNew As ToolStripMenuItem
Private mnuFileExit As ToolStripMenuItem
Private mnuFormat As ToolStripMenuItem
Private mnuFormatFont As ToolStripMenuItem
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub 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)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
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:
Public Sub InitializeComponent()
mnuMain = New MenuStrip
Controls.Add(mnuMain)
mnuFile = New ToolStripMenuItem("&File")
mnuFileNew = New ToolStripMenuItem("&New")
mnuFileNew.ShortcutKeys = Keys.Control Or 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)
End Sub
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:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private mnuMain As MenuStrip
Private mnuSelect As ToolStripMenuItem
Private mnuSelectColor As ToolStripMenuItem
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
mnuMain = New MenuStrip
Controls.Add(mnuMain)
mnuSelect = New ToolStripMenuItem("&Select")
mnuSelectColor = New ToolStripMenuItem("Background Color...")
mnuSelect.DropDownItems.Add(mnuSelectColor)
mnuMain.Items.Add(mnuSelect)
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
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:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private mnuMain As MenuStrip
Private mnuSelect As ToolStripMenuItem
Friend WithEvents mnuSelectColor As ToolStripMenuItem
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
mnuMain = New MenuStrip
Controls.Add(mnuMain)
mnuSelect = New ToolStripMenuItem("&Select")
mnuSelectColor = New ToolStripMenuItem("Background Color...")
mnuSelect.DropDownItems.Add(mnuSelectColor)
mnuMain.Items.Add(mnuSelect)
End Sub
Private Sub SelectBackgroundColor(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles mnuSelectColor.Click
Dim dlgColor As ColorDialog = New ColorDialog
If dlgColor.ShowDialog() = Windows.Forms.DialogResult.OK Then
BackColor = dlgColor.Color
End If
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
|
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 |
- In the Solution Explorer, right-click Central.vb and click View Code
- In the Class Name combo box, select mnuProperty
- In the Method Name combo box, select Click and implement the event as follows:
Private Sub mnuProperty_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles mnuProperty.Click
Dim RandomCode As Random
Dim Editor As PropertyEditor
Dim SampleProperty As RentalProperty
Dim strCode1 As String, strCode2 As String
RandomCode = New Random
strCode1 = CStr(RandomCode.Next(100, 999))
strCode2 = CStr(RandomCode.Next(100, 999))
Editor = New PropertyEditor
Editor.txtPropertyCode.Text = strCode1 & "-" & strCode2
If Editor.ShowDialog() = Windows.Forms.DialogResult.OK Then
SampleProperty = New RentalProperty
SampleProperty.PropertyCode = Editor.txtPropertyCode.Text
SampleProperty.PropertyType = Editor.cbxPropertyTypes.Text
SampleProperty.Bedrooms = CInt(Editor.txtBedrooms.Text)
SampleProperty.Bathrooms = CSng(Editor.txtBathrooms.Text)
SampleProperty.MonthlyRent = CDbl(Editor.txtMonthlyRent.Text)
SampleProperty.OccupancyStatus= Editor.cbxStatus.Text
lstRentalProperties.Add(SampleProperty)
End If
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
End Sub
|
- 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 Sub mnuFileNewProperty_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuFileNewProperty.Click
mnuProperty_Click(sender, e)
End Sub
|
- 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:
Imports System.Drawing
Imports System.Windows.Forms
Module Exercise
Public Class Starter
Inherits Form
Private mnuMain As MenuStrip
Private mnuFile As ToolStripMenuItem
Private mnuFileNew As ToolStripMenuItem
Private mnuSeparator As ToolStripSeparator
Friend WithEvents mnuFileExit As ToolStripMenuItem
Dim components As System.ComponentModel.Container
Public Sub New()
InitializeComponent()
End Sub
Public Sub InitializeComponent()
mnuMain = New MenuStrip
Controls.Add(mnuMain)
mnuFile = New ToolStripMenuItem("&File")
mnuFileNew = New ToolStripMenuItem("&New")
mnuFileNew.ShortcutKeys = Keys.Control Or 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)
End Sub
Private Sub FileExiting(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles mnuFileExit.Click
End
End Sub
End Class
Function Main() As Integer
Dim frmStart As Starter = New Starter
Application.Run(frmStart)
Return 0
End Function
End Module
This would produce:

|
Practical
Learning: Creating a Menu Separator
|
|
- 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 Sub mnuFileExit_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles mnuFileExit.Click
End
End Sub
|
- 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
- Right-click the form and click View Code
- In the Class Name combo box, select mnuShowAll
- In the Method Name combo box, select Click and implement the event 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
End Sub
|
- In the Class Name combo box, select mnuShowApartments
- In the Method Name combo box, select Click and implement the event as follows:
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
End Sub
|
- In the Class Name combo box, select mnuShowTownhouses
- In the Method Name combo box, select Click and implement the event as follows:
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
End Sub
|
- In the Class Name combo box, select mnuShowSingleFamilies
- In the Method Name combo box, select Click and implement the event as follows:
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
End Sub
|
- 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 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.
|
|