User Rating: 3 / 5

Star ActiveStar ActiveStar ActiveStar InactiveStar Inactive
 

ContextMenuStrip [System.Windows.Forms.ContextMenuStrip]

Represents a shortcut menu.

ContextMenuStrip Toolbox Item

Default Event: Opening

Why use a ContextMenuStrip control?

Use the ContextMenuStrip when you want to display a menu of commands when the user right clicks on a specific control.

ContextMenu

 

Adding a ContextMenuStrip to a control:

For your convenience, when you drop a ContextMenuStrip in the designer, PowerShell Studio will automatically assign the ContextMenuStrip to any control you drop it on.

To manually assign a ContextMenuStrip to a control, you need to set the control’s ContextMenuStrip property to the desired ContextMenuStrip control. Note: Not all controls have a ContextMenuStrip property, but the majority do. 

You can assign a ContextMenuStrip to a control by using the Property Pane:

Assigning a ContextMenuStrip

Or you can also assign a ContextMenuStrip using the Script Editor:

$treeviewServices.ContextMenuStrip = $contextmenustripService

Refer to the Show method example below to see how to manually display a context menu. 

 

Important Properties:

ShowCheckMargin

This property gets or sets a value indicating whether space for a check mark is shown on the left edge of the ToolStripMenuItem.

Default Value: False

ShowCheckMargin

If the ShowcheckMargin is set to False and there is no image set, the check mark will display in the image location.

ShowCheckMargin False

 

ShowImageMargin

This property gets or sets a value indicating whether space for an image is shown on the left edge of the ToolStripMenuItem.

Default Value: True

Image Margin Displayed:

ShowImageMargin

Image Margin Hidden:

ShowImageMargin  False

 

ShowItemToolTips

This property gets or sets a value indicating whether ToolTips are to be displayed on ToolStrip items.

Default Value: False

MenuItem Tooltip

When this property is set to True, the tooltips set on each menu item will be displayed when the mouse hovers over it.

 

SourceControl

This property returns the last control that caused the ContextMenuStrip to display.

 

Items

This property contains all the menu items that belong to a ContextMenuStrip.

 

Adding Menu Items:

The designer offers two ways of editing and adding menu items to your context menu.

1) Edit the ContextMenuStrip directly in the designer

Select the added ContextMenuStrip at the bottom of the designer:

ContextMenuStrip Located at bottom of Designer

Once you’ve selected the ContextMenuStrip, a menu strip will be displayed directly in the designer, allowing you to visually add and remove menu items.

Edit ContextMenu in Designer

Note: You can edit the menu item’s properties using the Property Pane.

2) Using the MenuItem Editor

The editor can be accessed via the Property Pane:

Edit Item in Property Pane

Or using the quick access menu in the designer:

Edit Items in Designer

The Menu Item Collection Editor allows you to add and remove MenuItems and access the properties at the same time.

Items Collection Editor

There are different types of menu items you can add to your context menu. 

 

Types of Menu Items:

MenuItem – Is a selectable option displayed on a ContextMenuStrip. It is the general purpose read only menu item which supports sub menu items.

Menu Item

Properties Description
Image The image that will be displayed next to the menu item.
Checked Indicates whether the menu item is in the checked state
CheckState Indicates the state of the menu item.

 

Values (Default Value: Unchecked):
Unchecked  –
Unchecked State
Checked
Checked State

Indeterminate
Indeterminate State

CheckOnClick Indicates whether the item should toggle its selected state when clicked.
DropDownMenuItems Specifies a submenu to display when the item is clicked. Functions similar to the ContextMenuStrip’s items property.
ShortcutKeys The shortcut key associated with the menu item. This is a quick way of assigning a short cut key to a command.
Shortcut Keys
Text The text displayed on the menu item.
Visible Determines whether the menu item is visible or hidden.

 

Events Description
Click This event occurs when the MenuItem is clicked. Use this event to process the menu command. You can also use the ContextMenuStrip’s ItemClicked event as an alternative.
CheckedChanged This event occurs whenever the Check property is changed. Use this event to react to the check change as it occurs.
CheckStateChanged The event occurs whenever the CheckState property is changed. Use this event to react to the check state change as it occurs.

 

ComboBox – Displays a combo box on a ContextMenuStrip. The combo box menu item behaves like the traditional ComboBox control. See the Spotlight on the ComboBox Control article for more details.

ComboBox Menu

Properties Description
Items This property contains a collection of items that are displayed in the combo box.
Event Description
SelectedIndexChanged This event occurs when the value of the SelectedIndex property has changed. Use this event to react to the selection change as it occurs.

 

TextBox – Displays a text box in a ContextMenuStrip which allows the user to enter text. The text box menu item behaves like the traditional TextBox control. See the Spotlight on the TextBox Control article for more details.

TextBox MenuItem

Properties Description
Text This property contains the text that is displayed in the text box.
Event Description
TextChanged This event occurs when the value of the Text property changes. Use this event to react to the selection change as it occurs.

 

Separator -Represents a static line used to group the drop-down items of the ContextMenuStrip control.

Separator MenuItem

 

Adding Menu Items Dynamically:

Typically you will add menu items via the designer, but there are times when you may want to add a menu item dynamically via the script editor.

#Add a new menu Item
$contextmenustripService.Items.Add('New Menu Item')

#Add a Separator
$contextmenustripService.Items.Add('-')

#Add a combo box item
$comboBoxMenuItem = New-Object System.Windows.Forms.ToolStripComboBox
$comboBoxMenuItem.Items.Add('Option 1')
$comboBoxMenuItem.Items.Add('Option 2')
$contextmenustripService.Items.Add($comboBoxMenuItem)

#Add a text box item
$textBoxMenuItem = New-Object System.Windows.Forms.ToolStripTextBox
$textBoxMenuItem.Text = 'Default Text'
$contextmenustripService.Items.Add($textBoxMenuItem)

 

Important Events:

ItemClicked

This event occurs when a menu item is clicked. Use this event as a general catch all event for the menu items. It allows you to react to the menu item clicks without having to set each individual menu item’s click event.

You can access the clicked item by accessing the event’s argument ClickItem property:

$_.ClickedItem

The following is an example of how to respond to the ItemClicked event:

$contextmenustripService_ItemClicked=[System.Windows.Forms.ToolStripItemClickedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.ToolStripItemClickedEventArgs]

    if($_.ClickedItem -eq $startToolStripMenuItem)
    {
        Write-Host 'Start'
    }
    elseif ($_.ClickedItem.Text -eq 'Stop')
    {
        Write-Host 'Stop'
    }
}

 

Opening

This event occurs when the ContextMenuStrip is opening. Use this event as a trigger to initialize the menu items such as enabling / disabling or hiding menu items.

You can prevent the ContextMenuStrip from opening by cancelling the event:

$_.Cancel = $true

The following is an example demonstrating the use of the Opening event to initialize the menu items:

$contextmenustripService_Opening=[System.ComponentModel.CancelEventHandler]{
#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]

    $pauseToolStripMenuItem.Enabled = $false
    $startToolStripMenuItem.Enabled = $false
    $stopToolStripMenuItem.Enabled = $false
    
    if($treeviewServices.SelectedNode -ne $null)
    {
        $service = $treeviewServices.SelectedNode.Tag
    
        if($service -ne $null)#Is there is a service object?
        {
            $pauseToolStripMenuItem.Enabled = $false
            $startToolStripMenuItem.Enabled = $false
            $stopToolStripMenuItem.Enabled = $false
            
            $service.Refresh() #Update the status
        
            if($service.Status -eq 'Running')
            {
                $stopToolStripMenuItem.Enabled = $true
                $pauseToolStripMenuItem.Enabled = $service.CanPauseAndContinue
            }
            elseif($service.Status -eq 'Paused')
            {
                $startToolStripMenuItem.Enabled = $true
                $stopToolStripMenuItem.Enabled = $true
            }
            else
            {
                $startToolStripMenuItem.Enabled = $true
            }
        }
    }
    else
    {
        $_.Cancel = $true   #Don’t show the context menu
    }
    
}

 

Closed

This event occurs when the ContextMenuStrip has closed. You can use this event to handle state changes or selection changes in a combo box menu item if you do not handle their state changes as they occur.

To determine why the ContextMenuStrip closed, you can access the event’s argument CloseReason property:

$_.CloseReason

Values:

AppFocusChange
Specifies that the ContextMenuStrip control was closed because another application has received the focus.

 

AppClicked
Specifies that the ContextMenuStrip control was closed because an application was launched.

 

ItemClicked
Specifies that the ContextMenuStrip control was closed because one of its items was clicked.

 

Keyboard
Specifies that the ContextMenuStrip control was closed because of keyboard activity, such as the ESC key being pressed.

 

CloseCalled
Specifies that the ContextMenuStrip control was closed because the Close method was called.

Example use of the Closed event:

$contextmenustripService_Closed=[System.Windows.Forms.ToolStripDropDownClosedEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.ToolStripDropDownClosedEventArgs]
    Write-Host "Close Reason: $($_.CloseReason)"
    
    if($_.CloseReason -eq 'ItemClicked')
    {
        Write-Host 'ComboBox Value: ' $comboBoxMenuItem.SelectedText
        Write-Host 'TextBox Value: ' $textBoxMenuItem.Text
    }
}

 

Important Methods:

Close

This method closes the ContextMenuStrip control.

$contextmenustripService.Close()

 

Show

This method  displays the context menu. Use this method when you wish to manually trigger the context menu.

Example use of the Show method:

$treeviewServices_NodeMouseClick=[System.Windows.Forms.TreeNodeMouseClickEventHandler]{
#Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]
    if($_.Button -eq 'Right')
    {
        $treeviewServices.SelectedNode = $_.Node
        
        #Display a context menu for any node without setting
        #each individual contextmenustrip property
        
        #Show the context menu relative to a control
        $contextmenustripService.Show($treeviewServices, $_.Location)
        
        #Alternative: Show context menu using a screen location
        #$truePoint = $treeviewServices.PointToScreen($_.Location)
        #$contextmenustripService.Show($truePoint)
    }
}

In the example above, the context menu is displayed whenever a user right clicks on a node in the TreeView control.

You can download the ContextMenuStrip example. This sample builds upon the Spotlight on the TreeView Control example.

If you have questions about our products, please post in our support forum.
For licensed customers, use the forum associated with your product in our Product Support Forums for Registered Customers.
For users of trial versions, please post in our Former and Future Customers - Questions forum.
Copyright © 2024 SAPIEN Technologies, Inc.