2013-04-05 81 views
-1

开发Windows应用程序我可以轻松地使用按键事件在文本框中输入唯一的数字。但我仍然能够在文本框中粘贴alpha贝塞尔我怎么能限制在文本框中粘贴阿尔贝茨如何防止在文本框上粘贴Alpha版窗口

+0

展示你的工作.. – 2013-04-05 12:31:27

+0

基本上一样[这个问题](http://stackoverflow.com/questions/3757913/prevent-numbers-from-being-pasted-in-textbox-in -net - 窗口形式?RQ = 1) – 2013-04-05 12:34:45

回答

1

我们使用TextChanged事件。

private void textBox1_TextChanged(object sender, EventArgs e) 
     { 
      var rgx = new Regex(@"\D"); 
      textBox1.Text = rgx.Replace(textBox1.Text, ""); 
     } 
1

使用KeyPress事件,

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (!Char.IsDigit(e.KeyChar)) 
      { 
       e.Handled = true; 
      } 
      else 
      { 
       e.Handled = false; 
      } 
     } 
0

你不明确说明你正在使用的UI技术,但如果它的WinForms,你可以尝试使用MaskedTextBox中使用相应的掩码属性设置。

MSDN MaskedTextBox Mask Property

相关问题