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.

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:

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: