user3783446
user3783446

Reputation: 435

how to make custom datetimepicker using mask edit textbox in c# winform

How to make a custom date time picker control using mask edit textbox in C# WinForm. By default it should display the text 'MM/DD/YYYY' and after clicking on it, it should show the mask edit TextBox.

Upvotes: 0

Views: 1965

Answers (1)

madan
madan

Reputation: 783

you can show a example by using below code snippet
in the designer.cs add following lines

        this.maskedTextBox1.Location = new System.Drawing.Point(29, 251);
        this.maskedTextBox1.Mask = "00/00/0000";
        this.maskedTextBox1.Name = "maskedTextBox1";
        this.maskedTextBox1.Size = new System.Drawing.Size(100, 20);
        this.maskedTextBox1.TabIndex = 10;
        this.maskedTextBox1.Text = "31071770";
        this.maskedTextBox1.TextMaskFormat = System.Windows.Forms.MaskFormat.ExcludePromptAndLiterals;
        this.maskedTextBox1.Enter += new System.EventHandler(this.maskedTextBox1_Enter);
        this.maskedTextBox1.Leave += new System.EventHandler(this.maskedTextBox1_Leave);

and in the code.cs add these functions

        private void maskedTextBox1_Enter(object sender, EventArgs e)
    {
        if (maskedTextBox1.Text == "31071770")
        {
            maskedTextBox1.Text = "";
        }
    }

    private void maskedTextBox1_Leave(object sender, EventArgs e)
    {
        if (maskedTextBox1.Text == "")
        {
            maskedTextBox1.Text = "31071770";
        }
    }

o/p 31/07/1970

Note : At last you have to validate that date

Upvotes: 0

Related Questions