ProgramText Box or RichTextBox needs to be used for text input or display, for general users, may even press the shortcut key Ctrl + C copy, CTRL + V paste will not be used, as a developer, you must right-click a menu to display "copy" and "Paste ".
You can assign a context menu (contextmenustrip, also called right-click menu and shortcut menu) to several controls by setting the contextmenustrip attribute of these controls to the menu to be displayed.
In menu events, how do I determine which control is clicked? The answer is the sourcecontrol attribute of the context menu, Which is copied and pasted below.Code:
Private Void Copy toolstripmenuitem_click ( Object Sender, eventargs E)
{
String Selecttext = (RichTextBox) menusend. sourcecontrol). selectedtext;
If (Selecttext! = "" )
{
Clipboard. settext (selecttext );
}
}
Private Void Paste toolstripmenuitem_click ( Object Sender, eventargs E)
{
If (Clipboard. containstext ())
{
RichTextBox txtbox = (RichTextBox) menusend. sourcecontrol;
Int Index = txtbox. selectionstart; // Record the cursor position before pasting
String TEXT = txtbox. text;
// Delete selected text
TEXT = text. Remove (txtbox. selectionstart, txtbox. selectionlength );
// Insert clipboard content at the current cursor Input Point
TEXT = text. insert (txtbox. selectionstart, clipboard. gettext ());
Txtbox. Text = text;
// Reset the cursor position
Txtbox. selectionstart = index;
}
}
To make the right-click menu more intelligent, "copy" takes effect when no text is selected; when the clipboard has no content, "Paste" is invalid. The following code is added to its opened event.
Private Void Menusend_opened ( Object Sender, eventargs E)
{
// If no text is selected, the copy menu is disabled.
String Selecttext = (RichTextBox) menusend. sourcecontrol). selectedtext;
If (Selecttext! = "" )
Copy toolstripmenuitem. Enabled = True ;
Else
Copy toolstripmenuitem. Enabled = False ;
// When the clipboard has no text content, the paste menu is disabled.
If (Clipboard. containstext ())
{
Paste toolstripmenuitem. Enabled = True ;
}
Else
Paste toolstripmenuitem. Enabled = False ;
}