C Custom Control Realizes the Method of TextBox Prohibiting Paste

  • 2021-12-13 16:47:31
  • OfStack

This article describes the example of C # custom control to implement TextBox paste prohibition method. Share it for your reference, as follows:

Development Environment: Visual Studio. net 2005 + Windows XP sp2 professional

New- > Project- > Windows Control Library: 1 new class, inherited from the TextBox class, the specific source code is as follows:


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(" Inherit from TextBox, However, the function of prohibiting paste is added ")]
  public partial class UC_TextBox_NoPaste : TextBox
  {
    public UC_TextBox_NoPaste()
    {
      InitializeComponent();
    }
    // Override the basic class's WndProc()
    protected override void WndProc(ref Message m)
    {
      if (m.Msg == 0x0302) //0x0302 Is to paste messages 
      {
        m.Result = IntPtr.Zero; // Intercept this message 
        return;
      }
      base.WndProc(ref m); // If this message is not a paste message, it is handed over to its base class for processing 
    }
  }
}

Compile this source code, you will generate a. dll file, if you want to use this control in other projects, just add it to the tab first, and then drag one out to the interface to use it.

Try it. It inherits all the features of TextBox and adds the function of prohibiting paste.

The same method can also customize your favorite controls, such as controls that can only enter numbers.

For more readers interested in C # related content, please check out the topics on this site: "C # Common Control Usage Tutorial", "C # Form Operation Tips Summary", "C # Data Structure and Algorithm Tutorial", "C # Object-Oriented Programming Introduction Tutorial" and "C # Programming Thread Use Tips Summary"

I hope this article is helpful to everyone's C # programming.


Related articles: