Example of clipboard and contextmenustrip in C #

Source: Internet
Author: User

Today, I suddenly remembered how to copy, cut, and paste a text, and added a shortcut to these features by right-clicking. Therefore, I wrote a simple small application with my own vs2008 to familiarize myself with the use of clipboard and shortcut menus in C.

First, we can easily find that the clipboard has three functions: Copy, paste, and cut.

Copy is to copy the selected content to the memory. paste the data in the memory to the selected region or container. For example, in the region textarea, the C # language is usually RichTextBox; you can copy the selected content to the memory and clear the selected items.

The implementation of the three functions can be roughly defined as follows:

Copy: Select + Store

Paste: select and retrieve

Cut: Select + storage + clear

Next, we can consider the design of the shortcut menu, which is a pop-up menu. Right-click the menu to adjust it. In C #, contextmenustrip appears to be called popedmenu in Java.

Finally, combine the two to create a shortcut menu that contains the copy, paste, and cut functions. According to the general software design, when there is no content in the clipboard, the "Paste" item should be gray, the logo is not available.

Use C # To implement the above content and write a simpleProgram:

At this time, there is nothing on the clipboard, so "Paste" is not available.

If you copy, paste, or cut and paste the content, the paste function is activated:

Some programsCodeIf not, please correct them.

///   <Summary>
/// Clipboard and shortcut menu examples
///   </Summary>
Public   Partial   Class Sampleclipboard: Form
{
///   <Summary>
/// Constructor
///   </Summary>
Public Sampleclipboard ()
{
Initializecomponent ();
}

///   <Summary>
/// Copy a piece of text from the editing area to the clipboard.
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private   Void Tsmicopy_click ( Object Sender, eventargs E)
{
Clipboard. setdataobject (RichTextBox. selectedtext );
}

///   <Summary>
/// Paste a piece of text to the selected area
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private   Void Tsmipaste_click ( Object Sender, eventargs E)
{
Idataobject data;
Data = Clipboard. getdataobject ();
// If the data is text, the specified RichTextBox text is used.
If (Data. getdatapresent (dataformats. Text ))
{
RichTextBox. selectedtext = Data. getdata (dataformats. Text). tostring ();
}
}

///   <Summary>
/// Cut the selected content to the clipboard
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private   Void Tsmicut_click ( Object Sender, eventargs E)
{
Clipboard. setdataobject (RichTextBox. selectedtext );
RichTextBox. selectedtext =   "" ;
}

///   <Summary>
/// How to set the status when the shortcut menu is opened
///   <Para> If the content in the clipboard is empty, the paste option is invalid. </Para>
///   </Summary>
///   <Param name = "sender"> </param>
///   <Param name = "E"> </param>
Private   Void Contextmenustrip_opening ( Object Sender, canceleventargs E)
{
If ( ! Clipboard. containsdata (dataformats. Text ))
{
Tsmipaste. Enabled =   False ;
}
Else
{
Tsmipaste. Enabled =   True ;
}
}
}

 

 

Finally, there is another noteworthy problem: to display the shortcut menu, a container must be used as the framework. That is to say, a container must be able to pop up the menu, the Object Name of the contextmenustrip class must be filled in the contextmenustrip column.

For example, in RichTextBox, The contextmenustrip attribute selects a shortcut menu:

 

For more content in the shortcut menu, right-click a control of contextmenustrip and select an edit item to edit more attributes. For C # development, it is very important to refer to msdn.

Related Article

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.