# Context Menu Support

## Overview

The Context Menu Support feature of the SiticoneCardNumberBox control implements a built‑in ContextMenuStrip that appears on right‑click, providing users with common text editing operations. The menu includes items for Copy, Cut, Paste, Delete, and Select All. It is designed to be automatically enabled (unless in read‑only mode) and is integrated directly into the control, so developers can rely on it without having to implement additional context menu logic.

***

### Property and Component Details

The table below summarizes the key aspects of the context menu functionality as implemented in the code:

<table><thead><tr><th width="211">Item</th><th>Description</th><th>Default Value / Notes</th></tr></thead><tbody><tr><td>ContextMenuStrip</td><td>The internal context menu used by the control.</td><td>Initialized in the InitializeContextMenu method.</td></tr><tr><td>Copy Menu Item</td><td>Provides the option to copy the selected text to the clipboard.</td><td>Enabled only when text is selected and control is not read‑only.</td></tr><tr><td>Cut Menu Item</td><td>Provides the option to cut the selected text to the clipboard.</td><td>Enabled only when text is selected and control is not read‑only.</td></tr><tr><td>Paste Menu Item</td><td>Provides the option to paste text from the clipboard.</td><td>Enabled only when Clipboard contains text and control is not read‑only.</td></tr><tr><td>Delete Menu Item</td><td>Provides the option to delete the selected text.</td><td>Enabled only when text is selected and control is not read‑only.</td></tr><tr><td>Select All Menu Item</td><td>Provides the option to select all text within the control.</td><td>Always available if the control contains text and is not read‑only.</td></tr><tr><td>Right‑click Activation</td><td>Activates the context menu when the user clicks the right mouse button on the control.</td><td>Implemented in the OnMouseDown event handler for right‑clicks.</td></tr></tbody></table>

***

### Code Examples

#### Example 1: Basic Integration of the Context Menu

Below is an example showing how the context menu is automatically integrated and used within the SiticoneCardNumberBox control.

```csharp
// Create an instance of the SiticoneCardNumberBox control
SiticoneCardNumberBox cardNumberBox = new SiticoneCardNumberBox();

// Set an initial text (optional)
cardNumberBox.Text = "4111 1111 1111 1111";

// Add the control to your form
this.Controls.Add(cardNumberBox);
cardNumberBox.Location = new Point(20, 20);
cardNumberBox.Size = new Size(350, 50);
```

When the user right‑clicks on the control, the internal ContextMenuStrip displays options for Copy, Cut, Paste, Delete, and Select All.

#### Example 2: Customizing or Disabling the Context Menu

If you need to modify or remove the built‑in context menu, you can override it as shown in the example below.

```csharp
// Create an instance of the SiticoneCardNumberBox control
SiticoneCardNumberBox cardNumberBox = new SiticoneCardNumberBox();

// Optionally disable the context menu by assigning an empty ContextMenuStrip
cardNumberBox.ContextMenuStrip = new ContextMenuStrip();

// Alternatively, customize the context menu by adding custom menu items
ContextMenuStrip customMenu = new ContextMenuStrip();
ToolStripMenuItem customItem = new ToolStripMenuItem("Custom Action");
customItem.Click += (s, e) => { MessageBox.Show("Custom action triggered!"); };
customMenu.Items.Add(customItem);
cardNumberBox.ContextMenuStrip = customMenu;

// Add the control to your form
this.Controls.Add(cardNumberBox);
cardNumberBox.Location = new Point(20, 100);
cardNumberBox.Size = new Size(350, 50);
```

***

### Key Points

The table below summarizes the key aspects of the Context Menu Support feature:

<table><thead><tr><th width="218">Topic</th><th>Description</th></tr></thead><tbody><tr><td>Built‑in Operations</td><td>The control includes Copy, Cut, Paste, Delete, and Select All commands.</td></tr><tr><td>Right‑click Activation</td><td>The context menu is automatically displayed on a right‑mouse click.</td></tr><tr><td>Conditional Availability</td><td>Menu items are enabled/disabled based on selection state, clipboard content, and read‑only mode.</td></tr></tbody></table>

***

### Best Practices

The table below outlines best practices when using the context menu functionality:

| Best Practice                                              | Explanation                                                                                                |
| ---------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------- |
| Leverage the built‑in context menu to simplify development | Reduces the need for custom code by utilizing the default implementation provided by the control.          |
| Ensure proper state management for menu items              | The control dynamically enables/disables menu items based on the current text selection and control state. |
| Customize only when necessary                              | Override the default context menu only if your application requires additional or specialized commands.    |

***

### Common Pitfalls

The table below lists common pitfalls and how to avoid them:

<table><thead><tr><th width="371">Pitfall</th><th>How to Avoid It</th></tr></thead><tbody><tr><td>Context menu appearing in read‑only mode unexpectedly</td><td>Verify that the control checks for IsReadOnly before enabling editing commands.</td></tr><tr><td>Custom modifications interfering with default behavior</td><td>Ensure that any custom ContextMenuStrip still includes the necessary standard operations if required.</td></tr><tr><td>Clipboard-dependent commands not working as expected</td><td>Check that the clipboard content is valid and that the control properly updates its state before showing the menu.</td></tr></tbody></table>

***

### Usage Scenarios

The table below describes several scenarios where context menu support is beneficial:

<table><thead><tr><th width="279">Scenario</th><th>Description</th></tr></thead><tbody><tr><td>Data entry forms</td><td>Users can quickly perform common editing actions such as copy, cut, and paste without additional UI elements.</td></tr><tr><td>Payment processing interfaces</td><td>Provides quick text manipulation commands for credit card inputs in checkout forms.</td></tr><tr><td>Custom text editors</td><td>Offers an integrated way for users to manage text selection and modification directly from the control.</td></tr></tbody></table>

***

### Real Life Usage Scenarios

<table><thead><tr><th width="284">Scenario</th><th>Example</th></tr></thead><tbody><tr><td>Banking application input fields</td><td>Users right‑click to copy their masked account or card numbers for later reference.</td></tr><tr><td>E‑commerce checkout pages</td><td>Users can easily cut or paste credit card numbers using the integrated context menu, streamlining the checkout process.</td></tr><tr><td>Administrative dashboards</td><td>Quick access to text editing commands in a form with multiple input fields improves efficiency and usability.</td></tr></tbody></table>

***

### Troubleshooting Tips

The table below provides troubleshooting advice for common issues related to the context menu:

| Issue                                          | Potential Cause                                                   | Recommended Action                                                                           |
| ---------------------------------------------- | ----------------------------------------------------------------- | -------------------------------------------------------------------------------------------- |
| Context menu does not appear on right‑click    | The OnMouseDown event might not be correctly triggering the menu. | Verify that the control is not in read‑only mode and that right‑click events are handled.    |
| Menu items remain disabled unexpectedly        | No text is selected or the clipboard does not contain valid text. | Check that the text selection state is correct and that clipboard data is available.         |
| Custom context menu not showing expected items | Overriding the default ContextMenuStrip incorrectly.              | Ensure that custom modifications include all desired items and that they are added properly. |

***

### Review

The table below summarizes the review of the Context Menu Support feature:

<table><thead><tr><th width="135">Aspect</th><th>Review Notes</th></tr></thead><tbody><tr><td>Usability</td><td>The built‑in context menu simplifies common text editing operations for end‑users.</td></tr><tr><td>Integration</td><td>Easy to integrate into any form without extra code, while still allowing customization if needed.</td></tr><tr><td>Flexibility</td><td>Developers can disable, modify, or extend the default context menu to suit specific application requirements.</td></tr></tbody></table>

***

### Summary

The Context Menu Support feature of the SiticoneCardNumberBox control enhances user interaction by providing a built‑in right‑click menu with standard editing commands. With automatic state management and the ability to customize or override the default menu, developers can ensure that users have quick access to common text manipulation functions without additional development overhead.

***

### Additional Integration Example

Below is a complete integration example demonstrating how to utilize and customize the context menu support in a WinForms application:

```csharp
public partial class ContextMenuDemoForm : Form
{
    public ContextMenuDemoForm()
    {
        InitializeComponent();
        InitializeCardNumberBoxWithContextMenu();
    }

    private void InitializeCardNumberBoxWithContextMenu()
    {
        // Create and configure the SiticoneCardNumberBox control
        SiticoneCardNumberBox cardNumberBox = new SiticoneCardNumberBox
        {
            Location = new Point(20, 20),
            Size = new Size(350, 50),
            Text = "4111 1111 1111 1111"
        };

        // Optionally, customize the context menu
        ContextMenuStrip customMenu = new ContextMenuStrip();
        ToolStripMenuItem customCopy = new ToolStripMenuItem("Copy");
        customCopy.Click += (s, e) => { cardNumberBox.CopySelectedText(); };
        ToolStripMenuItem customCut = new ToolStripMenuItem("Cut");
        customCut.Click += (s, e) => { cardNumberBox.CutSelectedText(); };
        ToolStripMenuItem customPaste = new ToolStripMenuItem("Paste");
        customPaste.Click += (s, e) => { cardNumberBox.PasteClipboardText(); };
        ToolStripMenuItem customDelete = new ToolStripMenuItem("Delete");
        customDelete.Click += (s, e) => { cardNumberBox.DeleteSelectedText(); };
        ToolStripMenuItem customSelectAll = new ToolStripMenuItem("Select All");
        customSelectAll.Click += (s, e) => { cardNumberBox.SelectAll(); };

        customMenu.Items.Add(customCopy);
        customMenu.Items.Add(customCut);
        customMenu.Items.Add(customPaste);
        customMenu.Items.Add(customDelete);
        customMenu.Items.Add(new ToolStripSeparator());
        customMenu.Items.Add(customSelectAll);

        // Assign the custom context menu
        cardNumberBox.ContextMenuStrip = customMenu;

        // Add the control to the form
        this.Controls.Add(cardNumberBox);
    }
}
```

***

By following this documentation, developers can effectively leverage the built‑in Context Menu Support of the SiticoneCardNumberBox control to provide users with familiar text editing operations. This feature not only simplifies development but also improves user experience by offering quick access to essential editing commands.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs-siticoneframework.gitbook.io/home/net-framework-or-net-core-ui/special-purpose-controls/siticone-cardnumber/context-menu-support.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
