Tuesday 7 August 2012

custom control textbox with validation c# and vb.net


Custom control textbox with validation in C#


first that why we create a custom text box control in .net application development. is any reason or not. so first reason is you can automatically validate all control. and it's easy to use. simple example for if you want to input only integer or decimal from text box you have to write event for it on text box. suppose you have to more than one text box than you have to write for all control. where you use. so that time if you create a custom control than it's help you in fast development.

here in code i give you how to input only integer or decimal from textbox in vb.net and c#.
here code in c# and vb.net.

1> go to add new item in visual studio's current project.
2> select custome control and give name CustomeTextBox. which example given in following image.



3> Goes to source code of and change inheritance from control to TextBox or System.Windows.Forms.TextBox









4> Add following code in class. for c#. For vb.net copy it from here and convert from web. it's also working for vb.net also.

private InputType _InputSelection;
        public InputType InputTextType
        {
            set { _InputSelection = value; }
            get { return _InputSelection; }
        }
        public enum InputType
        {
            [Description("Input General String")]
            Text,
            [Description("Input Only Integer Number")]
            Integer,
            [Description("Input Only Decimal Number")]
            Decimal
        }
        string FTextC;
        public  CustomeTextBox()
        {
            InitializeComponent();
            this.TextChanged += new EventHandler(ORTextBox_TextChanged);
            this.KeyDown += new KeyEventHandler(ORTextBox_KeyDown);
            FTextC = this.Text;
        }
        protected override void OnPaint(PaintEventArgs pe)
        {
            base.OnPaint(pe);
        }
        private void ORTextBox_TextChanged(object sender, EventArgs e)
        {
            if (_InputSelection == InputType.Text)
            {

            }
            else if (_InputSelection == InputType.Integer)
            {
                if (string.IsNullOrEmpty(((TextBox)sender).Text))
                {
                    FTextC = "";
                }
                else
                {
                    Int64 num = 0;
                    bool success = Int64.TryParse(((TextBox)sender).Text, out num);
                    if (success & num >= 0)
                    {
                        ((TextBox)sender).Text.Trim();
                        FTextC = ((TextBox)sender).Text;
                    }
                    else
                    {
                        ((TextBox)sender).Text = FTextC;
                        ((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
                    }
                }
            }
            else if (_InputSelection == InputType.Decimal)
            {
                if (string.IsNullOrEmpty(((TextBox)sender).Text))
                {
                    FTextC = "";
                }
                else
                {
                    double num = 0;
                    bool success = double.TryParse(((TextBox)sender).Text, out num);
                    if (success & num >= 0)
                    {
                        ((TextBox)sender).Text.Trim();
                        FTextC = ((TextBox)sender).Text;
                    }
                    else
                    {
                        ((TextBox)sender).Text = FTextC;
                        ((TextBox)sender).SelectionStart = ((TextBox)sender).Text.Length;
                    }
                }
            }
        }
        private void ORTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                SendKeys.Send("{TAB}");
                e.SuppressKeyPress = true;
            }
        }


here also code for move's next control. when you press enter than it's send TAB key so it's moves to next control.

and it's completed. debug your application.

you get your control in Toolbox panel. just drag on form and enjoy with it.

for change input type you have to just change the property which is InputType

have you any query about it than comment.
than you.

2 comments:

  1. One more thing i want to share you , when u are using custom control the form will not validate the control when it has no focus. but u can use form_validating event with form.ValidateChildren() method and just return if false. hope it helps someone who is creating custom control.

    ReplyDelete
    Replies
    1. Thank You vishal for sharing your valuable response...

      Delete