Characteristics of Menu Items |
|
Introduction |
In the previous lesson, we saw how to create a menu. 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 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") mnuFile.DropDownItems.Add(mnuFileNew) mnuMain.Items.Add(mnuFile) 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:
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.
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:
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.
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
|
|
||
Home | Copyright © 2008-2016, FunctionX, Inc. | Next |
|