Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#粘贴到文本框时检查剪贴板中的字符_C#_Textbox_Clipboard_Paste - Fatal编程技术网

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

C#粘贴到文本框时检查剪贴板中的字符,c#,textbox,clipboard,paste,C#,Textbox,Clipboard,Paste,在粘贴到文本框C#(Ctrl+V和右键单击->粘贴)之前,是否有一些方法可以检查剪贴板中的字符,而不是使用MarkedTextbox。我怀疑在粘贴到文本框之前是否有任何方法可以检查,我建议订阅下键和鼠标单击事件,写下你自己的逻辑 protected override void OnKeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.V && e.Modifiers == Keys.Control)

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

我怀疑在粘贴到文本框之前是否有任何方法可以检查,我建议订阅
下键
鼠标单击
事件,写下你自己的逻辑

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

您可以从获取有关如何读取/写入剪贴板内容的帮助

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

如果是,则通过SetWindowLong()在文本框上设置ES_编号样式:

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


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

        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_粘贴,在需要时抑制消息:

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
    }

}

另一种方法是签入文本框文本更改事件。是否关心拖放?@DCODE:如果我从键盘输入1位数字,则我会粘贴一些字符而不是数字。如何防止在文本更改事件中不粘贴这些字符检查新值。如果条件失败,则恢复旧值。私有字符串值;私有void textBox1_TextChanged(object sender,EventArgs e){//此时值仍然是old var oldValue=value;value=((TextBox)sender)。Text;//text1.Text//这里有oldValue和new value}您真的无法通过这种方式停止所有剪贴板/拖放交互。。。聆听值更改听起来更合适。@AlexeiLevenkov感谢您的建议,但是
TextChange
事件只有在焦点更改后才会触发,问题是在粘贴之前执行操作。当我粘贴字符而不是数字时,如何隐藏消息?使用这种方法,您不能。首先,我输入1个数字。然后,我粘贴字符而不是数字。光标移动到文本框的第一个位置,如何修复@我没有完全明白你的意思,你能详细解释一下吗。。要么你可以做点像。。this.focus()首先,我在文本框中输入'12'。然后我在'12'之后粘贴'ab'。现在,光标移到“1”字符之前的位置@DCODETry this.Select(this.Text.Length,0);或textBox1.选择(textBox1.Text.Length,0);或txtBox.Focus();txtBox.SelectionStart=txtBox.Text.Length;或txtBox.Focus();txtBox.CaretIndex=txtBox.Text.Length;
        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;
                    }
                }
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
    }

}