且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何验证Windows应用程序中的金额文本框

更新时间:2023-10-13 09:02:28

你可以使用正则表达式验证,短搜索或阅读这个 [ ^ ]应该让你进入它。使用正则表达式匹配,您可以轻松实现检查并确保只放入数字。



另一方面,您可以通过验证插入的字符串来检查自己使用子字符串或包含。



例如检查字符串是否超过8个字符,然后检查字符串是否在第8(9)位包含'。' 。
You could use a regex validation on that, a short googling or reading of this[^] should get you into it. With a regex matching you could easily implement your check and make sure only numbers are put in.

On the other hand you could check by your self by validating the inserted string with substring or contains.

For example check if the string is longer than 8 chars and then check if the string contains a '.' at the 8th (9th) place.


private void txtamount_KeyPress(object sender, KeyPressEventArgs e)
       {
            string[] arr = txtamount.Text.Split('.');

           if (arr.Length == 1)
           {
               if (txtamount.Text.Length > 7 && e.KeyChar != '.' && e.KeyChar != (char)Keys.Back)
               {
                   e.Handled = true;
               }

           }
       }