开发者

How do you disable or catch "paste" or "Control+V" event in an text box event handler in C# Winform?

Can you provide a code snippets f开发者_运维知识库or this?


Trapping the keystroke isn't enough, the user can also paste using the context menu. This requires trapping the operation at a lower level, you have to catch the WM_PASTE message and prevent it from reaching the native Windows control. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form. It does have one flaw, the Paste command isn't disabled on the context menu. No easy fix for that one, you'd have to replace it with your own.

using System;
using System.Windows.Forms;

class NopasteTextBox : TextBox {
    protected override void WndProc(ref Message m) {
        // Trap WM_PASTE:
        if (m.Msg == 0x302) return;
        base.WndProc(ref m);
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜