This article describes the method of C# custom controls that prohibit pasting TextBox by implementing textBox. Share it for your reference, as follows:
Development environment: Visual Studio .net 2005 + Windows XP sp2 professional
Create new->Project->Windows Control Library: Create a new class, inherit from TextBox class, the specific source code is as follows:
using System; using ; using ; using ; using ; using ; using ; namespace TextBox_NoPaste { [Description("Inherited from TextBox, but added the prohibited paste function")] public partial class UC_TextBox_NoPaste : TextBox { public UC_TextBox_NoPaste() { InitializeComponent(); } //Rewrite the WndProc() of the basic class protected override void WndProc(ref Message m) { if ( == 0x0302) //0x0302 is pasting the message { = ; //Intercept this message return; } (ref m); //If this message is not a pasted message, it will be handed over to its base class for processing } } }
Compile this source code and a .dll file will be generated. If you want to use this control in other projects, just add it to the tab first and then drag one into the interface to use it.
Try it, inheriting all the features of TextBox and adding the prohibited paste function.
In the same way, you can customize your favorite controls, such as controls that can only enter numbers.
For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of C# form operation skills》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming》
I hope this article will be helpful to everyone's C# programming.