设置文本框字段以接受输入作为日期格式 [英] Set textbox field to accept input as date format
本文介绍了设置文本框字段以接受输入作为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我可以知道如何设置文本框(例如.txt1)以接受日期格式(例如2014-05)。每次运行程序时都会自动出现 - ,输入2014后,光标会跳到 - 后面。我知道有DateTimePicker,但我的白痴团队领导坚持要做文本框的事情。任何帮助将不胜感激。
目前我有以下代码(使用DateTimePicker):
Hi, may i know how can i set the textbox(eg. txt1) to accept date format(eg. 2014-05). The "-" would be automatically be there every time i run the program and after i have enter "2014", the cursor would jump to behind the "-". I know there is DateTimePicker but my idiot team leader insist me on doing the textbox thing. Any help would be appreciated thks.
Currently i have the following code(which is using DateTimePicker):
string dateFrom = perFromDate.Value.ToString("MMM-yyyy");
string dateTo = perToDate.Value.ToString("MMM-yyyy");
TimeSpan range = DateTime.Parse(dateTo).Subtract(DateTime.Parse(dateFrom));
int newRange = range.Days;
if (newRange > 365)
{
MessageBox.Show("Date is out of range.", "Validation", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
推荐答案
您可以使用AjaxMaskExtnder获取更多信息:
Date MaskEdit ,
MaskExtender示例
You can use AjaxMaskExtnder for more you can refer :
Date MaskEdit ,
Example of MaskExtender
从它的属性设置datetimepicker格式
Set datetimepicker format from it's Properties
private void Form1_Load(object sender, EventArgs e)
{
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = "yyyy-MM";
}
这篇关于设置文本框字段以接受输入作为日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文