none
Masked textbox formatting issue

    Question

  • C#, Visual Studio 2008 Express, Winforms, MS Access.

    I have a form that opens another form where user is to enter a date and if the date exists in the database the 2nd form closes and the parent form displays all the relating data that is tied to that date.  My problem is that if the date in the database is 5/29/2013 it gets displayed in the masked textbox(00/00/0000) as 52/92/013_.  The date is being passed as a string so I formatted the string like so to deal with this:

    string output = String.Format("0{0}", thedate);

    This corrects the issue for months 1-9, but if the month is 11/27/2012 then it is displayed as 01/12/7201. 

    What is the best way of dealing with this.  Should I be going an entirely differnet route?

    Wednesday, July 03, 2013 6:40 PM

Answers

  •  If I'm pulling dates from a database wouldn't this change them all to Now?  Besides I came up with the following solution:

    I'm using a property that holds the date of the record in database.  If the record is a certain length(for months that are single digit)

    theDataConn.InvoiceDate = dRow[1].ToString();
                    if (theDataConn.InvoiceDate.Length == 21)
                    {
                        theDataConn.InvoiceDate = "0" + theDataConn.InvoiceDate;
                    }

    I then assign this to my masked textbox where appropriate.

    I used Now just as an example, you would replace Now with your date field.

    theDataConn.InvoiceDate = dRow[1].ToString();
        if (theDataConn.InvoiceDate.Length == 21)
        {
            theDataConn.InvoiceDate = theDataConn.InvoiceDate.ToString("MM/dd/yyyy");
        }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 05, 2013 2:42 PM
  • Please refer to DataTime.ToString docs, have no idea why you can not use the overload

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 05, 2013 5:44 PM

All replies

  • If the date is a string and we convert it to a date and use .ToString where the date is 5/29/2013 you would get 05/29/2013 displayed in the MaskedTextBox.

    Example

    MaskedTextBox1.Text = Now.ToString("MM/dd/yyyy")
    The same thing if you used a DateTimePicker when setting CustomFormat to MM/dd/yyyy we get a leading zero.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Thursday, July 04, 2013 12:50 AM
  •  If I'm pulling dates from a database wouldn't this change them all to Now?  Besides I came up with the following solution:

    I'm using a property that holds the date of the record in database.  If the record is a certain length(for months that are single digit)

    theDataConn.InvoiceDate = dRow[1].ToString();
                    if (theDataConn.InvoiceDate.Length == 21)
                    {
                        theDataConn.InvoiceDate = "0" + theDataConn.InvoiceDate;
                    }

    I then assign this to my masked textbox where appropriate.
    Friday, July 05, 2013 2:03 PM
  •  If I'm pulling dates from a database wouldn't this change them all to Now?  Besides I came up with the following solution:

    I'm using a property that holds the date of the record in database.  If the record is a certain length(for months that are single digit)

    theDataConn.InvoiceDate = dRow[1].ToString();
                    if (theDataConn.InvoiceDate.Length == 21)
                    {
                        theDataConn.InvoiceDate = "0" + theDataConn.InvoiceDate;
                    }

    I then assign this to my masked textbox where appropriate.

    I used Now just as an example, you would replace Now with your date field.

    theDataConn.InvoiceDate = dRow[1].ToString();
        if (theDataConn.InvoiceDate.Length == 21)
        {
            theDataConn.InvoiceDate = theDataConn.InvoiceDate.ToString("MM/dd/yyyy");
        }


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 05, 2013 2:42 PM
  • Upon further testing I have realized I can't determine a length because what if the day is a single digit.

    Using your example I tried just putting the format directly to the assignment:

    theDataConn.InvoiceDate = dRow[1].ToString("MM/dd/yyyy");

    But I get 'No overload for method 'ToString' takes 1 arguments.

    I also tried:

    mskSearchDate.Text = theDataConn.InvoiceDate.ToString("MM/dd/yyyy");

    but i receive 'the best overloaded method match for string.ToString(System.IFormatProvider) has some invalid arguments.  Cannot convert from string to System.IFormatProvider

    Friday, July 05, 2013 3:39 PM
  • Please refer to DataTime.ToString docs, have no idea why you can not use the overload

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.

    Friday, July 05, 2013 5:44 PM