FOLLOW US
4dots Software CODE BLOG

FourDotsCreditCardNumbersTextBox

FourDotsCreditCardNumbersTextBox Custom Control Screenshot

For various ecommerce applications it is always necessary to be able to enter the credit card number of the customer in a textbox.

The credit card number consits of 16 numbers which we split into 4 parts, this means that we need 4 textboxes.

This custom control makes it easy to move to the next credit card number textbox once the user has entered the 4 numbers that consist each of the four parts.

This means that he does not have to press the TAB key to move to the next textbox or mouse click on the next textbox.

The the input cursor is automatically moved to the next textbox when he has entered 4 numbers and its contents are selected so that they can be easily overwritten.

Download Demo Project

FourDotsCreditCardNumbersTextBox Demo Project

Source Code


using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace FourDotsCreditCardNumbersTextBoxDemo
{
    public class FourDotsCreditCardNumbersTextBox : System.Windows.Forms.TextBox 
    {
        public FourDotsCreditCardNumbersTextBox() :base()
        {
            InitializeComponent();
        }    

        public bool SelectAllOnEnter = true;              

        public bool MoveToNextTextBox = true;
        
        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);

            if (SelectAllOnEnter)
            {
                this.SelectAll();
            }
        }

        protected override void OnClick(EventArgs e)
        {
            base.OnClick(e);

            if (SelectAllOnEnter)
            {
                this.SelectAll();
            }
        }

        protected override void OnTextChanged(EventArgs e)
        {
            base.OnTextChanged(e);

            if (MoveToNextTextBox)
            {
                if (this.SelectionStart == this.MaxLength)
                {
                    int nextminleft = -1;
                    Control next_txt = null;

                    foreach (Control co in this.Parent.Controls)
                    {
                        if (co is FourDotsCreditCardNumbersTextBox)
                        {
                            if (co.Left > this.Left && (co.Left < nextminleft || (nextminleft == -1)))
                            {
                                next_txt = co;
                                nextminleft = co.Left;
                            }
                        }
                    }

                    if (next_txt != null)
                    {
                        next_txt.Focus();

                        if (SelectAllOnEnter)
                        {
                            ((FourDotsCreditCardNumbersTextBox)next_txt).SelectAll();
                        }
                    }
                }
            }
        }

        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }            

            base.OnKeyPress(e);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            // 
            // FourDotsCreditCardNumbersTextBox
            // 
            this.Font = new System.Drawing.Font("Microsoft Sans Serif", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(161)));
            this.MaxLength = 4;
            this.ResumeLayout(false);

        }
        
    }
}
   

 

Please Donate!

Please Share!