注册 登录
编程论坛 C# 论坛

不允许在textBox里粘贴

小白到底白不 发布于 2024-04-07 11:17, 740 次点击
程序代码:
namespace 不允许在textBox里粘贴
{
    public partial class loginForm : Form
    {
        public loginForm()
        {
            InitializeComponent();
        }
        private Point offset;

        private void panel1_MouseDown(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;

            Point cur = this.PointToScreen(e.Location);
            offset = new Point(cur.X - this.Left, cur.Y - this.Top);
        }

        private void panel1_MouseMove(object sender, MouseEventArgs e)
        {
            if (MouseButtons.Left != e.Button) return;
            Point cur = MousePosition;
            this.Location = new Point(cur.X - offset.X, cur.Y - offset.Y);
        }

        private void loginForm_Load(object sender, EventArgs e)
        {

        }

        private void btlCloseButton_Click(object sender, EventArgs e)
        {
            System.Windows.Forms.Application.Exit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text!=""){  //要注意.Text,需要大写
               
            };

        }


      
    }


}


我设计一个窗口,就是想让textBox屏蔽粘贴功能,在别处找了一个代码,如下:
程序代码:
    class TextBoxx : TextBox
    {
        public const int WM_PASTE = 0x0302;//粘贴消息信息
        protected override void WndProc(ref Message m)//重写处理消息方法
        {
            if (m.Msg != WM_PASTE)//屏蔽粘贴消息信息
            {
                base.WndProc(ref m);//调用基类消息处理方法
            }
        }
    }

现在不知道怎么把这段代码融合进去,我那个textBox是设计时添加的,这个是运行时再产生
3 回复
#2
yiyanxiyin2024-04-07 17:00
将TextBoxx的namespace改成 不允许在textBox里粘贴, 也就是和你的loginForm在同一个名称空间中
然后定位到textBox1的定义处, 将它的类型改成TextBoxx
#3
小白到底白不2024-04-07 19:31
以下是引用yiyanxiyin在2024-4-7 17:00:59的发言:
然后定位到textBox1的定义处


这一句,不太明白,要修改Form1.Designer.cs,还是Form1.cs,请再详细点,谢谢!
#4
apull2024-04-07 22:23
  class TextBoxx : TextBox 重写了TextBox控件,
Form1.Designer.cs里的TextBox 改为TextBoxx
1