| Skype: | TextControlSupport | |
| Orders: | 877-462-4772 |

| Author: | TX Text Control Support Department |
| Language: | Visual Basic |
| Version: | 1.0 |
| Released: | August 04, 2004 |
| Last modified: | January 11, 2008 |
| Requirements: | TX Text Control .NET with Visual Basic .NET |
| Download code: | tx_context_menu.zip |

A context menu is a very helpful feature to provide end users with information or menu options at a specific text position.
For example, a context menu for adding additional field information could be shown, but only if the user clicks on a field.
This week, we would like to show you how to create a very simple context menu.
When an end user right clicks, a menu will be displayed that offer the actions Cut, Copy and Paste. Only the possible menu entries will be shown.
For example, a Copy menu will be only shown, if a part of the text has been selected by the user.
Private Sub TextControl1_MouseUp(ByVal sender As Object, ByVal_ e As System.Windows.Forms.MouseEventArgs) Handles TextControl1.MouseUp Dim pos As System.Drawing.Point If e.Button = MouseButtons.Right Then MenuItem3.Visible = TextControl1.CanCopy MenuItem4.Visible = TextControl1.CanPaste If TextControl1.Selection.Length = 0 Then MenuItem2.Visible = False Else MenuItem2.Visible = True End If pos.X = e.X pos.Y = e.Y ContextMenu1.Show(TextControl1, pos) End If End Sub
We use the TX Text Control MouseUp event to intercept the end user's right mouse click. The Visible property of the different menu entries can be used to hide functionality that is currently not possible. Finally, the context menu will be shown at the X and Y position returned by the MouseUp event.
At least TX Text Control .NET trial version and Visual Studio .NET is required to test this sample.