[英]Mask Textbox C# 2016 for Persian Date?
I use a Mask Text box in c# 2016 for Persian Date for accepting such date: 我在C#2016中使用“蒙版文本”框表示波斯日期,以接受这样的日期:
1367/1/1 1367/1/1
I set Right to left to True and set mask to 00 /00 /0000 . 我将从右到左设置为True并将掩码设置为00/00/0000。 all things is okey but user must input 1367/01/01.
一切正常,但用户必须输入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).
我希望能够在用户输入1367/1/1之后将文本框替换为1367/01/01的时候使用此掩码文本框(即,在月份和日期中添加零)。 any comments should be highly appropriated.
任何评论都应高度适当。
You might consider adding a KeyUp event method that looks like the following: 您可以考虑添加如下所示的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;
}
}
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. 这将允许输入的人输入1/1/2016,并且当他们键入“ /”键时,它将更新前两个日期部分中小于10的任何值,并输出= 01/01/2016。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.