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

  • Question

  • 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.


    mercredi 10 août 2005 13:45

Toutes les réponses

  • 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.
    vendredi 11 mai 2007 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")



    lundi 18 juin 2007 16: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.

    lundi 18 juin 2007 20: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






    dimanche 13 avril 2008 15: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.





    lundi 10 novembre 2008 16:41
  • Glad to help.


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


    lundi 10 novembre 2008 16: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.

    mercredi 12 novembre 2008 23:01
  • What is the solution when the maskedtextbox is not databound? 
    jeudi 4 décembre 2008 16:12
  • I don't databind. Just pass the string to the text of the textbox.


    jeudi 4 décembre 2008 18:51
  • I also face this problem in c# but i dont find out the solution till now plz help me out

    any suggestion welcome
    lundi 18 janvier 2010 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.
    jeudi 2 mai 2013 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.


    mercredi 5 août 2015 17:31