用户可以在文本框中键入Only Numeric values [英] User can type Only Numeric valuesin the textbox

查看:68
本文介绍了用户可以在文本框中键入Only Numeric values的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何解决此问题

How can I solve this problem

private void txtTestingNumeric_TextChanged(object sender, EventArgs e)
       {
           if (System.Text.RegularExpressions.Regex.IsMatch("[^0-9]", txtTestingNumeric.Text))
           {
               MessageBox.Show("Please enter only numbers.");
               txtTestingNumeric.Text.Remove(txtTestingNumeric.Text.Length - 1);
           }
       }



请帮助我

提前感谢


Please help me
thanks in advance

推荐答案

使用像这样的文本框的keyPress事件

use the keyPress event of textbox like this
private void txtTestingNumeric_KeyPress(object sender, KeyPressEventArgs e)
       {
           int ascii = e.KeyChar;
           if ((ascii >= 48 && ascii <= 57) || ascii == 8 || ascii == 13 || ascii==46)
           {
               e.Handled = false;
           }
           else
           {
               e.Handled = true;
           }
       }


private void TectBox1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if ((e.KeyChar >= 48 && e.KeyChar <= 57) )
           {
               e.Handled = false;
           }
           else
           {

               e.Handled = true;
           }
       }


这是最好的代码..它将允许你只使用数字和空格和退格

This Is Best Code.. It will allow you to use only numbers and Space and Backspace
private void txttax1_KeyPress(object sender, KeyPressEventArgs e)
       {
           if ((e.KeyChar >= 48 && e.KeyChar <= 57) || (e.KeyChar == 32) || (e.KeyChar == 8))
           {
               e.Handled = false;
           }
           else
           {

               e.Handled = true;
           }
       }


这篇关于用户可以在文本框中键入Only Numeric values的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆