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

    Bryan

    Wednesday, August 10, 2005 1:45 PM

All replies

  • 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.
    Friday, May 11, 2007 12:36 PM
  •  

    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")

     

     

    Monday, June 18, 2007 4:20 PM
  • 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.

    Monday, June 18, 2007 8:26 PM
  • 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

     

    Thanks

     

    Bob

     

    Sunday, April 13, 2008 3:28 PM
  •  

    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.

     

    Thanks,

     

    Jason

    Monday, November 10, 2008 4:41 PM
  • Glad to help.

     

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

     

    Monday, November 10, 2008 4:48 PM
  • 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.

    Wednesday, November 12, 2008 11:01 PM
  • What is the solution when the maskedtextbox is not databound? 
    Thursday, December 4, 2008 4:12 PM
  • I don't databind. Just pass the string to the text of the textbox.

     

    Thursday, December 4, 2008 6:51 PM
  • I also face this problem in c# but i dont find out the solution till now plz help me out

    any suggestion welcome
    Monday, January 18, 2010 6:39 AM
  • 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.
    Thursday, May 2, 2013 4:13 AM
  • 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.

    THANK YOU!

    Wednesday, August 5, 2015 5:31 PM