{"id":3667,"date":"2011-12-16T08:47:00","date_gmt":"2011-12-16T16:47:00","guid":{"rendered":"http:\/\/www.sapien.com\/blog\/?p=3667"},"modified":"2011-12-16T08:48:06","modified_gmt":"2011-12-16T16:48:06","slug":"primalforms-2011-spotlight-on-the-contextmenustrip-control","status":"publish","type":"post","link":"https:\/\/dev.sapien.com\/blog\/2011\/12\/16\/primalforms-2011-spotlight-on-the-contextmenustrip-control\/","title":{"rendered":"PrimalForms 2011: Spotlight on the ContextMenuStrip Control"},"content":{"rendered":"<p><em>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms.<\/em><\/p>\n<p>Last time we took a look at the TreeView . This time we will look at the ContextMenuStrip control:<\/p>\n<p><span style=\"font-size: medium\"><strong>ContextMenuStrip <\/strong>[System.Windows.Forms.ContextMenuStrip]<\/span><\/p>\n<p>Represents a shortcut menu. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ContextMenuStrip Toolbox Item\" border=\"0\" alt=\"ContextMenuStrip Toolbox Item\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ContextMenuStrip-Toolbox-Item1.png\" width=\"126\" height=\"17\"><\/p>\n<p>Default Event: Opening<\/p>\n<p><em>Why use a ContextMenuStrip control?<\/em><\/p>\n<p>Use the ContextMenuStrip when you want to display a menu of commands when the user right clicks on a specific control.  <\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ContextMenu1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ContextMenu\" border=\"0\" alt=\"ContextMenu\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ContextMenu_thumb1.png\" width=\"244\" height=\"124\"><\/a>  <\/p>\n<p><strong><em>Adding a ContextMenuStrip to a control:<\/em><\/strong>  <\/p>\n<p>For your convenience, when you drop a ContextMenuStrip in the designer, PrimalForms will automatically assign the ContextMenuStrip to any control you drop it on.  <\/p>\n<p>To manually assign a ContextMenuStrip to a control, you need to set the control\u2019s <em>ContextMenuStrip<\/em> property to the desired ContextMenuStrip control. Note: Not all controls have a ContextMenuStrip property, but the majority do.&nbsp; <\/p>\n<p>You can assign a ContextMenuStrip to a control by using the Property Pane:  <\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Assigning-a-ContextMenuStrip1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Assigning a ContextMenuStrip\" border=\"0\" alt=\"Assigning a ContextMenuStrip\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Assigning-a-ContextMenuStrip_thumb1.png\" width=\"244\" height=\"82\"><\/a>  <\/p>\n<p>Or you can also assign a ContextMenuStrip using the Script Editor:<\/p>\n<pre><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.ContextMenuStrip <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$contextmenustripService<\/span><\/pre>\n<p>Refer to the Show method example below to see how to manually display a context menu.&nbsp; <\/p>\n<p><strong><span style=\"font-size: small\"><font size=\"3\"><\/font><\/span><\/strong><\/p>\n<p><span style=\"font-size: small\"><font size=\"3\"><strong>Important Properties:<\/strong><\/font><\/span> <\/p>\n<p><strong>ShowCheckMargin<\/strong><\/p>\n<p>This property gets or sets a value indicating whether space for a check mark is shown on the left edge of the ToolStripMenuItem. <\/p>\n<p>Default Value: <em>False<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ShowCheckMargin\" border=\"0\" alt=\"ShowCheckMargin\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ShowCheckMargin1.png\" width=\"174\" height=\"96\"><\/p>\n<p>If the ShowcheckMargin is set to <em>False<\/em> and there is no image set, the check mark will display in the image location.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ShowCheckMargin False\" border=\"0\" alt=\"ShowCheckMargin False\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ShowCheckMargin-False1.png\" width=\"152\" height=\"96\"><\/p>\n<p>&nbsp;<\/p>\n<p><strong>ShowImageMargin<\/strong><\/p>\n<p>This property gets or sets a value indicating whether space for an image is shown on the left edge of the ToolStripMenuItem.<\/p>\n<p>Default Value: <em>True<\/em><\/p>\n<p>Image Margin Displayed:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ShowImageMargin\" border=\"0\" alt=\"ShowImageMargin\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ShowImageMargin1.png\" width=\"110\" height=\"74\"><\/p>\n<p>Image Margin Hidden:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ShowImageMargin  False\" border=\"0\" alt=\"ShowImageMargin  False\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ShowImageMargin-False1.png\" width=\"85\" height=\"74\"><\/p>\n<p>&nbsp;<\/p>\n<p><strong>ShowItemToolTips<\/strong><\/p>\n<p>This property gets or sets a value indicating whether ToolTips are to be displayed on ToolStrip items. <\/p>\n<p>Default Value: <em>False<\/em><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"MenuItem Tooltip\" border=\"0\" alt=\"MenuItem Tooltip\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/MenuItem-Tooltip1.png\" width=\"183\" height=\"85\"><\/p>\n<p>When this property is set to <em>True<\/em>, the tooltips set on each menu item will be displayed when the mouse hovers over it.<\/p>\n<p><strong>Items<\/strong><\/p>\n<p>This property contains all the menu items that belong to a ContextMenuStrip.<\/p>\n<p><em><strong>Adding Menu Items:<\/strong><\/em><\/p>\n<p>The designer offers two ways of editing and adding menu items to your context menu.<\/p>\n<p>1) Edit the ContextMenuStrip directly in the designer<\/p>\n<p>Select the added ContextMenuStrip at the bottom of the designer:<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ContextMenuStrip-Located-at-bottom-of-Designer1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ContextMenuStrip Located at bottom of Designer\" border=\"0\" alt=\"ContextMenuStrip Located at bottom of Designer\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ContextMenuStrip-Located-at-bottom-of-Designer_thumb1.png\" width=\"244\" height=\"104\"><\/a><\/p>\n<p>Once you\u2019ve selected the ContextMenuStrip, a menu strip will be displayed directly in the designer, allowing you to visually add and remove menu items.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Edit-ContextMenu-in-Designer1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Edit ContextMenu in Designer\" border=\"0\" alt=\"Edit ContextMenu in Designer\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Edit-ContextMenu-in-Designer_thumb1.png\" width=\"244\" height=\"240\"><\/a><\/p>\n<p>Note: You can edit the menu item\u2019s properties using the Property Pane.<\/p>\n<p>2) Using the MenuItem Editor<\/p>\n<p>The editor can be accessed via the Property Pane:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Edit Item in Property Pane\" border=\"0\" alt=\"Edit Item in Property Pane\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Edit-Item-in-Property-Pane1.png\" width=\"244\" height=\"93\"><\/p>\n<p>Or using the quick access menu in the designer:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Edit Items in Designer\" border=\"0\" alt=\"Edit Items in Designer\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Edit-Items-in-Designer1.png\" width=\"436\" height=\"172\"><\/p>\n<p>The Menu Item Collection Editor allows you to add and remove MenuItems and access the properties at the same time.<\/p>\n<p><a href=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Items-Collection-Editor1.png\"><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Items Collection Editor\" border=\"0\" alt=\"Items Collection Editor\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Items-Collection-Editor_thumb1.png\" width=\"244\" height=\"151\"><\/a><\/p>\n<p>There are different types of menu items you can add to your context menu.&nbsp; <\/p>\n<p><strong><em>Types of Menu Items:<\/em><\/strong><\/p>\n<p><em><strong>MenuItem<\/strong><\/em> \u2013 Is a selectable option displayed on a ContextMenuStrip. It is the general purpose read only menu item which supports sub menu items.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Menu Item\" border=\"0\" alt=\"Menu Item\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Menu-Item1.png\" width=\"109\" height=\"71\"><\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"499\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"139\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"358\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">Image<\/td>\n<td valign=\"top\" width=\"358\">The image that will be displayed next to the menu item.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">Checked<\/td>\n<td valign=\"top\" width=\"358\">Indicates whether the menu item is in the checked state<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">CheckState<\/td>\n<td valign=\"top\" width=\"358\">Indicates the state of the menu item.<\/p>\n<p>Values (Default Value: Unchecked):<br \/><em>Unchecked<\/em>&nbsp; &#8211; <br \/><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"UncheckedState\" border=\"0\" alt=\"UncheckedState\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/UncheckedState1.png\" width=\"99\" height=\"30\"><br \/><em>Checked<br \/><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"CheckedState\" border=\"0\" alt=\"CheckedState\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/CheckedState1.png\" width=\"99\" height=\"30\"><\/em><br \/><em>Indeterminate<\/em><br \/><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"IndeterminateState\" border=\"0\" alt=\"IndeterminateState\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/IndeterminateState1.png\" width=\"99\" height=\"29\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">CheckOnClick<\/td>\n<td valign=\"top\" width=\"358\">Indicates whether the item should toggle its selected state when clicked.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">DropDownMenuItems<\/td>\n<td valign=\"top\" width=\"358\">Specifies a submenu to display when the item is clicked. Functions similar to the ContextMenuStrip\u2019s items property.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">ShortcutKeys<\/td>\n<td valign=\"top\" width=\"358\">The shortcut key associated with the menu item. This is a quick way of assigning a short cut key to a command. <br \/><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ShortcutKeys\" border=\"0\" alt=\"ShortcutKeys\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ShortcutKeys1.png\" width=\"237\" height=\"141\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">Text<\/td>\n<td valign=\"top\" width=\"358\">The text displayed on the menu item.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"139\">Visible<\/td>\n<td valign=\"top\" width=\"358\">Determines whether the menu item is visible or hidden.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"497\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"140\"><strong>Events<\/strong><\/td>\n<td valign=\"top\" width=\"355\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"140\">Click<\/td>\n<td valign=\"top\" width=\"355\">This event occurs when the MenuItem is clicked. Use this event to process the menu command. You can also use the ContextMenuStrip\u2019s ItemClicked event as an alternative.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"140\">CheckedChanged<\/td>\n<td valign=\"top\" width=\"355\">This event occurs whenever the Check property is changed. Use this event to react to the check change as it occurs.<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"140\">CheckStateChanged<\/td>\n<td valign=\"top\" width=\"355\">The event occurs whenever the CheckState property is changed. Use this event to react to the check state change as it occurs.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<p><strong><\/strong><\/p>\n<p><strong><em>ComboBox<\/em><\/strong> \u2013 Displays a combo box on a ContextMenuStrip. The combo box menu item behaves like the traditional ComboBox control. See the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/21\/primalforms-2011-spotlight-on-the-combobox-control\/\">Spotlight on the ComboBox Control<\/a> blog article for more details. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"ComboBox Menu\" border=\"0\" alt=\"ComboBox Menu\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/ComboBox-Menu2.png\" width=\"143\" height=\"100\"><\/p>\n<blockquote>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"450\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"108\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"342\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"108\">Items <\/td>\n<td valign=\"top\" width=\"342\">This property contains a collection of items that are displayed in the combo box.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n<blockquote>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"450\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"152\"><strong>Event<\/strong><\/td>\n<td valign=\"top\" width=\"298\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"152\">SelectedIndexChanged <\/td>\n<td valign=\"top\" width=\"298\">This event occurs when the value of the SelectedIndex property has changed. Use this event to react to the selection change as it occurs.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>&nbsp;<\/p>\n<\/blockquote>\n<p><em><strong>TextBox<\/strong><\/em> \u2013 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 <a href=\"http:\/\/www.sapien.com\/blog\/2011\/06\/13\/primalforms-2011-spotlight-on-the-textbox-control\/\">Spotlight on the TextBox Control<\/a> blog article for more details. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"TextBox MenuItem\" border=\"0\" alt=\"TextBox MenuItem\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/TextBox-MenuItem1.png\" width=\"165\" height=\"54\"><\/p>\n<blockquote>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"450\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"108\"><strong>Properties<\/strong><\/td>\n<td valign=\"top\" width=\"342\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"108\">Text <\/td>\n<td valign=\"top\" width=\"342\">This property contains the text that is displayed in the text box.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<table border=\"1\" cellspacing=\"0\" cellpadding=\"2\" width=\"450\">\n<tbody>\n<tr>\n<td valign=\"top\" width=\"107\"><strong>Event<\/strong><\/td>\n<td valign=\"top\" width=\"343\"><strong>Description<\/strong><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\" width=\"107\">TextChanged <\/td>\n<td valign=\"top\" width=\"343\">This event occurs when the value of the Text property changes. Use this event to react to the selection change as it occurs.<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<\/blockquote>\n<p><em><strong>Separator<\/strong><\/em> -Represents a static line used to group the drop-down items of the ContextMenuStrip control. <\/p>\n<p><img loading=\"lazy\" decoding=\"async\" style=\"background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px\" title=\"Separator MenuItem\" border=\"0\" alt=\"Separator MenuItem\" src=\"http:\/\/www.sapien.com\/blog\/wp-content\/uploads\/2011\/12\/Separator-MenuItem1.png\" width=\"165\" height=\"105\"><\/p>\n<p>&nbsp;<\/p>\n<p><strong><em>Adding Menu Items Dynamically:<\/em><\/strong><\/p>\n<p>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.<\/p>\n<pre><span style=\"color: #008000\">#Add a new menu Item<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$contextmenustripService<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #ff0000\">'New Menu Item'<\/span><span style=\"color: #000000\">)\n\n<\/span><span style=\"color: #008000\">#Add a Separator<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$contextmenustripService<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #ff0000\">'-'<\/span><span style=\"color: #000000\">)\n\n<\/span><span style=\"color: #008000\">#Add a combo box item<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$comboBoxMenuItem<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> System.Windows.Forms.ToolStripComboBox\n<\/span><span style=\"color: #8b0000\">$comboBoxMenuItem<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #ff0000\">'Option 1'<\/span><span style=\"color: #000000\">)\n<\/span><span style=\"color: #8b0000\">$comboBoxMenuItem<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #ff0000\">'Option 2'<\/span><span style=\"color: #000000\">)\n<\/span><span style=\"color: #8b0000\">$contextmenustripService<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$comboBoxMenuItem<\/span><span style=\"color: #000000\">)\n\n<\/span><span style=\"color: #008000\">#Add a text box item<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$textBoxMenuItem<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff; font-weight: bold\">New-Object<\/span><span style=\"color: #000000\"> System.Windows.Forms.ToolStripTextBox\n<\/span><span style=\"color: #8b0000\">$<span style=\"color: #8b0000\">textBoxMenuItem<\/span><\/span><span style=\"color: #000000\">.Text <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Default Text'<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #8b0000\">$contextmenustripService<\/span><span style=\"color: #000000\">.Items.Add(<\/span><span style=\"color: #8b0000\">$<span style=\"color: #8b0000\">textBoxMenuItem<\/span><\/span><span style=\"color: #000000\">)<\/span><\/pre>\n<p>&nbsp;<\/p>\n<p><strong><font size=\"3\">Important Events:<\/font><\/strong><\/p>\n<p><strong>ItemClicked<\/strong><\/p>\n<p>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\u2019s click event.<\/p>\n<p>You can access the clicked item by accessing the event\u2019s argument ClickItem property:<\/p>\n<p><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.ClickedItem <\/span><\/p>\n<p>The following is an example of how to respond to the ItemClicked event:<\/p>\n<pre><span style=\"color: #8b0000\">$contextmenustripService_ItemClicked<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ToolStripItemClickedEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ToolStripItemClickedEventArgs]<\/span><span style=\"color: #000000\">\n\n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.ClickedItem <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$startToolStripMenuItem<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Start'<\/span><span style=\"color: #000000\">\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\"> (<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.ClickedItem.Text <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Stop'<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Stop'<\/span><span style=\"color: #000000\">\n    }\n}<\/span><\/pre>\n<p><strong>Opening<\/strong><\/p>\n<p>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.<\/p>\n<p>You can prevent the ContextMenuStrip from opening by cancelling the event:<\/p>\n<pre><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><\/pre>\n<p>The following is an example demonstrating the use of the Opening event to initialize the menu items:<\/p>\n<pre><span style=\"color: #8b0000\">$contextmenustripService_Opening<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.ComponentModel.CancelEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.ComponentModel.CancelEventArgs]<\/span><span style=\"color: #000000\">\n\n    <\/span><span style=\"color: #8b0000\">$pauseToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$startToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #8b0000\">$stopToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.SelectedNode <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.SelectedNode.Tag\n    \n        <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #0000ff\">-ne<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$null<\/span><span style=\"color: #000000\">)<\/span><span style=\"color: #008000\">#Is there is a service object?<\/span><span style=\"color: #000000\">\n        {\n            <\/span><span style=\"color: #8b0000\">$pauseToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n            <\/span><span style=\"color: #8b0000\">$startToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n            <\/span><span style=\"color: #8b0000\">$stopToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$false<\/span><span style=\"color: #000000\">\n            \n            <\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.Refresh() <\/span><span style=\"color: #008000\">#Update the status<\/span><span style=\"color: #000000\">\n        \n            <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.Status <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Running'<\/span><span style=\"color: #000000\">)\n            {\n                <\/span><span style=\"color: #8b0000\">$stopToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n                <\/span><span style=\"color: #8b0000\">$pauseToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.CanPauseAndContinue\n            }\n            <\/span><span style=\"color: #0000ff\">elseif<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$service<\/span><span style=\"color: #000000\">.Status <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Paused'<\/span><span style=\"color: #000000\">)\n            {\n                <\/span><span style=\"color: #8b0000\">$startToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n                <\/span><span style=\"color: #8b0000\">$stopToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n            }\n            <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n            {\n                <\/span><span style=\"color: #8b0000\">$startToolStripMenuItem<\/span><span style=\"color: #000000\">.Enabled <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">\n            }\n        }\n    }\n    <\/span><span style=\"color: #0000ff\">else<\/span><span style=\"color: #000000\">\n    {\n        <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Cancel <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$true<\/span><span style=\"color: #000000\">   <span style=\"color: #008000\">#Don\u2019t show the context menu<\/span>\n    }\n    \n}<\/span><\/pre>\n<p><strong>Closed<\/strong><\/p>\n<p>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.<\/p>\n<p>To determine why the ContextMenuStrip closed, you can access the event\u2019s argument CloseReason property:<\/p>\n<pre><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.CloseReason<\/span><\/pre>\n<p>Values:<\/p>\n<blockquote>\n<p><em>AppFocusChange<\/em><br \/>Specifies that the ContextMenuStrip control was closed because another application has received the focus. <\/p>\n<p><em>AppClicked<\/em><br \/>Specifies that the ContextMenuStrip control was closed because an application was launched. <\/p>\n<p><em>ItemClicked<\/em><br \/>Specifies that the ContextMenuStrip control was closed because one of its items was clicked. <\/p>\n<p><em>Keyboard<\/em><br \/>Specifies that the ContextMenuStrip control was closed because of keyboard activity, such as the ESC key being pressed. <\/p>\n<p><em>CloseCalled<\/em><br \/>Specifies that the ContextMenuStrip control was closed because the Close method was called. <\/p>\n<p><em><\/em>&nbsp;<\/p>\n<\/blockquote>\n<p>Example use of the Closed event:<\/p>\n<pre><span style=\"color: #8b0000\">$contextmenustripService_Closed<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.ToolStripDropDownClosedEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.ToolStripDropDownClosedEventArgs]<\/span><span style=\"color: #000000\">\n    <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">\"Close Reason: $($_.CloseReason)\"<\/span><span style=\"color: #000000\">\n    \n    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.CloseReason <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'ItemClicked'<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'ComboBox Value: '<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$comboBoxMenuItem<\/span><span style=\"color: #000000\">.SelectedText\n        <\/span><span style=\"color: #0000ff; font-weight: bold\">Write-Host<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'TextBox Value: '<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$textBoxMenuItem<\/span><span style=\"color: #000000\">.Text\n    }\n}<\/span><\/pre>\n<p><font size=\"3\"><strong>Important Methods:<\/strong><\/font><\/p>\n<p><strong>Close<\/strong><\/p>\n<p>This method closes the ContextMenuStrip control.<\/p>\n<pre><span style=\"color: #8b0000\">$contextmenustripService<\/span><span style=\"color: #000000\">.Close()<\/span><\/pre>\n<p><strong>Show<\/strong><\/p>\n<p>This method&nbsp; displays the context menu. Use this method when you wish to manually trigger the context menu.<\/p>\n<p>Example use of the Show method:<\/p>\n<pre><span style=\"color: #8b0000\">$treeviewServices_NodeMouseClick<\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\">[System.Windows.Forms.TreeNodeMouseClickEventHandler]{\n<\/span><span style=\"color: #008000\">#Event Argument: $_ = [System.Windows.Forms.TreeNodeMouseClickEventArgs]<\/span><span style=\"color: #000000\">\n<\/span><span style=\"color: #000000\">    <\/span><span style=\"color: #0000ff\">if<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Button <\/span><span style=\"color: #0000ff\">-eq<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #ff0000\">'Right'<\/span><span style=\"color: #000000\">)\n    {\n        <\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">.SelectedNode <\/span><span style=\"color: #0000ff\">=<\/span><span style=\"color: #000000\"> <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Node\n        \n        <\/span><span style=\"color: #008000\">#Display a context menu for any node without setting<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #008000\">#each individual contextmenustrip property<\/span><span style=\"color: #000000\">\n        \n        <\/span><span style=\"color: #008000\">#Show the context menu relative to a control<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #8b0000\">$contextmenustripService<\/span><span style=\"color: #000000\">.Show(<\/span><span style=\"color: #8b0000\">$treeviewServices<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #8b0000\">$_<\/span><span style=\"color: #000000\">.Location)\n        \n        <\/span><span style=\"color: #008000\">#Alternative: Show context menu using a screen location<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #008000\">#$truePoint = $treeviewServices.PointToScreen($_.Location)<\/span><span style=\"color: #000000\">\n        <\/span><span style=\"color: #008000\">#$contextmenustripService.Show($truePoint)<\/span><span style=\"color: #000000\">\n    }\n}<\/span><\/pre>\n<p>In the example above, the context menu is displayed whenever a user right clicks on a node in the TreeView control.<\/p>\n<p>You can download the <a href=\"http:\/\/www.sapien.com\/downloads#Sample Scripts\/PrimalForms 2011\/ServiceswithContextMenuStrip.zip\">ContextMenuStrip<\/a> example from our <a href=\"http:\/\/www.sapien.com\/downloads\">Downloads<\/a> section. This sample builds upon the <a href=\"http:\/\/www.sapien.com\/blog\/2011\/11\/23\/primalforms-2011-spotlight-on-the-treeview-control-part-2\/\">Spotlight on the TreeView Control<\/a> example. <\/p>\n","protected":false},"excerpt":{"rendered":"<p>The \u201cSpotlight on Controls\u201d series focuses on a single WinForms control in PrimalForms 2011 , details the important Properties, Methods, and Events of the control and demonstrates how to utilize the control. Most of the information about the controls is still applicable to previous versions of PrimalForms. Last time we took a look at the [&hellip;]<\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[509,575,644,25],"tags":[693,641,28,633,327],"class_list":["post-3667","post","type-post","status-publish","format-standard","hentry","category-primalforms-software-news","category-primalforms-2011-software-news","category-spotlight-on-controls","category-windows-powershell","tag-contextmenustrip","tag-controls","tag-powershell","tag-primalforms-2011","tag-winform"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3667","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/comments?post=3667"}],"version-history":[{"count":2,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3667\/revisions"}],"predecessor-version":[{"id":3672,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/posts\/3667\/revisions\/3672"}],"wp:attachment":[{"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/media?parent=3667"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/categories?post=3667"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.sapien.com\/blog\/wp-json\/wp\/v2\/tags?post=3667"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}