This article mainly describes the C # custom control implementation of the TextBox prohibit Paste method, combined with specific examples of the creation of C # Custom controls, the use of the method and the implementation of the TextBox prohibit paste, the need for friends can refer to the next
This example describes a C # custom control that implements a TextBox-forbidden paste method. Share to everyone for your reference, as follows:
Development environment: Visual Studio. NET 2005 + Windows XP SP2 Professional
New Project->windows Control Library: Create a new class that inherits from the TextBox class, with the following source code:
Using system;using system.collections.generic;using system.componentmodel;using system.drawing;using System.Data; Using system.text;using system.windows.forms;namespace textbox_nopaste{ [Description ("inherited from TextBox, but added no paste feature") ] public Partial class Uc_textbox_nopaste:textbox {public uc_textbox_nopaste () { InitializeComponent (); } Overriding the base class of WndProc () protected override void WndProc (ref message m) { if (m.msg = = 0x0302)//0x0302 is a paste message { m.result = IntPtr.Zero;//intercept this message return; } Base. WndProc (ref m); If the message is not a paste message, it is given its base class to process}}}
Compiling this source code generates a. dll file, and if the control is to be used in other projects, simply add it to the tab and drag it out to the interface.
Try it, inherit all the features of the textbox, add the Prohibit paste function.
The same approach also allows you to customize your favorite controls, such as those that can only enter numbers.