本文介绍了文本框中的数字验证(无数字)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想要做的是有一个不允许输入任何数字的文本框。我目前有以下代码,但它给出了一个错误运算符||不能应用在0和1。我想知道如何解决这个错误并让代码做我想做的事情。 if (TextBox1.Text == 0 | | 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9) { MessageBox.Show( 没有数字允许名称); } 解决方案 您可以使用 TextBox 的 KeyPress 事件控制: 私人 void textBox1_KeyPress( object sender,KeyPressEventArgs e) { char [] charsNotAllowed = {' 0',' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9' }; if (charsNotAllowed.Contains(e.KeyChar)) { e.Handled = 真; } } 希望它可以帮到你:) Marcin解决方案4的变体: private void textBox1_KeyPress( object sender,KeyPressEventArgs e) { if ( char .IsDigit(e.KeyChar)) { e.Handled = true ; } } 无需数组! 尝试 if (TextBox1.Text == 0 || TextBox1.Text == 1 || TextBox1 .Text == 2 || TextBox1.Text == 3 || TextBox1.Text == 4 || TextBox1.Text == 5 | | TextBox1.Text == 6 || TextBox1.Text == 7 || TextBox1.Text == TextBox1.Text == 8 || TextBox1.Text == 9) { MessageBox.Show( 没有数字允许名称); } 您可以改用RegEx。 if (!Regex.Match(TextBox1.Text, ^ [0-9] + Hi, What I am trying to do is have a textbox that does not permit any numbers in. I currently have the below code but it is giving me a error "operator || cannot be applied" between the "0" and the "1". I was wondering how I can fix this error and make the code do what i would like it to do please.if (TextBox1.Text == "0" ||"1"|| "2"|| "3"|| "4"|| "5" || "6"|| "7"|| "8"|| "9"){ MessageBox.Show("No Numbers Allowed For name");} 解决方案 You can use KeyPress event of TextBox control:private void textBox1_KeyPress(object sender, KeyPressEventArgs e){ char[] charsNotAllowed = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; if (charsNotAllowed.Contains(e.KeyChar)) { e.Handled = true; }}Hope it helps you :)A variation on Marcin's Solution 4:private void textBox1_KeyPress(object sender, KeyPressEventArgs e){ if (char.IsDigit(e.KeyChar)) { e.Handled = true; }}No array required!Tryif (TextBox1.Text == "0" ||TextBox1.Text == "1"|| TextBox1.Text == "2"|| TextBox1.Text == "3"|| TextBox1.Text == "4"|| TextBox1.Text == "5" || TextBox1.Text == "6"|| TextBox1.Text == "7"|| TextBox1.Text == TextBox1.Text == "8"|| TextBox1.Text == "9"){ MessageBox.Show("No Numbers Allowed For name");}You could use RegEx instead.if ( !Regex.Match ( TextBox1.Text, "^[0-9]+ 这篇关于文本框中的数字验证(无数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-25 17:18