[C#] [UWP]仅接受文本框号码 [英] [C#][UWP] Accepts textbox only Numbers

查看:48
本文介绍了[C#] [UWP]仅接受文本框号码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你能帮我解决这个只接受号码的事吗。我在c#中有一个代码看起来很好

Can you help me on this accepting only numbers. I have a code in c# looks fine

这里是代码 

here's the code 

char ch = (char)e.Key;

if (ch == 46 && txtquantity.Text.IndexOf('.') != -1)
  {
      e.Handled = true;
      return;
  }

if (!Char.IsDigit(ch) && ch != 8 && ch != 46)
 {
     e.Handled = true;
  }

看起来不错,但是当我点击特殊字符时,就像  !@# $%^& *()_ + [] \ /

that looks fine but when I tap the Special Characters  like  this !@#$%^&*()_+[]\/

仍然接受并显示在文本框中。对此有什么解决方案吗?

still accepts and display on the textbox . Is there any solution on this ?

先谢谢了;)

推荐答案

嗨  imNeil



如果仅接受文本框中的数字,则在下面添加以下代码行随时随地申请。



            //仅限文本框中的数字

            string oldText = string.Empty;

            if(txtbxusermobilenumber.Text.All(chr => char.IsDigit(chr)))

            {

                oldText = txtbxusermobilenumber.Text;

                txtbxusermobilenumber.Text = oldText;



            }¥b $ b           否则

            {

                txtbxusermobilenumber.Text = oldText;              

            }¥b $ b            txtbxusermobilenumber.SelectionStart = txtbxusermobilenumber.Text.Length;
$




添加上面的代码行并检查一次后,上面的代码包含逻辑不允许文本框中的特殊字符,但每当您按下特殊字符时,它会自动清除文本中的所有信息,您之前输入的
。 



-Kishore
Hi imNeil,

For accepting only numbers in your Text Box, you have add below lines of code in your application where ever you want.

            //for only digits in text box
            string oldText = string.Empty;
            if (txtbxusermobilenumber.Text.All(chr => char.IsDigit(chr)))
            {
                oldText = txtbxusermobilenumber.Text;
                txtbxusermobilenumber.Text = oldText;

            }
            else
            {
                txtbxusermobilenumber.Text = oldText;              
            }
            txtbxusermobilenumber.SelectionStart = txtbxusermobilenumber.Text.Length;


After added the above lines of code and check it once, the above code contains the logic doesn’t allowing the special characters in Text Box, but whenever you pressed the special character then it automatically clears the entire information inside text, what you entered earlier. 

-Kishore


这篇关于[C#] [UWP]仅接受文本框号码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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