How to Customize Default ContextMenu of a TextBox Control in Windows Forms Application Using C#

In this article I will explain how to customize a default ContextMenu of a TextBox control in a Windows Forms application using C#.

Introduction

In a previous article I explained How to create Context Menu in Windows Forms application using C#. In this article I will explain how to customize the default ContextMenu of a TextBox control and add custom menu items for changing Font, ForeColor and BackColor. FontDialog and ColorDialog controls are used to select font and colors of the TextBox.

FontDialog-and-ColorDialog-controls.jpg

Step 1

Create a new Windows Forms application and drag a TextBox and a ContextMenuStrip control onto the form. Set the MultiLine property of the TextBox to true and the ContextMenuStrip property to contextMenuStrip1 to override the default ContextMenu (as in the following figure) of the TextBox. See:

contextMenuStrip1-to-override-default-ContextMenu.jpg

Step 2

Add six menu items in the ContextMenuStrip as in default ContextMenuStrip of the TextBox control, Undo, Cut, Copy, Paste, Delete, and "Select All" with three Separators after Undo, Delete and "Select All" menu items. Add three more menu items in the ContextMenuStrip Font, Forecolor and Backcolor for changing the font, text color and background color of the TextBox. contextMenuStrip1 should look as in the first figure above. You can refer to my previous article for adding a context menu.

Set the Name property of these menu items the same as their Text. Like for the menu item "Undo" set its Name to "Undo".

Step 3

Add the following code in the Opening event of contextMenuStrip1 to disable ToolStripMenuItems based on certain conditions (see comments). See:

private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)

{

    // Disable Undo if CanUndo property returns false

    if (textBox1.CanUndo)

    {

        contextMenuStrip1.Items["Undo"].Enabled = true;

    }

    else

    {

        contextMenuStrip1.Items["Undo"].Enabled = false;

    }

 

    // Disable Cut, Copy and Delete if any text is not selected in TextBox

    if (textBox1.SelectedText.Length == 0)

    {

        contextMenuStrip1.Items["Cut"].Enabled = false;

        contextMenuStrip1.Items["Copy"].Enabled = false;

        contextMenuStrip1.Items["Delete"].Enabled = false;

    }

    else

    {

        contextMenuStrip1.Items["Cut"].Enabled = true;

        contextMenuStrip1.Items["Copy"].Enabled = true;

        contextMenuStrip1.Items["Delete"].Enabled = true;

    }

 

    // Disable Paste if Clipboard does not contains text

    if (Clipboard.ContainsText())

    {

        contextMenuStrip1.Items["Paste"].Enabled = true;

    }

    else

    {

        contextMenuStrip1.Items["Paste"].Enabled = false;

    }

 

    // Disable Select All if TextBox is blank

    if (textBox1.Text.Length == 0)

    {

        contextMenuStrip1.Items["SelectAll"].Enabled = false;

    }

    else

    {

        contextMenuStrip1.Items["SelectAll"].Enabled = true;

    }

}

Step 4

Add the following code in the click event of ToolStripMenuItems:

private void Undo_Click(object sender, EventArgs e)

{

    textBox1.Undo();

}

 

private void Cut_Click(object sender, EventArgs e)

{

    textBox1.Cut();

}

 

private void Copy_Click(object sender, EventArgs e)

{

    textBox1.Copy();

}

 

private void Paste_Click(object sender, EventArgs e)

{

    textBox1.Paste();

}

 

private void Delete_Click(object sender, EventArgs e)

{

    int SelectionIndex = textBox1.SelectionStart;

    int SelectionCount = textBox1.SelectionLength;

    textBox1.Text = textBox1.Text.Remove(SelectionIndex, SelectionCount);

    textBox1.SelectionStart = SelectionIndex;

}

 

private void SelectAll_Click(object sender, EventArgs e)

{

    textBox1.SelectAll();

}

 

private void Font_Click(object sender, EventArgs e)

{

    FontDialog fontDialog = new FontDialog();

    if (fontDialog.ShowDialog() == DialogResult.OK)

    {

        textBox1.Font = fontDialog.Font;

    }

}

 

private void Forecolor_Click(object sender, EventArgs e)

{

    ColorDialog colorDialog = new ColorDialog();

    if (colorDialog.ShowDialog() == DialogResult.OK)

    {

        textBox1.ForeColor = colorDialog.Color;

    }

}

 

private void Backcolor_Click(object sender, EventArgs e)

{

    ColorDialog colorDialog = new ColorDialog();

    if (colorDialog.ShowDialog() == DialogResult.OK)

    {

        textBox1.BackColor = colorDialog.Color;

    }

}

Step 5

Add shortcuts to ToolStripMenuItems using the ShortcutKeys property:

ToolStripMenuItem ShortKeys property
Undo Ctrl+Z
Cut Ctrl+X
Copy Ctrl+C
Paste Ctrl+V
Select All Ctrl+A


Set the ShowShortcutKeys property of ToolStripMenuItems to false to hide shortcut keys from the menu items.
 


Similar Articles