如何绑定日期时间到MaskedTextBox中应用时,格式化字符串? [英] How to apply formatting string when binding DateTime to MaskedTextBox?

查看:614
本文介绍了如何绑定日期时间到MaskedTextBox中应用时,格式化字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 MaskedTextBox中使用面膜00/00 / \\ 2 \\ 000来限制输入的格式XX / XX / 20XX ,以及绑定到 MyBindingSource.SomeProperty的文本属性类型的DateTime

I have a MaskedTextBox using the mask "00/00/\2\000" to restrict input to a format of XX/XX/20XX, with the Text property bound to MyBindingSource.SomeProperty of type DateTime.

近日,随着一个个位数的月或日值近期开始显示不正确。我想到的是,的ToString()方法被调用的值在数据绑定某些时候,我相信呼叫未填充月/日以零。

Recently, values with a single-digit month or day recently started displaying incorrectly. I expect that the ToString() method is being called on the value at some point in data binding, and I believe the call is not padding month / day with zeroes.

我试图在高级数据绑定属性的 MaskedTextBox.Text 属性的格式类型设置为的DateTime ,但它并没有帮助。

I tried setting the format type of the MaskedTextBox.Text property to DateTime in the advanced data binding properties, but it didn't help.

如何申请的ToString(MMDDYYYY)的DateTime 对象转换为字符串,前值被绑定到文本属性?

How can I apply ToString("MMddyyyy") when converting the DateTime object to a string, before the value is bound to the Text property?

推荐答案

您可以使用绑定的的解析和的格式事件自己做转换,如<一看到href=\"http://stackoverflow.com/questions/982261/bind-nullable-datetime-to-maskedtextbox/986666#986666\">this回答

You can use the binding's Parse and Format events to do the conversion yourself, as seen in this answer

这篇关于如何绑定日期时间到MaskedTextBox中应用时,格式化字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆