无法清除文本框数据

时间:2013-06-04 12:50:56

标签: c# wpf textbox

我无法使用以下代码删除文本框数据

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {            
        if(char.IsDigit(e.KeyChar)==false)
        {
            count++;
       }
        if (count == 1)
        {
            textBox1.Text = ("");
            count = 0;
        }
   }

尝试使用clear方法,我输入的字母也保留在文本框中,当我键入任何键时,它会被覆盖但我希望文本框第二次为空并且要删除的prev数据

4 个答案:

答案 0 :(得分:3)

你只需要说你已经处理了这个事件:

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
        if (char.IsDigit(e.KeyChar) == false)
        {
            count++;
        }
        if (count == 1)
        {
            textBox1.Text = ("");
            count = 0;
            e.Handled = true; // this bit fixes it
        }
    }

答案 1 :(得分:0)

使用textBox1.Text = "";textBox1.clear();

这将清除您的文本框。

答案 2 :(得分:0)

你做错了。你可以用Ctrl + V粘贴一堆字母。删除KeyDown事件并创建TextChanged事件。此代码应该完成您正在尝试的内容。请告诉我是否有更多细节,我会添加到我的答案。

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        foreach (char c in textBox1.Text)
        if (!char.IsDigit(c)) { textBox1.Clear(); break; }
    }

答案 3 :(得分:0)

将此添加到文本框按键事件中,您的问题将得到解决

e.handle = true;