Click here to Skip to main content
15,894,825 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have a Textbox mask when I enter the data 02/22/2019 and save to the datagrid the grid shows the correct format but the mask textbox shows 22/22/019. It's deleting the leading 0. Data source is Access database and it is set to date time. What is the issue? I have also tried it in code but still get the same results.

Results saved in Grid and database = 02/22/2019
When I select the grid row it shows in the mask textbox as 22/22/019, missing the 0.

Any help

What I have tried:

C#
private void Form1_Load_1(object sender, EventArgs e)
        {      
 maskedTextBox1.Mask = "00/00/0000";

            maskedTextBox1.MaskInputRejected += new MaskInputRejectedEventHandler(maskedTextBox1_MaskInputRejected);
            maskedTextBox1.KeyDown += new KeyEventHandler(maskedTextBox1_KeyDown);
        }

        void maskedTextBox1_MaskInputRejected(object sender, MaskInputRejectedEventArgs e)
        {
            if (maskedTextBox1.MaskFull)
            {
                toolTip1.ToolTipTitle = "Input Rejected - Too Much Data";
                toolTip1.Show("You cannot enter any more data into the date field. Delete some characters in order to insert more data.", maskedTextBox1, 0, -20, 5000);
            }
            else if (e.Position == maskedTextBox1.Mask.Length)
            {
                toolTip1.ToolTipTitle = "Input Rejected - End of Field";
                toolTip1.Show("You cannot add extra characters to the end of this date field.", maskedTextBox1, 0, -20, 5000);
            }
            else
            {
                toolTip1.ToolTipTitle = "Input Rejected";
                toolTip1.Show("You can only add numeric characters (0-9) into this date field.", maskedTextBox1, 0, -20, 5000);
            }
        }
Posted
Updated 16-Jun-22 21:58pm

I have found the answer.

Go to Property of the masked text box. Select 'DataBindings', then 'Advanced'. Select 'Custom' in the 'Format Type' list box in the middle of the dialog box. Enter 'MM/dd/yyyy' in the 'Custom Format' box to the right.

I hope this helps others
 
Share this answer
 
Comments
Dave Kreskowiak 15-Apr-18 13:31pm    
I really have to question why you're putting dates into the database as strings instead of a date type. This pretty much renders any date queries useless as you're no longer comparing dates but strings.
The problem solves changing your Windows formatting date configuration for short date:
Open your Windows the Control Panel. ( Small Icon)
Click on the Region icon.
Click on the Customize this format button. ( Circled Red below)
Click on the Date tab.
Select short date and change the Date Format: DD-MMM-YYYY.
Click OK to apply
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900