面具文本框C#2016波斯日期?

时间:2016-06-04 14:05:00

标签: c# .net winforms datetime textbox

我在c#2016中使用掩码文本框作为波斯日期接受此日期:

1367/1/1

我从右到左设置为True并将mask设置为00/00/0000。所有事情都很好,但用户必须输入1367/01/01。当用户输入1367/1/1然后文本框替换1367/01/01(即为月和日添加零)时,我希望能够使用此掩码文本框。任何评论都应该被高度挪用。

1 个答案:

答案 0 :(得分:-1)

您可以考虑添加如下所示的KeyUp事件方法:

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;
        }
    }

这将允许进入的人进入2016年1月1日,并且当他们输入&#39; /&#39;键,它将更新前两个日期部分中的10以下的任何值,并输出= 01/01/2016。

相关问题