C#粘贴到文本框时检查剪贴板中的字符

C# check characters in clipboard when paste into textbox

有没有一些方法可以在粘贴到文本框 C#(Ctrl+V 和右键单击 -> 粘贴)之前仅检查剪贴板中的字符,而不使用 MaskedTextbox。

我怀疑在粘贴到 TextBox 之前是否需要检查,我建议订阅 KeyDownMouseClick 事件,并编写您自己的逻辑。

protected override void OnKeyDown(object sender, KeyEventArgs e)
{
     if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)
     {
           // Your logic to read clipboard content and check length.;
     }     
}

private void Form1_MouseClick(object sender, MouseEventArgs e)
{ 
     if (e.Button == System.Windows.Forms.MouseButtons.Right)
     {
          // You logic goes here.
     }
}

您可以从 MSDN 获得有关如何 read/write 剪贴板内容的帮助。

我~认为~你想要一个只能接受数字的文本框?

如果是,则通过 SetWindowLong() 在 TextBox 上设置 ES_NUMBER 样式:

public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
        this.Load += Form2_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        SetNumbersOnlyTextBox(this.textBox1);
    }

    public const int GWL_STYLE = (-16);
    public const int ES_NUMBER = 0x2000;

    [System.Runtime.InteropServices.DllImport("user32.dll", SetLastError = true)]
    static extern int GetWindowLong(IntPtr hWnd, int nIndex);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);

    public void SetNumbersOnlyTextBox(TextBox TB)
    {
        SetWindowLong(TB.Handle, GWL_STYLE, GetWindowLong(TB.Handle, GWL_STYLE) | ES_NUMBER);
    }

}

或者,您可以从 TextBox 继承并在 CreateParams() 中设置 ES_NUMBER:

public class IntegerTextBox : TextBox
{

    private const int ES_NUMBER = 0x2000;

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams cp = base.CreateParams;
            cp.Style = cp.Style | ES_NUMBER;
            return cp;
        }
    }

}

在文本框文本更改中添加规则以仅接受数字,例如:

        private string value;
             private void textBox1_TextChanged(object sender, EventArgs e)
                {
                    // at this moment value is still old 
                    var oldValue = value;  
                    if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "[^0-9]"))
                    {
                        MessageBox.Show("Please enter numbers only.");
                        textBox1.Text = oldvalue;
                    }
                    else{
                        value = ((TextBox)sender).Text;
                    }
                }

如果您真的只想允许只有数字的粘贴,那么继承自 TextBox 并捕获 WM_PASTE,在需要时抑制消息:

public class DigitsOnlyOnPasteTextBox : TextBox
{

    private const int WM_PASTE = 0x302;

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == WM_PASTE && Clipboard.ContainsText())
        {
            int i;
            string txt = Clipboard.GetText();
            foreach(char c in txt)
            {
                if (!char.IsNumber(c))
                {
                    return;// suppress default behavior
                }
            }
        }
        base.WndProc(ref m); // allow normal processing of the message
    }

}