To save the time required to create an application and reduce the amount of code, you can share multiple controls to share a single context menu object. With a "Dynamic" context menu (or shortcut menu) that only contains the control menu item, you can reduce the total number of context menus required in the application. The following exercise shows how to change the menu item for each control.
Create an application
The following steps will create a Windows application that has a form that contains two controls. At runtime, if you click on each control (as long as it has a focus, it is selected), the corresponding context menu will be displayed. The context menu of the Radiobutton control will contain two items; the context menu of the CheckBox control will contain three items.
Create a dynamic context menu on a Windows Form
Create a new Windows application. For more information, see Creating a Windows Application Project. Drag the Checkbox control and Radiobutton control from the Toolbox to the form. Although any two (or more) controls can share a context menu, it is also advantageous to share the context menu with similar commands, as this can reduce the required dynamic display and hidden quantity. Double-click the "ContextMenu" component in Toolbox and add it to the form. It will become a shared context menu. In the Properties window, set the CHECKBOX control and the Radiobutton control of the ContextMenu property to ContextMenu1 (in Visual Basic) or ContextMenu1 (in Visual C # or Visual C ). In the Properties window, set the THREESTATE property of the Checkbox control to True.
Note that you can also use code generation using code
ContextMenu and
The menu item and setting of the ThreeState property. For more information, see
Add a context menu to the Windows Form
Set the properties of controls, documents, and forms
. related
For more information on the ThreeState property, please see
Windows Form Checkbox Control Introduction.
Double-click the ContextMenu component from the designer to create a default handler for the component's Popup event. For more information, see Creating an Event Handle on the Windows Form Designer. Insert the code that performs the following tasks in the event handler:
Add two menu items, a Checked status representing the control, and another represents the unchecked state. Use the IF statement to verify if the checkbox control is on the form.
SourceControl
. According to the test results, dynamically add the third menu item, which indicates the Indeterminate status of the control. The following example shows how to use
Add
Methods To set the text attribute of the menu item and how to define the event handler associated with the menu item. For other add-on methods with different signatures, see
Menu.MenuItemCollection.Add method
. 'Visual Basic
Protected Sub ContextMenu1_Popup (Byval E AS System.EventArgs) Handles ContextMenu1.popup
'Clear The Contents of The Context Menu.
ContextMenu1.MenuItems.clear ()
'Add a menu it.
ContextMenu1.Menuitems.Add ("Checked", new system.eventhandler (addressofful me.checked_onclick) 'add a menu it unchecked state.
ContextMenu1.MenuItems.Add ("unchecked", new system.eventhandler (addressof me.unchecked_onclick)
'Test Which Control IT IS.
'If it is the checkbox, add a menu it.
IF ContextMenu1.SourceControl Is Checkbox1 Then
ContextMenu1.Menuitems.Add ("Indeterminate", New System.EventHandler
END IF
End Sub
// C #
Protected void contextMenu1_popup (System.Object Sender, System.EventArgs E)
{
// Clear The Contents of the Context Menu.
ContextMenu1.MenuItems.clear ();
// Add a menu item for the checked stat.
ContextMenu1.MenuItems.Add ("Checked", New System.EventHandler (this.checked_onclick);
// Add a menu it is the unchecked state.
ContextMenu1.MenuItems.Add ("unchecked", new system.eventhandler (this.unchecked_onclick);
// Test Which Control IS.
// if it is the checkbox, add a menu item for the indeterminate state.
IF (ContextMenu1.SourceControl == Checkbox1)
{
This.ContextMenu1.Menuitems.Add ("Indeterminate", New System.EventHandler (this.indeeeeterminate_onclick);
}
}
// C
Private:
System :: void contextMenu1_popup (System :: Object * Sender,
System :: Eventargs * e)
{
// Clear The Contents of the Context Menu.
ContextMenu1-> MenuItems-> clear ();
// Add a menu item for the checked stat.
ContextMenu1-> MenuItems-> Add ("checked",
New System :: EventHandler (this, checked_onclick);
// Add a menu it is the unchecked state.
ContextMenu1-> MenuItems-> Add ("unchecked",
New System :: EventHandler (this, unchecked_onclick); // Test Which Control IT IS.
// if it is the checkbox, add a menu item
// for the indeterminate state.
IF (ContextMenu1-> SourceControl == Checkbox1)
{
this-> contextMenu1-> Menuitems-> Add ("IndeteMinate",
New System :: EventHandler (this, Indeterminate_Onclick);
}
} Create an event handler for Menuitem1. Add the following code, check the SourceControl property of the form, then set the checked attribute of the Radiobutton or Checkbox control according to the test results: 'Visual Basic
Protected Sub Checked_onclick (Byval Sender As System.Object, ByVal E as System.EventArgs)
IF ContextMenu1.SourceControl Is Radiobutton1 Then
Radiobutton1.checked = true
Elseif ContextMenu1.SourceControl is Checkbox1 Then
Checkbox1.checked = true
END IF
End Sub
// C #
Protected void checked_onclick (System.Object Sender, System.EventArgs E)
{
IF (ContextMenu1.SourceControl == Radiobutton1)
Radiobutton1.checked = true;
Else IF (ContextMenu1.SourceControl == Checkbox1)
Checkbox1.checked = true;
}
// C
Private:
System :: void checked_onclick (System :: Object * Sender,
System :: Eventargs * e)
{
IF (ContextMenu1-> SourceControl == Radiobutton1)
Radiobutton1-> checked = true;
Else IF (ContextMenu1-> SourceControl == Checkbox1)
Checkbox1-> checked = true;
}
Note this example
Checkstate attribute
Indeterminate_ONCLICK Event Handler
Checkbox control is set to
Indeterminate.
For more information on how to create an event handler in Visual Basic, see Creating an Event Handle in the Visual Basic Code Editor. For more information on how to create an event handler in C #, see Creating a default event handler on a Windows Form Designer. For more information on how to create an event handler in C , see
Event processing in Visual C
. Create a similar event handler for Menuitem2. Enter the following code for the event handler: 'Visual Basic
Protected sub unchecked_onclick (Byval E AS System.EventArgs) if ContextMenu1.SourceControl Is Radiobutton1 Then
Radiobutton1.checked = false
Elseif ContextMenu1.SourceControl is Checkbox1 Then
Checkbox1.checked = false
END IF
End Sub
// C #
Protected void unchecked_onclick (System.Object Sender, System.EventArgs E)
{
IF (ContextMenu1.SourceControl == Radiobutton1)
Radiobutton1.checked = false;
Else IF (ContextMenu1.SourceControl == Checkbox1)
Checkbox1.checked = false;
}
// C
Private:
System :: void unchecked_onclick (System :: Object * Sender,
System :: Eventargs * e)
{
IF (ContextMenu1-> SourceControl == Radiobutton1)
Radiobutton1-> checked = false;
Else IF (ContextMenu1-> SourceControl == Checkbox1)
Checkbox1-> checked = false;
} Create a similar event handler for Menuitem3. Enter the following code for the event handler to ensure that the event is named Indeterminate_Onclick: 'Visual Basic
Protected Sub Indeterminate_onclick (Byval Sender As System.Object, ByVal E AS System.EventArgs)
IF ContextMenu1.SourceControl Is Checkbox1 Then
Checkbox1.checkstate = system.windows.forms.checkstate.indeeeeeeeeeeterminate
END IF
End Sub
// C #
Protected Void Indeterminate_onclick (System.Object Sender, System.EventArgs E)
{
IF (ContextMenu1.SourceControl == Checkbox1)
Checkbox1.checkstate = system.windows.Forms.checkstate.indeeeeterminate;
}
// C
Private:
System :: void Indeterminate_Onclick (System :: Object * Sender,
System :: Eventargs * e)
{
IF (ContextMenu1-> SourceControl == Checkbox1)
Checkbox1-> checkstate =
System :: Windows :: Forms :: Checkstate :: Indeterminate
}
Test application
At this point, run the application and observe the behavior of the application when dynamically adding and removing menu items in the context menu. Debug the app and press F5 to run it. For more information on debugging, see
Debug basics
. Click the Radiobutton control to select it, then right-click the Radiobutton display context menu. Note that there are two menu items that set the Radiobutton control to Checked or unchecked. Click the Checkbox control to select it, then right-click the Checkbox display context menu. Note that there are three menu items that set the checkbox control to Checked, Unchecked or Indeterminate.
See