The method of the MenuStrip class creates a ToolStripMenuItem on a new MenuStrip
Here are the various options: 1. Register a new student 2. Review a student's information 3. Enter student's grades 4. Close the application The user would then enter the number (or character) that corresponds to the desired option and continue using the program. For a graphical application, a menu is presented as a list of words and, using a mouse or a keyboard, the user can select the desired item from the menu. To enhance the functionality of a graphical application, also to take advantage of the mouse and the keyboard, there are various types of menus. A menu is considered a main menu when it carries most of the actions the user can perform on a particular application. Such a menu is positioned in the top section of the form in which it is used. A main menu is divided in categories of items and each category is represented by a word. In WordPad, the categories of menus are File, Edit, View, Insert, Format, and Help: To use a menu, the user first clicks one of the words that displays on top. When clicked, the menu expands and displays a list of items that belong to that category. Here is an example: There is no strict rule on how a menu is organized. There are only suggestions. For example, actions that are related to file processing, such as creating a new file, opening an existing file, saving a file, printing the open file, or closing the file usually stay under a category called File. In the same way, actions related to viewing documents can be listed under a View menu category. To support actions that are used to graphically enhance the functionality of an application, the .NET Framework provides the ToolStrip class: type ToolStrip = class inherit ScrollableControl interface IComponent interface IDisposable end To support menus for an application, the .NET Framework provides the MenuStrip class: type MenuStrip = class inherit ToolStrip end To create a main menu, declare a variable of type MenuStrip class and initialize it with its default constructor. Because the main menu is primarily a control, you must add it to the list of controls of the form. Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
exercise.Controls.Add mnuMain
do Application.Run exercise To support menu items, the .NET Framework provides the ToolStripMenuItem class: type ToolStripMenuItem = class inherit ToolStripDropDownItem end Using it, to create a menu category, declare a variable of the type of this class and initialize it using one of its constructors. The default constructor is used to create a menu item without specifying any of its options. Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
let mnuFile : ToolStripMenuItem = new ToolStripMenuItem()
exercise.Controls.Add mnuMain
do Application.Run exercise To specify the caption that will be displayed on a menu category, you can use the following constructor of the ToolStripMenuItem class: new : text:string -> ToolStripMenuItem Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
let mnuFile : ToolStripMenuItem = new ToolStripMenuItem("File")
exercise.Controls.Add mnuMain
do Application.Run exercise If you had instantiated the class using its default constructor, to specify its caption, the ToolStripMenuItem class is equipped with the Text property: abstract Text : string with get, set override Text : string with get, set Therefore, assign a string to this property. Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
let mnuFile : ToolStripMenuItem = new ToolStripMenuItem("File")
let mnuEdit : ToolStripMenuItem = new ToolStripMenuItem()
mnuEdit.Text <- "Edit"
exercise.Controls.Add mnuMain
do Application.Run exercise In the same way, you can create as many menu categories as you judge necessary for your application.
In our introduction, we saw that if you click a menu category, a list comes up. Here is an example: Both the menu category and the menu item are created using the ToolStripMenuItem class. Here are examples: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
let mnuFile : ToolStripMenuItem = new ToolStripMenuItem("File")
let mnuNew : ToolStripMenuItem = new ToolStripMenuItem("New")
let mnuExit : ToolStripMenuItem = new ToolStripMenuItem("Exit")
let mnuEdit : ToolStripMenuItem = new ToolStripMenuItem("Edit")
let mnuCopy : ToolStripMenuItem = new ToolStripMenuItem("Copy")
exercise.Controls.Add mnuMain
do Application.Run exercise
To support menu categories, ToolStripMenuItem, the class used to create menu categories, is derived from a class named ToolStripDropDownItem. The ToolStripDropDownItem class is abstract: [ This means that you cannot instantiate it. Instead, it provides functionality to other classes derived from it. The ToolStripDropDownItem class is based on the ToolStripItem class. To support menu items, the ToolStripDropDownItem class is equipped with a property named DropDownItems. This property is of type ToolStripItemCollection, which a collection-based class: member DropDownItems : ToolStripItemCollection with get The ToolStripItemCollection class implements the IList and the ICollection interfaces. To specify that a menu item will be part of a menu category, call the Add() method of the ToolStripItemCollection class. This method is overloaded with various versions. One of the versions uses the following syntax: member Add : value:ToolStripItem -> int This version allows you to pass a ToolStripItem-type of item class, such as a ToolStripMenuItem object. Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuEdit : ToolStripMenuItem = new ToolStripMenuItem("Edit")
let mnuCopy : ToolStripMenuItem = new ToolStripMenuItem("Copy")
mnuEdit.DropDownItems.Add mnuCopy |> ignore
do Application.Run exercise The ToolStripItemCollection class also allows you to create a menu item without going through a ToolStripItem-type of object. To support this, its provides the following version of its Add() method: member Add : text:string -> ToolStripItem This method takes as argument the text that the menu item would display and it returns the ToolStripItem item that was created. Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuEdit : ToolStripMenuItem = new ToolStripMenuItem("Edit")
let mnuCopy : ToolStripMenuItem = new ToolStripMenuItem("Copy")
mnuEdit.DropDownItems.Add mnuCopy |> ignore
let mnuPaste = mnuEdit.DropDownItems.Add "Paste" |> ignore
do Application.Run exercise Instead of adding one menu item at a time, you can create an array of menu items and then add it to a category in one row. To support this, the ToolStripItemCollection class implements the AddRange() method. This method is overloaded with two versions. One of the versions uses the following syntax: member AddRange : toolStripItems:ToolStripItem[] -> unit When calling this method, you must pass it an array of ToolStripItem-type of objects.
After creating the menu categories, you can add them to the main menu. To support this, the ToolStrip class is equipped with a property named Items and it makes this property available to the MenuStrip class. The Items property is of type ToolStripItemCollection. This class implements the IList, the ICollection, and the IEnumerable interfaces. Therefore, to add a menu category to a MenuStrip object, you can call the Add() method of the ToolStripItemCollection class. This method is overloaded with various versions and one of them uses the following version: member Add : value:ToolStripItem -> int You can call this version and pass it a ToolStripItem-type of object, such as a ToolStripMenuItem value. Here is an example: open System
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
let mnuFile : ToolStripMenuItem = new ToolStripMenuItem("File")
let mnuNew : ToolStripMenuItem = new ToolStripMenuItem("New")
mnuFile.DropDownItems.Add mnuNew |> ignore
let mnuOpen : ToolStripMenuItem = new ToolStripMenuItem("Open")
mnuFile.DropDownItems.Add mnuOpen |> ignore
let mnuExit : ToolStripMenuItem = new ToolStripMenuItem("Exit")
mnuFile.DropDownItems.Add mnuExit |> ignore
mnuMain.Items.Add mnuFile |> ignore
exercise.Controls.Add mnuMain
do Application.Run exercise In the same way, you can add the other items. Alternatively, you can create an array of menu categories and add them in a row. To support this, the ToolStripItemCollection is equipped with the AddRange() method that is overloaded with two versions. One of the versions uses the following syntax: member AddRange : toolStripItems:ToolStripItem[] -> unit When calling this method, pass it an array of ToolStripItem types of values. Here is an example: open System
open System.Drawing
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
let mnuFile : ToolStripMenuItem = new ToolStripMenuItem("File")
let mnuNew : ToolStripMenuItem = new ToolStripMenuItem("New")
mnuFile.DropDownItems.Add mnuNew |> ignore
let mnuOpen : ToolStripMenuItem = new ToolStripMenuItem("Open")
mnuFile.DropDownItems.Add mnuOpen |> ignore
let mnuExit : ToolStripMenuItem = new ToolStripMenuItem("Exit")
mnuFile.DropDownItems.Add mnuExit |> ignore
mnuMain.Items.Add mnuFile |> ignore
let mnuView = new ToolStripMenuItem("View")
let mnuViewItems : ToolStripItem array =
[| new ToolStripMenuItem("Standard Toolbar")
new ToolStripMenuItem("Formatting Toolbar")
new ToolStripMenuItem("Status Bar") |]
mnuView.DropDownItems.AddRange mnuViewItems
let mnuHelp = new ToolStripMenuItem("Help")
let mnuHelpItems : ToolStripItem array =
[| new ToolStripMenuItem("Search")
new ToolStripMenuItem("Contents")
new ToolStripMenuItem("Index")
new ToolStripMenuItem("Support Web Site")
new ToolStripMenuItem("About this application")
|]
mnuHelp.DropDownItems.AddRange mnuHelpItems
let mnuAccessories : ToolStripItem array = [| mnuView; mnuHelp |]
mnuMain.Items.AddRange mnuAccessories
exercise.Controls.Add mnuMain
do Application.Run exercise This would produce: If you create a menu as we have just done, to write code for one of the menu items, you can double-click the menu item. This would open the Click event of the menu item in the Code Editor and you can start writing the desired code for that item.
Here is an example of a starting menu: open System
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
exercise.Controls.Add mnuMain
let mnuFile = new ToolStripMenuItem("File")
let mnuFileNew = new ToolStripMenuItem("New")
mnuFile.DropDownItems.Add mnuFileNew |> ignore
mnuMain.Items.Add mnuFile |> ignore
do Application.Run exercise 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: open System open System.Windows.Forms let exercise = new Form() let mnuMain = new MenuStrip() exercise.Controls.Add mnuMain let mnuFile = new ToolStripMenuItem("&File") let mnuFileNew = new ToolStripMenuItem("&New") mnuFile.DropDownItems.Add mnuFileNew |> ignore mnuMain.Items.Add mnuFile |> ignore do Application.Run exercise 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. You are probably more familiar with shortcuts made of combinations of keys, such as Ctrl + N, Alt + F6, or Ctrl + Alt + Delete. To support shortcuts, the ToolStripMenuItem class is equipped with a property named ShortcutKeys. To 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: member ShortcutKeys : Keys with get, set Keys is an enumeration of the various keys of a keyboard recognized by Microsoft Windows. Here is an example of using it: open System open System.Windows.Forms let exercise = new Form() let mnuMain = new MenuStrip() exercise.Controls.Add mnuMain let mnuFile = new ToolStripMenuItem("&File") let mnuFileNew = new ToolStripMenuItem("&New") mnuFile.DropDownItems.Add mnuFileNew |> ignore let mnuFileExit = new ToolStripMenuItem("E&xit") mnuFile.DropDownItems.Add mnuFileExit |> ignore mnuMain.Items.Add mnuFile |> ignore let mnuFormat = new ToolStripMenuItem("For&mat") let mnuFormatFont = new ToolStripMenuItem("Fo&nt") mnuFormatFont.ShortcutKeys <- Keys.F4 mnuFormat.DropDownItems.Add mnuFormatFont |> ignore mnuMain.Items.Add mnuFormat |> ignore do Application.Run exercise 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: open System
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
exercise.Controls.Add mnuMain
let mnuFile = new ToolStripMenuItem("&File")
let mnuFileNew = new ToolStripMenuItem("&New")
mnuFileNew.ShortcutKeys <- Keys.Control ||| Keys.N
mnuFile.DropDownItems.Add mnuFileNew |> ignore
let mnuFileExit = new ToolStripMenuItem("E&xit")
mnuFile.DropDownItems.Add mnuFileExit |> ignore
mnuMain.Items.Add mnuFile |> ignore
let mnuFormat = new ToolStripMenuItem("For&mat")
let mnuFormatFont = new ToolStripMenuItem("Fo&nt")
mnuFormatFont.ShortcutKeys <- Keys.F4
mnuFormat.DropDownItems.Add mnuFormatFont |> ignore
mnuMain.Items.Add mnuFormat |> ignore
do Application.Run exercise This would produce: Some applications don't show all of their shortcuts on the menu. To let you control the visibility of a shortcut, the ToolStripMenuItem class is equipped with a property named ShowShortcutKeys: member ShowShortcutKeys : bool with get, set If you want to hide a shortcut, after creating it, set the ShowShortcutKeys property to false. The default value of this property is true. 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: open System
open System.Windows.Forms
let exercise = new Form()
let mnuMain = new MenuStrip()
exercise.Controls.Add mnuMain
let mnuSelect = new ToolStripMenuItem("&Select")
let mnuSelectColor = new ToolStripMenuItem("Background Color...")
mnuSelect.DropDownItems.Add mnuSelectColor |> ignore
mnuMain.Items.Add mnuSelect |> ignore
do Application.Run exercise 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: open System open System.Windows.Forms let exercise = new Form() let mnuMain = new MenuStrip() exercise.Controls.Add mnuMain let mnuSelect = new ToolStripMenuItem("&Select") let mnuSelectColor = new ToolStripMenuItem("Background Color...") mnuSelect.DropDownItems.Add mnuSelectColor |> ignore mnuMain.Items.Add mnuSelect |> ignore let selectBackgroundColor e = let dlgColor : ColorDialog = new ColorDialog() if dlgColor.ShowDialog() = DialogResult.OK then exercise.BackColor <- dlgColor.Color mnuSelectColor.Click.Add selectBackgroundColor do Application.Run exercise What control can be place on a MenuStrip?The MenuStrip control supports the multiple-document interface (MDI) and menu merging, tool tips, and overflow. You can enhance the usability and readability of your menus by adding access keys, shortcut keys, check marks, images, and separator bars.
What is MenuStrip?MenuStrip is the top-level container that supersedes MainMenu. It also provides key handling and multiple document interface (MDI) features.
What is ToolStripMenuItem?ToolStripMenuItem(String, Image, ToolStripItem[]) Initializes a new instance of the ToolStripMenuItem class that displays the specified text and image and that contains the specified ToolStripItem collection.
What is the difference between MenuStrip and ContextMenuStrip?MenuStrip is used to add menu items on the form (along the top edge). ContextMenuStrip is used to add items that appear when you right-click on a control.
|