درج تاریخ در TextBox مانند MaskTextBox

یک برنامه ویندوزی ایجاد کرده و textBox بر روی فرم قرار دهید و در رویداد KeyUp آن کد زیر را بنویسید :

using System;
using System.Windows.Forms;

namespace TextBoxDate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            textBox1.MaxLength = 10;
            try
            {
                if (textBox1.TextLength < 10)
                {
                    if (textBox1.TextLength == 4)
                    {
                        textBox1.Text = this.textBox1.Text + '/';
                        textBox1.Select(textBox1.TextLength, textBox1.TextLength);
                    }

                    if (textBox1.TextLength == 7)
                    {
                        textBox1.Text = textBox1.Text + '/';
                        textBox1.Select(textBox1.TextLength, textBox1.TextLength);
                    }

                    if (e.KeyData == Keys.Back)
                    {
                        string a = textBox1.Text;
                        textBox1.Text = a.Remove(a.Length - 1);
                        textBox1.Select(textBox1.TextLength, textBox1.TextLength);
                    }
                }

            }
            catch (Exception)
            {
                //throw;
            }
        }
    }
}

حال در داخل textBox یک تاریخ بنویسید و نتیجه را مشاهده کنید.
ارسال کننده : datatools
لینک منبع