Home

Applications Menus

 

The Main Menu

 

Introduction

When it comes to a restaurant, a menu is a list of food items that the business offers to its customers. For a computer application, a menu is a list of actions that can be performed on that program. To be aware of these actions, the list must be presented to the user upon request. On a typical DOS application, a menu is presented with numerical or character options that the user can select from. An example would be:

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:

Main Menu

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:

Menu Category

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.

Main Menu Creation

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

Menu Categories

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.

Introduction to Menu Items

In our introduction, we saw that if you click a menu category, a list comes up. Here is an example:

Menu expanded

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

Associating Menu Items to Menu Categories

To support menu categories, ToolStripMenuItem, the class used to create menu categories, is derived from a class named ToolStripDropDownItem. The ToolStripDropDownItem class is abstract:

[<AbstractClass>]
type ToolStripDropDownItem =  
    class 
        inherit ToolStripItem 
    end

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.

Associating Menu Categories to the Main Menu 

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:

Main menu

Coding a Menu Item

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.

Visual Assistance With Menu Items

 

Introduction to Visual Characteristics of Menu Items

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:

Main Menu

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.

Access Keys

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:

Access Keys

Shortcuts

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.

Three Periods

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:

The 3-periods menu

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
   
 

Grouping Menu Items

 

Menu Separators

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.

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 support menu separators, the .NET Framework provides the ToolStripSeparator class, which is derived from ToolStripItem:

type ToolStripSeparator =  
    class 
        inherit ToolStripItem 
    end

To create a separator, declare a variable of type 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:

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
mnuFileNew.ShortcutKeys <- Keys.Control ||| Keys.N
let mnuFileExit = new ToolStripMenuItem("E&xit")

let mnuSeparator = new ToolStripSeparator()
mnuFile.DropDownItems.Add mnuSeparator |> ignore

mnuFile.DropDownItems.Add mnuFileExit |> ignore
mnuMain.Items.Add mnuFile |> ignore

do Application.Run exercise

This would produce:

Menu Separator

Sub-Menus

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:

Sub Menu

Like any menu item, each sub-menu item is an object of type ToolStripMenuItem. Therefore, to create a sub-menu, create each ToolStripMenuItem item and add it to the ToolStripMenuItem menu item that will act as its parent.

Checked Box Menu Items

 

Introduction

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.

Implementing Check Boxes

To assist you with displaying a check box on a menu item, the ToolStripMenuItem class is equipped with a property named Checked:

member Checked : bool with get, set

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).

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:

member CheckState : CheckState with get, set

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.

Pictures on Menu Items

 

Introduction

Because Microsoft Windows is a graphical operating system, you can enhance the appearance of your menu items by displaying indicative pictures close to menu items. Here is an example of three menu items that display pictures:

Pictures on Menu Items

When introducing menus, we saw that each menu was treated separately. This means that you choose what menu would display a picture and which one would not display any picture. The picture to display can be of almost any format and you can use a picture of any size, which includes bitmaps, JPEGs, GIFs, and icons. Although you can use bitmaps, it may be better to use icons. The reason is that it is easier to control and even predict the background color of an icon. The operating system also is equipped to identify the background color of an icon. You can use icons that either you design or acquire one way or another.

Using Pictures on Menu Items

The (new) .NET Framework provides a complete and high level of support for pictures on a menu. To specify only the picture that the menu item would display, you can initialize it with the following constructor of the ToolStripMenuItem class:

new : 
        image:Image -> ToolStripMenuItem

Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise = new Form()

let mnuMain = new MenuStrip()
let mnuFile = new ToolStripMenuItem("&File")
let mnuFileNew = new ToolStripMenuItem(Image.FromFile(@"C:\Resources\new.ico"))
mnuFileNew.Text <- "&New"

mnuFile.DropDownItems.Add mnuFileNew |> ignore
mnuMain.Items.Add mnuFile |> ignore

exercise.Controls.Add mnuMain |> ignore

do Application.Run exercise

If you had created the menu item using either the default constructor or the constructor that takes only a string, to specify the picture of the menu, the ToolStripMenuItem is equipped with a property named Image that is of type Image:

abstract Image : Image with get, set 
override Image : Image with get, set

Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise = new Form()

let mnuMain = new MenuStrip()
let mnuFile = new ToolStripMenuItem("&File")
let mnuFileNew = new ToolStripMenuItem(Image.FromFile(@"C:\Resources\new.ico"))
mnuFileNew.Text <- "&New"
mnuFile.DropDownItems.Add mnuFileNew |> ignore
mnuMain.Items.Add mnuFile |> ignore

let mnuFileOpen = new ToolStripMenuItem("Open")
mnuFileOpen.Image <- Image.FromFile(@"C:\Resources\open.ico")
mnuFile.DropDownItems.Add mnuFileOpen |> ignore

exercise.Controls.Add mnuMain |> ignore

do Application.Run exercise

To specify both the caption and the picture of the menu, you can initialize it using the following constructor:

new : 
        text:string * 
        image:Image -> ToolStripMenuItem

Here is an example:

open System
open System.Drawing
open System.Windows.Forms

let exercise = new Form()

let mnuMain = new MenuStrip()
let mnuFile = new ToolStripMenuItem("&File")
let mnuFileNew = new ToolStripMenuItem(Image.FromFile(@"C:\Resources\book.ico"))
mnuFileNew.Text <- "&New"
mnuFile.DropDownItems.Add mnuFileNew |> ignore
mnuMain.Items.Add mnuFile |> ignore

let mnuFileOpen = new ToolStripMenuItem("Open")
mnuFileOpen.Image <- Image.FromFile(@"C:\Resources\book.ico")
mnuFile.DropDownItems.Add mnuFileOpen |> ignore

let mnuFileSave = new ToolStripMenuItem("Save",
                                        Image.FromFile(@"C:\Resources\book.ico"))
mnuFile.DropDownItems.Add mnuFileSave |> ignore

exercise.Controls.Add mnuMain |> ignore

do Application.Run exercise

Characteristics of Pictures

After specifying the picture to display on a menu item, you can exercise a great deal of control on its appearance. By default, the picture is displayed on the left side of the menu item's caption. The alignment is controlled by the ImageAlign property of the ToolStripItem class. The ImageAlign property is of type ContentAlignment, which is an enumeration:

member ImageAlign : ContentAlignment with get, set

It uses the same values as the content alignment of text on a control.

After specifying the picture to display on a menu item, if the picture is too big, it may get shrunk to fit 16 x 16. Still, if you want, you can keep the size. The ability to keep the 16 x 16 size or the original size is controlled by the ImageScaling property. This property is of type ToolStripItemImageScaling, which is an enumeration and has only two members:

member ImageScaling : ToolStripItemImageScaling with get, set

The default value of this property is None, which means the picture would be resized for a 16 x 16 size. An alternative is to set the property to SizeToFit. Here is an example:

You can make the menu item display both the picture and the caption. This characteristic is controlled by the DisplayStyle property of the ToolStripItem class. This property is of type ToolStripItemDisplayStyle, which is an enumeration:

abstract DisplayStyle : ToolStripItemDisplayStyle with get, set 
override DisplayStyle : ToolStripItemDisplayStyle with get, set
  • The default value of this property is ImageAndText. This value indicates that the menu item would display both the picture and the caption
  • If you set the property value to Image, the menu item would display only the image
  • If you set the property value to Text, the menu item would display only the caption
  • If you want to display neither the picture nor the caption, you can set this property to None

Menu Management

 

Copying a Menu

You can copy a menu category to duplicate it in your application. To assist you with programmatically copying a menu category or a menu item, the ToolStripItemCollection class is equipped a method named CopyTo. Its syntax is:

member CopyTo :
        array:ToolStripItem[] *
        index:int -> unit

This method takes a collection of menu items (as an array) and copies it to the index of your choice.

Enabling or Disabling a Menu Item

A menu item is said to be disabled if the user can see it but cannot use it. The caption of such a menu appears gray. Here are examples:

When a menu item is disabled, the user can position the mouse on it and click but nothing would happen. This indicates that the action of that menu item is not available at that time. The reverse to a disabled menu item is that it is enabled. That's the case for the Under and the Select All items in the above application. The user cannot directly enable or disable a menu. You as the application developer decides when (and why) the item would be enabled or disabled.

To support the ability to enable or to disable a menu item, the ToolStripMenuItem class is equipped with a Boolean property named Enabled:

abstract Enabled : bool with get, set 
override Enabled : bool with get, set

By default, a new menu item you have just created is enabled. This is because the default value of this property is true. To enable or re-enable it, you can set the value to true. In the same way, to find out the enabled status of a menu item, you can check the value of its Enabled property.

Hiding or Showing a Menu Item

A menu is said to be hidden if it is programmatically available behind the scenes but the user cannot see. Since the user cannot see it, he or she cannot use it. The reverse to a hidden menu item is that it is shown to the user. The user cannot directly hide or show a menu item. You as the application developer create a menu item and then decides when, how, and where (and why) the item would be hidden or shown.

To support the ability to enable or to disable a menu item, the ToolStripMenuItem class inherits a Boolean property named Visible from the its parents (the ToolStripMenuItem class inherits it from the ToolStripDropDownItem class that inherits it from the ToolStripItem class):

member Visible : bool with get, set

After you have just created a new menu, by default, it is made visible to the user. This is because the default value of the Visible property is set to true. To hide a menu item, you can set its Visible property to false. To show or to reveal it, you can set the value to true. In the same way, to find out whether a menu item is currently displaying, you can check the status of its Visible property.

Deleting a Menu Item

Instead of disabling or hiding a menu you don't want the user to use anymore, you can simply delete it. If you do this, you cannot programmatically refer to it anymore, unless you re-create it. To assist you with deleting a menu category or a menu item, the ToolStripItemCollection class is equipped with three methods. To delete a menu using its ToolStripItem object name, you can call the Remove() method. Its syntax is:

member Remove : 
        value:ToolStripItem -> unit

When calling this method, pass it the name of the menu category or the menu item you want to delete. If you want to delete a menu category or a menu item based on its index, you can call the following method:

member RemoveAt : 
        index:int -> unit

To delete a menu using its key, you can use the ToolStripItemCollection.RemoveByKey() method whose syntax is:

abstract RemoveByKey : 
        key:string -> unit  
override RemoveByKey : 
        key:string -> unit

In the same way, you can keep removing one item at a time. To help you delete all items, the ToolStripItemCollection class is equipped with a method named Clear. Its syntax is:

abstract Clear : unit -> unit  
override Clear : unit -> unit

When called, this method will get rid of all the menu categories and items that belong to the object that called it.

 
 
   
 

Home Copyright © 2015, FunctionX Home