Small is New Big in C#

Coding

Make TextBox Number Only With Length Limit

leave a comment »

    //Detect Value
        private void txtID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar) || Char.IsDigit(e.KeyChar))
            { e.Handled = false; }
            else
            { e.Handled = true; }
        }

    //Check TextBox.Text Length
        private string CollectionIDCheck(string id,int length)
        {
            Regex check = new Regex("^[0-9]{" + length.ToString() +"}");
            int count;
            if (!check.IsMatch(id))
            {

                if (id.Length < length)
                {
                    count = length – id.Length;
                    string zero = "0";
                    for (int i = 1; i < count; i++)
                        zero = "0" + zero;
                    id = zero + id;
                }
                else if (id.Length > length)
                {
                    count = id.Length – length;
                    id = id.Substring(count,
length);
                }
            }
            return id;
        }

Advertisements

Written by nauhil

August 30, 2010 at 10:48 am

Posted in Tips of C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: