Home > Backend Development > C#.Net Tutorial > How to implement the sample code of prohibiting pasting in TextBox using custom controls in C#

How to implement the sample code of prohibiting pasting in TextBox using custom controls in C#

黄舟
Release: 2017-06-18 10:32:34
Original
2430 people have browsed it

This article mainly introduces the C# custom control method to implement TextBox prohibition of pasting, and analyzes the creation and use of C# custom controls and TextBox prohibition based on specific examples. For pasting implementation skills, friends who need it can refer to

. This article describes the method of using C# custom controls to prohibit pasting in TextBox. Share it with everyone for your reference, the details are as follows:

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

New->Project->Windows Control Library: Create a new class , Inherits 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("继承自TextBox,但加入了禁止粘贴功能")]
  public partial class UC_TextBox_NoPaste : TextBox
  {
    public UC_TextBox_NoPaste()
    {
      InitializeComponent();
    }
    //重写基本类的WndProc()
    protected override void WndProc(ref Message m)
    {
      if (m.Msg == 0x0302) //0x0302是粘贴消息
      {
        m.Result = IntPtr.Zero; //拦截此消息
        return;
      }
      base.WndProc(ref m); //若此消息不是粘贴消息,则交给其基类去处理
    }
  }
}
Copy after login

Compile this source code, a .dll file will be generated. If it is in other projects To use this control, just add it to the tab first, and then drag it out into the interface.

Give it a try, it inherits all the features of TextBox and adds a paste prohibition function.

The same method can also be used to customize the controls you like, such as controls that can only input numbers.

The above is the detailed content of How to implement the sample code of prohibiting pasting in TextBox using custom controls in C#. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template