주 콘텐츠로 건너뛰기

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

    Bryan

    2005년 8월 10일 수요일 오후 1: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일 월요일 오후 4: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일 월요일 오후 8: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

     

    Thanks

     

    Bob

     

    2008년 4월 13일 일요일 오후 3: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.

     

    Thanks,

     

    Jason

    2008년 11월 10일 월요일 오후 4:41
  • Glad to help.

     

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

     

    2008년 11월 10일 월요일 오후 4: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일 목요일 오후 4:12
  • I don't databind. Just pass the string to the text of the textbox.

     

    2008년 12월 4일 목요일 오후 6: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일 월요일 오전 6: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일 목요일 오전 4: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.

    THANK YOU!

    2015년 8월 5일 수요일 오후 5:31