Saturday, March 17, 2012

Only Numeric Text Box C#

using System;
using System.Collections.Generic;
using System.Text;

namespace TyroDeveloperDLL
{
    public class TextBoxCustom{

        static int _decimales = 3;

        public int Decimales{
            get {
                return (_decimales);
            }
            set {
                _decimales = value;
            }
        }

        public static void OnlyNumeric(System.Windows.Forms.TextBox _TextBox, System.Windows.Forms.KeyPressEventArgs e){
            if (e.KeyChar == 8){
                e.Handled = false;
                return;
            }
            if (_TextBox.Text.Length == 0) { 
                if((e.KeyChar == Convert.ToChar("-"))){
                    e.Handled = false;
                    return;
                }
            }
            bool IsDec = false;
            int nroDec = 0;
            for (int i = 0; i < _TextBox.Text.Length; i++){
                if (_TextBox.Text[i] == '.')
                    IsDec = true;

                if (IsDec && nroDec++ >= _decimales){
                    e.Handled = true;
                    return;
                }
            }
            if (e.KeyChar >= 48 && e.KeyChar <= 57)
                e.Handled = false;
            else if (e.KeyChar == 46)
                e.Handled = (IsDec) ? true : false;
            else
                e.Handled = true;
        }

        public static void OnlyLetters(System.Windows.Forms.TextBox textBox, System.Windows.Forms.KeyPressEventArgs e){
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        public static void OnlyLetterAndNumber(System.Windows.Forms.TextBox _TextBox, System.Windows.Forms.KeyPressEventArgs e){
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

        public static void OnlyKeys(System.Windows.Forms.TextBox textBox, System.Windows.Forms.KeyPressEventArgs e){
            if (Char.IsLetter(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsNumber(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
    }
}

Please visit: www.TyroDeveloper.com

Please Click on +1

No comments:

Post a Comment