验证文本框 [英] Validating textBox

查看:62
本文介绍了验证文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试检查文本框中是否已经存在小数点.  如果允许,请勿再允许其他人.  如果您要创建自己的键盘,这非常容易,但是我无法使用SIP进行此验证.

I'm trying to check to see if a decimal point already exist in a textBox.  If it does, don't allow another.  This is very easy if you're creating your own keypad but I haven't been able to achieve this validation using SIP.


推荐答案

根据需要,使用下面的代码向您的文本框中添加一个按键事件处理程序. e.Handled阻止事件冒泡.

 

在使用文本框完成操作后,您需要清除bool标志,以便再次输入dp.调试写入行非常方便,可以查看每个键的代码.

       bool   hasDP  =  false ;  
       私有 void 对象  发件人,  KeyEventArgs  e)  
        { 
            Debug.WriteLine(键被按下== {0}" ,e.PlatformKeyCode.ToString());  
            如果  
            {
               如果 (hasDP) 
              td>
              nbsp; b true ;  
             b ;  返回 ;  
             } 
             true ;  
          } 
       } 
 
        bool hasDP = false;  
        private void textBox1_KeyDown(object sender, KeyEventArgs e)  
        {  
            Debug.WriteLine("key pressed = {0}", e.PlatformKeyCode.ToString());  
            if (e.PlatformKeyCode == 190)   // code for decimal point   
            {  
                if (hasDP)  
                {  
                    e.Handled = true;  
                    return;  
                }  
                hasDP = true;  
            }  
        }  
 


这篇关于验证文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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