---
description: >-
  Adds a built-in ContextMenuStrip with a “Toggle” menu item for quick
  right-click actions on the SiticoneToggleButton.
---

# Context Menu Integration

## **Overview**

**Context Menu Integration** provides users with a secondary way to interact with the `SiticoneToggleButton` by right-clicking the control. When the user right-clicks, a context menu appears with an item that allows toggling the state (from “Turn On” to “Turn Off” or vice versa). This menu can be customized by adding additional items, and its font is set via the `ContextMenuFont` property.

***

### **Key API Members**

Below is a table summarizing the primary elements and properties that control context menu behavior:

| **Property / Feature** | **Type**            | **Description**                                                                         | **Example**                                                               |
| ---------------------- | ------------------- | --------------------------------------------------------------------------------------- | ------------------------------------------------------------------------- |
| **ContextMenuStrip**   | `ContextMenuStrip`  | Internally created for the toggle button.                                               | `// Accessed via myToggle.ContextMenuStrip`                               |
| **ContextMenuFont**    | `Font`              | Sets the **font** used by the context menu.                                             | `myToggle.ContextMenuFont = new Font("Segoe UI", 12f);`                   |
| **ToggleMenuItem**     | `ToolStripMenuItem` | Built-in context menu item labeled “Toggle” or shows the toggle state text dynamically. | `// Accessed internally via myToggle, updated automatically on toggling.` |

***

### **Key Points to Note**

1. **Automatic Label Updates**
   * The menu item text reflects the current state, e.g., if the toggle is Off, it might display “Turn On XXX,” where XXX is your `ToggledText` or “On.”
   * The label switches appropriately when the control’s state changes.
2. **Read-Only Considerations**
   * If `IsReadonly` is `true`, selecting the context menu’s “Toggle” item triggers a **shake** or **beep** if enabled, but **does not** change the state.
3. **Adding More Menu Items**
   * The `ContextMenuStrip` is accessible for developers to insert additional menu items. For instance, you could add “Settings…” or “Help…” to the same context menu.
4. **Customization via `ContextMenuFont`**
   * Change the context menu’s font family or size to match your application’s style or to highlight the toggle action more prominently.

***

### **Usage Example**

```csharp
// Assume you have a SiticoneToggleButton named myToggle

// Customize the context menu font
myToggle.ContextMenuFont = new Font("Consolas", 10f);

// Optionally, add more items to the context menu
ContextMenuStrip cms = myToggle.ContextMenuStrip;
ToolStripMenuItem extraItem = new ToolStripMenuItem("Additional Option");
extraItem.Click += (sender, e) => 
{
    MessageBox.Show("Extra menu item clicked!");
};
cms.Items.Add(extraItem);

// Now your toggle has a right-click menu with the built-in "Toggle" item plus "Additional Option."
```

> **Tip:** The built-in “Toggle” item automatically updates its label between “Turn On \[ToggledText]” and “Turn Off \[UntoggledText]” based on the control’s current state.

***

### **Best Practices to Create Beautiful UI and Apps**

| **Practice**                                                                | **Reason**                                                                                               |
| --------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- |
| Keep the **context menu** concise and relevant.                             | Users expect quick actions here; avoid cluttering it with too many items.                                |
| Match the **context menu font** with your application’s general font style. | Maintains design consistency and a professional look.                                                    |
| Provide **helpful item labels** if adding custom menu items.                | Clarity is key. Descriptive labels guide users without requiring them to guess the menu item’s function. |
| If using **read-only mode**, ensure your context menu clarifies the lock.   | Minimizes user confusion if toggling is not permitted.                                                   |

***

### **Common Pitfalls and Design Considerations**

| **Pitfall**                                                                 | **Mitigation / Recommendation**                                                           |
| --------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
| Overcrowding the **context menu**.                                          | Add only essential items; too many can confuse users.                                     |
| Inconsistent **context menu font** size or family.                          | Align with your application’s overall UI theme for a cohesive look.                       |
| Not realizing **IsReadonly** blocks context menu toggling.                  | Provide feedback (e.g., beep or shake) or a disabled menu item if toggling is disallowed. |
| Forgetting to **update or remove** the default “Toggle” item if not needed. | You can modify or remove it by adjusting the `ContextMenuStrip` items collection.         |

***

### **Review and Summary**

* **What You Learned**:\
  How `SiticoneToggleButton` includes a built-in context menu with an automatic toggle option, and how you can customize it via `ContextMenuFont` and additional `ToolStripMenuItem`s.
* **Why It’s Important**:\
  A right-click context menu can be a handy shortcut for power users or when screen space is limited, offering an alternative way to toggle or access related features.
* **How to Move Forward**:\
  Integrate the context menu with your **Appearance & Text** preferences and any other custom items you need. Ensure the menu remains intuitive, concise, and consistent with the rest of your application’s UI.

By effectively leveraging **Context Menu Integration**, you provide users with an additional, discoverable pathway to manage your toggle’s state, further enhancing the user interface of your WinForms application.







