C# check characters in clipboard when paste into t

2019-09-02 14:15发布

问题:

Are there some ways to check charater in clipboard only digit before paste into textbox C# (Both Ctrl+V and right click -> Paste), which not using MarkedTextbox.

回答1:

I ~think~ you want a TextBox that can only accept digits?

If yes, then set the ES_NUMBER style on the TextBox via SetWindowLong():

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);
    }

}

Alternatively, you can Inherit from TextBox and set ES_NUMBER in CreateParams():

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;
        }
    }

}


回答2:

Add rule in textbox text change to accept number only like:

        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;
                    }
                }


回答3:

I doubt there are anyways to check before pasting to TextBox, I would suggest subscribing to KeyDown and MouseClick events, and writing your own logic.

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.
     }
}

You can get help from MSDN on how to read/write clipboard content.



回答4:

If you literally want to only allow pastes that have digits only, then Inherit from TextBox and trap WM_PASTE, suppressing the message when desired:

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
    }

}