In this article I will explain how to customize default ContextMenu of a TextBox control in Windows Forms application using C#
Introduction
In previous article I explained How to create Context Menu in Windows Forms application using C#. In this article I will explain how to customize 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
Step 1:
Create a new Windows Forms application and drag a TextBox and a ContextMenuStrip control on the form. Set MultiLine property of TextBox to true and ContextMenuStrip property to contextMenuStrip1 to override default ContextMenu (see below figure) of the TextBox
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 font, text color and background color of the TextBox. contextMenuStrip1 should look like as in the first figure above. You can refer my previous article for adding context menu. Set Name property of these menu items same as their Text. Like for menu item “Undo” set its Name to “Undo”
Step 3:
Add following code in Opening event of contextMenuStrip1 to disable ToolStripMenuItems based on certain conditions (see comments)
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 following code in 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 ShortcutKeys property
ToolStripMenuItem | ShortKeys property |
Undo |
Ctrl+Z |
Cut |
Ctrl+X |
Copy |
Ctrl+C |
Paste |
Ctrl+V |
Select All |
Ctrl+A |
Set ShowShortcutKeys property of ToolStripMenuItems to false to hide shortcut keys from the menu items
Filed under: .Net, WinForm
