. Net standard controls and Custom Controls

Source: Internet
Author: User

Recently, we have discussed anti-copy of textbox in csdn.
The general conclusion is that the processcmdkey method of form can be rewritten.

The Code is as follows:

Protected override bool processcmdkey (ref message MSG, keys keydata)
{
 
If (keydata = (Keys. control | keys. c) | keydata = (Keys. control | keys. V ))
{
MessageBox. Show ("Do not paste, copy ");
Return true;
}
 
Else
Return base. processcmdkey (ref MSG, keydata );
// Return false;
 
}

The processcmdkey intercepts the form command keys and processes them.
This method determines whether the user has pressed Ctrl + C and CTRL + v.
If yes, a dialog box pops up and returns true. If true is returned, it indicates that I have processed the event and the program does not need to continue to process it.
Otherwise, the base. processcmdkey is called to continue processing the event.

However, I personally think this is a big problem.
First of all, this does not fundamentally address the anti-copy of Textbox, because when you right-click, you can still activate the standard contextmeun. You can easily copy the pop-up menu.

Second, processcmdkey is reloaded from, that is to say, the keys on all sub-controls using the form as the container will be processed by the disaster recovery method.
For example, I want to implement a form with textbox1, which cannot copy data on it. There is another textbox2 that allows you to paste and copy data. This method cannot be used, no matter which control you are using. The program will relentlessly pop up a MessageBox to tell you that it cannot be copied.

Therefore, in form, reloading processcmdkey not only has low practical value, but also makes people feel very bad...

Therefore, a better way is to develop custom controls. For example, a nocopytextbox can be developed in this application.
The control is inherited from textbox.

In this control, we reload the processcmdkey method and replace the default contextmenu of nocopytextbox, which can solve the appeal issue well.
In future development, we can directly use nocopytextbox if we encounter the same problem.
Use standard controls for textbox that do not require copy.

The nocopytextbox code is as follows:

 

Public class nocopytextbox: system. Windows. Forms. textbox
{
Contextmenu nocontext = new contextmenu ();

Public nocopytextbox ()
{
This. contextmenu = nocontext;
}

Private void initializecomponent ()
{
}

Protected override bool processcmdkey (ref message MSG, keys keydata)
{
 
If (keydata = (Keys. control | keys. c) | keydata = (Keys. control | keys. V ))
{
MessageBox. Show ("Do not paste, copy ");
Return true;
}
 
Else
Return base. processcmdkey (ref MSG, keydata );
// Return false;
 
}
}

This control replaces the default contextmeun with an empty contextmenu during the construction. In this way, right-click the control and then click nothing.

So what I want to say is that in actual development, we can create controls with different details on the basis of standard controls. This may be more difficult than directly using standard controls, but it is much easier to create controls from the header :)

Therefore, it is better for application programmers to have their own libraries.

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.