2011-11-30 133 views

回答

14

您可以使用下面的代码片段:

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    if (!System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z ]")) 
    { 
     MessageBox.Show("This textbox accepts only alphabetical characters"); 
     textBox1.Text.Remove(textBox1.Text.Length - 1); 
    } 
} 
+1

似乎不能很好地工作。如果我添加一个字母,然后添加一个数字/空格/不管它会让我走,因为我满足正则表达式(以字母开头)。 –

+0

@favoretti现在的代码工作正常,但是如果我键入第一个字符的整数,并且当我按退格键删除时,会发生异常。代码适用于数字,它会在我键入数字时弹出消息。但是当我试图用退格清除时,它给了我错误StartIndex不能小于零,参数名称startIndex。如何处理这个? – chathwind

15

您可以通过处理KeyPress事件对TextBox

void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
{ 
    e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back); 
} 

另外说允许退格键的情况下,要删除一些文本尝试,这应该完全适合你的工作

编辑

上述代码不适用于粘贴在我认为必须使用TextChanged事件的字段中,但是如果您必须删除不正确的字符或突出显示它并将光标置于光标处以供用户进行更正或者,也许您可​​以在用户输入完整文本并关闭控件后进行验证。

+0

这一个很好,但它不会允许我粘贴应该通过验证的文本。 –

4

最简单的方法是处理TextChangedEvent和检查什么被键入:

string oldText = string.Empty; 
    private void textBox2_TextChanged(object sender, EventArgs e) 
    { 
     if (textBox2.Text.All(chr => char.IsLetter(chr))) 
     { 
      oldText = textBox2.Text; 
      textBox2.Text = oldText; 

      textBox2.BackColor = System.Drawing.Color.White; 
      textBox2.ForeColor = System.Drawing.Color.Black; 
     } 
     else 
     { 
      textBox2.Text = oldText; 
      textBox2.BackColor = System.Drawing.Color.Red; 
      textBox2.ForeColor = System.Drawing.Color.White; 
     } 
     textBox2.SelectionStart = textBox2.Text.Length; 
    } 

这是一个免费的正则表达式版本,如果你喜欢。它会使文本框在输入错误时闪烁。 请注意,它似乎也支持粘贴操作。

+0

先生,这是伟大的,但你可以告诉我deatail如何停止键入automaticallu它只是如果循环检查,但如何停止键入? 和约lambda表达式? textBox1.Text.All(chr => char.IsLetter(chr))这是什么。意思是如何工作,如果你可以 –

+0

嘿!那么它做了什么,以便将输入中的前一个文本存储在oldText变量中。然后在文本框的后续更改中逐字符检查,如果该字符不是字母,则返回旧文本。如果是 - 将旧文本设置为与当前(新)文本框输入相同。 所以如果你有一个有效的输入 - oldText得到的TextBox值,如果你有无效的输出 - 它的工作方式相反。还有一些GUI造型有助于可视化是否有错误。希望这可以帮助。 –

+0

因此,即使一个字符不是字母,textBox2.Text.All()lambda也会返回false。如果全部都是字母 - 它会返回true。从此开始进行上述解释。 –

2
 if (System.Text.RegularExpressions.Regex.IsMatch(textBox1.Text, "^[a-zA-Z]+$")) 
     { 
     } 
     else 
     { 
      textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1); 
      MessageBox.Show("Enter only Alphabets"); 


     } 

请尝试在Text_KeyPress事件这个

3

编写代码为

private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 

     if (!char.IsLetter(e.KeyChar)) 
     { 
      e.Handled = true; 
     } 
    } 
5
private void textbox1_KeyDown_1(object sender, KeyEventArgs e) 
{ 
    if (e.Key >= Key.A && e.Key <= Key.Z) 
    { 
    } 
    else 
    { 
     e.Handled = true; 
    } 
} 
1
private void textBox2_KeyPress(object sender, KeyPressEventArgs e) 
     { 
      if (e.KeyChar >= '0' && e.KeyChar <= '9') 
       e.Handled = true; 
      else 
       e.Handled = false; 
     } 
+0

请包括解释为什么您的答案将工作以及如何将文本框连接到您的活动。 – Vulcronos

2

这一个是工作绝对没问题......

private void manufacturerOrSupplierTextBox_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (char.IsControl(e.KeyChar) || char.IsLetter(e.KeyChar)) 
     { 
      return; 
     } 
     e.Handled = true; 
    } 
+0

感谢您的IsControl检查。 –

0
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     if (!char.IsControl(e.KeyChar) && !char.IsLetter(e.KeyChar) && 
     (e.KeyChar !='.')) 
     { 
      e.Handled = true; 
      MessageBox.Show("Only Alphabets"); 
     } 
    } 
+1

更好地解释你的解决方案 –

1

尝试这个

  private void tbCustomerName_KeyPress(object sender, KeyPressEventArgs e) 
    { 
     e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back||e.KeyChar==(char)Keys.Space); 
    } 

它允许白空间​​太

1

你可以在按键下压事件

private void tbOwnerName_KeyPress(object sender, KeyPressEventArgs e) 
    { 

     //===================to accept only charactrs & space/backspace============================================= 

     if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space)) 
     { 
      e.Handled = true; 
      base.OnKeyPress(e); 
      MessageBox.Show("enter characters only"); 
     } 
0

这里的时间尝试下面的代码,警报我的解决方案,它的工作原理按计划:

string errmsg = "ERROR : Wrong input"; 
ErrorLbl.Text = errmsg; 

if (e.Handled = !(char.IsLetter(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Space)) 
{ 
    ErrorLbl.Text = "ERROR : Wrong input"; 
} 
else ErrorLbl.Text = string.Empty; 
if (ErrorLbl.Text == errmsg) 
{ 
    Nametxt.Text = string.Empty; 
} 
0

请在KeyPress事件的文本框中尝试以下代码

if (char.IsLetter(e.KeyChar) == false & 
     Convert.ToString(e.KeyChar) != Microsoft.VisualBasic.Constants.vbBack) 
      e.Handled = true 
相关问题