简体   繁体   中英

Mask Textbox C# 2016 for Persian Date?

I use a Mask Text box in c# 2016 for Persian Date for accepting such date:

1367/1/1

I set Right to left to True and set mask to 00 /00 /0000 . all things is okey but user must input 1367/01/01. I want to capable this mask text box for times when user input 1367/1/1 then the text box replace 1367/01/01 (ie add zeros for month and day). any comments should be highly appropriated.

You might consider adding a KeyUp event method that looks like the following:

private void maskedTextBox1_KeyUp(object sender, KeyEventArgs e)
    {
        //check for slash press
        if (e.KeyValue == 191)
        {
            //split the input into string array
            string[] input = maskedTextBox1.Text.Split('/');

            //save current cursor position
            int currentpos = maskedTextBox1.SelectionStart;

            // currently entering first date part
            if (currentpos < 3)
            {
                // add leading 0 if number is less than 10
                if ((Int32.Parse(input[0]) < 10) && (!input[0].Contains("0")))
                {
                    input[0] = '0' + input[0];
                    currentpos++;
                }
            }
            // currently entering second date part
            else if ((currentpos < 6) && (currentpos > 3))
            {
                // add leading 0 if number is less than 10
                if ((Int32.Parse(input[1]) < 10) && (!input[1].Contains("0")))
                {
                    input[1] = '0' + input[1];
                    currentpos++;
                }
            }
            // currently entering last date part
            else
            {
                // do nothing here!
            }
            // set masked textbox value to joined array
            maskedTextBox1.Text = String.Join("/",input);
            // adjust the cursor position after setting new textbox value
            maskedTextBox1.SelectionStart = currentpos;
        }
    }

This would allow the person entering to enter 1/1/2016 and as they typed the '/' key it would update any values under 10 in the first two date parts and output = 01/01/2016.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM