Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Walkthrough: Creating Dynamic Context Menus on Windows Forms

Visual Studio .NET 2003

To save time and the amount of code involved in creating applications, multiple controls can share a single context menu object. By having one "dynamic" context menu (or shortcut menu) that features only the menu items necessary for that control, you can reduce the overall number of context menus you need for the controls in your application. The following walkthrough shows you how to change menu items per control.

Creating the Application

In the steps below, you will create a Windows application that has a form with two controls on it. At run time, right-clicking each control (as long as it has focus, meaning it is selected) will result in the appropriate context menu being shown. The RadioButton control will have two items on its context menu, and the CheckBox control will have three.

To create a dynamic context menu on a Windows Form

  1. Create a new Windows application. For details, see Creating a Windows Application Project.
  2. Drag a CheckBox control and a RadioButton control to the form from the Toolbox.

    While any two (or more) controls could conceivably share a context menu, it makes sense to have controls with similar commands share, as this reduces the amount of dynamic showing and hiding that will be necessary.

  3. Double-click the ContextMenu component in the Toolbox to add it to the form. This will be the shared context menu.
  4. In the Properties window, set the ContextMenu property of both the CheckBox control and the RadioButton control to ContextMenu1 in Visual Basic or contextMenu1 in Visual C# or Visual C++.
  5. In the Properties window, set the ThreeState property of the CheckBox control to true.
    Note   You can also generate the menu item and setting of the ContextMenu and ThreeState properties entirely in code. For details, see Adding Context Menus to Windows Forms and Setting Properties for Controls, Documents, and Forms. For details about the ThreeState property, see Introduction to the Windows Forms CheckBox Control.
  6. From the designer, double-click the ContextMenu component to create a default handler for the component's Popup event. For details, see Creating Event Handlers on the Windows Forms Designer.
  7. Insert code in the event handler to do the following:
    • Add two menu items, one for the Checked state of the controls and one for the Unchecked state.
    • Test, with an If statement, whether the CheckBox control is the SourceControl on the form. Based on this, dynamically add a third menu item for the Indeterminate state of the control.

    The following example show how to use the Add method to set the Text property of the menu item as well as define the event handler associated with the menu item. For other Add methods with different signatures, see Menu.MenuItemCollection.Add Method.

    ' Visual Basic
    Protected Sub ContextMenu1_Popup(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContextMenu1.Popup
    ' Clear the contents of the context menu.
       ContextMenu1.MenuItems.Clear()
    ' Add a menu item for the Checked state.
       ContextMenu1.MenuItems.Add("Checked", New System.EventHandler(AddressOf Me.Checked_OnClick))
    ' Add a menu item for the 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 item for the Indeterminate state.
       If ContextMenu1.SourceControl Is CheckBox1 Then
         ContextMenu1.MenuItems.Add("Indeterminate", New System.EventHandler(AddressOf Me.Indeterminate_OnClick))
       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 state.
       contextMenu1.MenuItems.Add("Checked",new System.EventHandler(this.Checked_OnClick));
    // Add a menu item for 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("Indeterminate", new System.EventHandler(this.Indeterminate_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 state.
        contextMenu1->MenuItems->Add("Checked",
          new System::EventHandler(this, Checked_OnClick));
    
        // Add a menu item for 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("Indeterminate",
            new System::EventHandler(this, Indeterminate_OnClick));
        }
      }
    
  8. Create an event handler for MenuItem1. Add code similar to the following to test the form's SourceControl property and, based on that, set the Checked property of the RadioButton or CheckBox control:
    ' 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.CheckState = System.Windows.Forms.CheckState.Checked
       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.CheckState = System.Windows.Forms.CheckState.Checked;
    }
    
    // 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->CheckState = System::Windows::Forms::
            CheckState::Checked;
      }
    
    Note   This example uses the CheckState property to set the CheckBox control to Indeterminate in the Indeterminate_OnClick event handler.

    For details on creating event handlers in Visual Basic, see Creating Event Handlers in the Visual Basic Code Editor. For details on creating event handlers in C#, see Creating Default Event Handlers on the Windows Forms Designer. For details on creating event handlers in C++, see Event Handling in Visual C++.

  9. Create a similar event handler for MenuItem2. Enter code similar to the following for the event handler:
    ' Visual Basic
    Protected Sub Unchecked_OnClick(ByVal sender As System.Object, 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;
      }
    
  10. Create a similar event handler for MenuItem3. Enter code similar to the following for the event handler, being sure to name the event 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.Indeterminate
       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.Indeterminate;
    }
    
    // C++
    private:
      System::Void Indeterminate_OnClick(System::Object *  sender,
        System::EventArgs *  e)
      {
        if (contextMenu1->SourceControl == checkBox1)
          checkBox1->CheckState =
            System::Windows::Forms::CheckState::Indeterminate;
      }
    

To test the application

At this point, you will run the application, and observe the behavior as the menu items within the context menu are dynamically added and removed.

  1. Debug the application and press F5 to run it. For details about debugging, see Debugging Basics.
  2. Click the RadioButton control to select it, and then right-click the RadioButton to display the context menu.

    Note that there are two menu items, which will set the RadioButton control to either Checked or Unchecked.

  3. Click the CheckBox control to select it, and then right-click the CheckBox to display the context menu.

    Note that there are three menu items, which will set the CheckBox control to Checked, Unchecked, or Indeterminate.

See Also

Context Menus on Windows Forms | Copying Menu Items from Menus to Context Menus | ContextMenu Component (Windows Forms) | Deploying Applications and Components

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft