-1

我正在开发一个 Windows 应用程序,我很容易使用按键事件在文本框中输入唯一的数字。但我仍然可以在文本框中粘贴 alpha bates 我如何限制在文本框中粘贴 alpha bates

4

3 回答 3

1

我们使用 TextChanged 事件。

private void textBox1_TextChanged(object sender, EventArgs e)
        {
            var rgx = new Regex(@"\D");
            textBox1.Text = rgx.Replace(textBox1.Text, "");
        }
于 2013-04-05T12:49:07.127 回答
1

使用 KeyPress 事件,

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
        }
于 2013-04-05T12:53:36.870 回答
0

您没有明确说明您正在使用什么 UI 技术,但如果它是 WinForms,您可以尝试使用具有适当掩码属性设置的 MaskedTextBox。

MSDN MaskedTextBox 掩码属性

于 2013-04-05T12:57:42.480 回答