How to apply formatting string when binding DateTime to MaskedTextBox?

淺唱寂寞╮ 提交于 2019-12-23 02:48:10

问题


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.

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.

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

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


回答1:


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



来源:https://stackoverflow.com/questions/2329164/how-to-apply-formatting-string-when-binding-datetime-to-maskedtextbox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!