C # + winform: Right-click the custom copy and paste shortcut menu. Multiple widgets share one shortcut menu.

Source: Internet
Author: User

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 ;

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.