[英]Mask Textbox C# 2016 for Persian Date?
我在C#2016中使用“蒙版文本”框表示波斯日期,以接受這樣的日期:
1367/1/1
我將從右到左設置為True並將掩碼設置為00/00/0000。 一切正常,但用戶必須輸入1367/01/01。 我希望能夠在用戶輸入1367/1/1之后將文本框替換為1367/01/01的時候使用此掩碼文本框(即,在月份和日期中添加零)。 任何評論都應高度適當。
您可以考慮添加如下所示的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;
}
}
這將允許輸入的人輸入1/1/2016,並且當他們鍵入“ /”鍵時,它將更新前兩個日期部分中小於10的任何值,並輸出= 01/01/2016。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.