[翻译]自定义TextBox数字和文本验证

代码下载:http://dev.mjxy.cn/a-Custom-TextBox-validation-numbers-and-text.aspx

 

Numbers and Characters only Textbox Validation in C#

源文档 <http://www.codeproject.com/KB/cs/TexboxValidation.aspx>

翻译:白水(敏捷学院)

 

介绍

GUI开发中,处理文本框验证是一种很常见的任务。通常情况下,处理一个TextBox允许用户输入数字(无论是整数或实)或字母字符我在网络上找到的代码用正则表达式处理这个问题,它工作得很好,但有一定的局限性。在这篇文章中,我使用另外一种方法,方便和灵活的方式处理的TextBox验证

 

代码结构

定义一下3个方法在TextBox TextChanged事件触发时使用:

  1. validateTextInteger()
  2. validateTextDouble()
  1. validateTextCharacter()

 

validateTextInteger()仅允许用户输入Integer类型的值,validateTextDouble仅允许用户输入double类型的值,validateTextCharacter()仅允许用户输入字符。

要使用这些验证,只需要在TextChanged事件选择对应的方法即可

 

以上3个方法都是正常的验证功能,另外3个是更加灵活的自定义验证方法,我们将在自定义验证部分讨论。

ValidateTextInteger

首先使用parse方法来转换TextBox中的文本,如果是Integer类型则不会有任何变化。但是如何parse方法转换失败抛出一个异常,代码将捕获异常,然后定位TextBox中的光标回到上一个位置,并删除最后输入的字符。当TextBox只有一个"-"时不执行Parse方法。

private void validateTextInteger(object sender, EventArgs e)

        {

           

            Exception X = new Exception();

 

            TextBox T = (TextBox)sender;

 

            T.Text = T.Text.Trim();

            try

            {

                if (T.Text != "-")

                {

                    int x = int.Parse(T.Text);                

                }

            }

            catch (Exception)

            {

                try

                {

                    int CursorIndex = T.SelectionStart - 1;

                    T.Text = T.Text.Remove(CursorIndex, 1);

 

                    //Align Cursor to same index

                    T.SelectionStart = CursorIndex;

                    T.SelectionLength = 0;

                }

                catch (Exception) { }

 

            }

        }

示例:

 

输入

处理后

11.

11

34a5

345

5_67

567

 

 

 

ValidateTextDouble

Double类型的验证与Integer的处理方式相似

private void validateTextDouble(object sender, EventArgs e)

        {          

         

            Exception X = new Exception();

 

            TextBox T = (TextBox)sender;

 

            T.Text = T.Text.Trim();

 

            try

            {

                if (T.Text != "-")

                {

                    double x = double.Parse(T.Text);

                 

                    if (T.Text.Contains(','))

                        throw X;

                   

                }

            }

            catch (Exception)

            {

                try

                {

                    int CursorIndex = T.SelectionStart - 1;

                    T.Text = T.Text.Remove(CursorIndex, 1);

 

                    //Align Cursor to same index

                    T.SelectionStart = CursorIndex;

                    T.SelectionLength = 0;

                }

                catch (Exception) { }

            }

        }

示例 :

 

输入

处理后

23.97.

23.97

0d.01

0.01

2$16

216

ValidateTextCharacter

对于字符的验证定义了一个 textContainsUnalowedCharacter()方法来处理不允许输入的字符,如果检测到不允许输入的字符将被移除并重新定位光标位置。

private void validateTextCharacter(object sender, EventArgs e)

        {

           

            TextBox T = (TextBox)sender;

           

            try

            {

                //Not Allowing Numbers

                char[] UnallowedCharacters = { '0', '1',

                                               '2', '3',

                                               '4', '5',

                                               '6', '7',

                                               '8', '9'};

                if (textContainsUnallowedCharacter(T.Text,UnallowedCharacters))

                {

                    int CursorIndex = T.SelectionStart - 1;

                    T.Text = T.Text.Remove(CursorIndex, 1);

                    //Align Cursor to same index

                    T.SelectionStart = CursorIndex;

                    T.SelectionLength = 0;

                }

            }

            catch(Exception){ }

        }

        private bool textContainsUnallowedCharacter(string T, char[] UnallowedCharacters)

        {

           

            for (int i = 0; i < UnallowedCharacters.Length; i++)

                if (T.Contains(UnallowedCharacters[i]))

                    return true;

            return false;

        }

示例:

输入

处理后

ab6cd

abcd

8tyhg

tyhg

%$#@6

%$#@

posted @ 2011-07-17 14:12  敏捷学院  阅读(559)  评论(1编辑  收藏  举报