FIX: MaskedTextBox and Databinding Date fails with inital digit of 0 RRS feed

  • 問題

  • Problem
    : If I bind a DateTime field to a masked text box with the default short date mask, the value fails to recognize a leading 0 and shifts all other digits one place to the left.

    Solution: Select the MaskedTextBox.  Expand Databindings in Properties.  Click on the ellipses for “(Advanced)”

    A dialog box appears where you can set a custom format.  Choose custom and select MM/dd/yyyy as the custom format.

    I posted this because I looked everywhere trying to find an answer without success.  The Ladybug site (Bug ID FDBK21883) does not give an adequate answer.

    Hope it helps someone.


    2005年8月10日 下午 01:45


  • I had the same problem. I'll bet you are assigning a string to this control. If you do it ignores the slashes. You have to format the date string to "G" mmddyyyy with no slashes.
    2007年5月11日 下午 12:36

    Databinding Date fails VS.Net 2.0 VB.Net 2.0 MaskedTextBox Short Date


    Sending the ToString() conversion results in a "G" mmddyyyy format with zeros and seperators dropped no matter what the control settings.

    e.g. text_date_needed.Text = dslog.Fields("date_needed").Value.ToString()


    You have to pass the date value itself.

    e.g. text_date_needed.Text = Format(dslog.Fields("date_needed").Value, "MM/dd/yyyy")



    2007年6月18日 下午 04:20
  • Both of your code examples convert the date to a string, but they put the date in different formats. ToString() on a date will give you the full date and time string, whereas your call to Format only includes the date portion. Most likely the problem is the MaskedTextBox didn't know what to do with the extra characters it received that were not in the mask.

    2007年6月18日 下午 08:26
  • This fix is very helpful. I had given up on using maskedtextbox with shortdate fields because I couln't figure out how to do this, despite spending many more hours then I would like to admit. Like everything else with .net, once you see how to do it, it makes sense and seems so simple






    2008年4月13日 下午 03:28

    I appreciate you posting that here.  It worked.  the MM/dd/yyyy seem to be cap sensitive.  I decided to use the MaskedTextBox because the datetimepicker did not easily allow for a null value.





    2008年11月10日 下午 04:41
  • Glad to help.


    Yes, the date format is case sensitive. MM indicates month, but mm indicates minutes.


    2008年11月10日 下午 04:48
  • Brian,


    Great job.


    I've been fighting this problem for two days. Glad I finally typed in the right combination of words into Google search and found this posting.


    Pretty sorry that there isn't anything from Microsoft regarding this issue.

    2008年11月12日 下午 11:01
  • What is the solution when the maskedtextbox is not databound? 
    2008年12月4日 下午 04:12
  • I don't databind. Just pass the string to the text of the textbox.


    2008年12月4日 下午 06:51
  • I also face this problem in c# but i dont find out the solution till now plz help me out

    any suggestion welcome
    2010年1月18日 上午 06:39
  • Any way of doing this is code?  All my data is gathered via code so no actual dataset information is available when choosing advanced binding options for my maskedtextbox.
    2013年5月2日 上午 04:13
  • I don't care how old this is.. it worked for me in VS 2012. I looked for two days trying to find a solution to this issue of shifted characters in a maskedtextbox and this was the fix.


    2015年8月5日 下午 05:31