设置文本框字段以接受输入作为日期格式

编程入门 行业动态 更新时间:2024-10-07 16:18:35
本文介绍了设置文本框字段以接受输入作为日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我可以知道如何设置文本框(例如.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"; }

更多推荐

设置文本框字段以接受输入作为日期格式

本文发布于:2023-11-28 08:09:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1641607.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段   文本框   日期   格式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!