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

  • Pergunta


  • 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

    quarta-feira, 10 de agosto de 2005 13:45

Todas as Respostas

  • 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.
    sexta-feira, 11 de maio de 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")

     

     

    segunda-feira, 18 de junho de 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.

    segunda-feira, 18 de junho de 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

     

    Thanks

     

    Bob

     

    domingo, 13 de abril de 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.

     

    Thanks,

     

    Jason

    segunda-feira, 10 de novembro de 2008 16:41
  • Glad to help.

     

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

     

    segunda-feira, 10 de novembro de 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.

    quarta-feira, 12 de novembro de 2008 23:01
  • What is the solution when the maskedtextbox is not databound? 
    quinta-feira, 4 de dezembro de 2008 16:12
  • I don't databind. Just pass the string to the text of the textbox.

     

    quinta-feira, 4 de dezembro de 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
    segunda-feira, 18 de janeiro de 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.
    quinta-feira, 2 de maio de 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.

    THANK YOU!

    quarta-feira, 5 de agosto de 2015 17:31