Get formatted date with text box

fatih uyanık 225 Reputation points
2023-05-17T08:40:54.7766667+00:00

Hello

I am working on a Wpf project. I want to enter data in date format as "DD.MM.YYYY" with a text box. But when the user types "01", "." When it says "02" it says "." I want the dot mark to be added automatically. How can I do this with Regex or otherwise?

Thanks.

Developer technologies Windows Presentation Foundation
Developer technologies C#
0 comments No comments
{count} votes

6 answers

Sort by: Most helpful
  1. Hui Liu-MSFT 48,676 Reputation points Microsoft External Staff
    2023-05-17T09:07:34.36+00:00

    Hi,@fatih uyanık. For entering date in TextBox, automatically add . , you could refer to the following code.

    <TextBox x:Name="textBox" TextChanged="DateTextBox_TextChanged"  Height="50" Background="Beige"/>
    

    Codebedhind:

     private void DateTextBox_TextChanged(object sender, TextChangedEventArgs e)
            {
                if (sender is TextBox textBox)
                {
                    var text = textBox.Text.Replace(".", "");
                    if (text.Length >= 2 && text.Length < 4)
                    {
                        textBox.Text = text.Insert(2, ".");
                        textBox.Select(textBox.Text.Length, 0);
                    }
                    else if (text.Length >= 4)
                    {
                        textBox.Text = text.Insert(2, ".").Insert(5, ".");
                        textBox.Select(textBox.Text.Length, 0);
                    }
                }
            }
    

    The result:

    enter image description here

    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  2. Serkan Sahan 0 Reputation points
    2023-05-17T09:52:22.6033333+00:00

    To automatically add a dot after the user enters the day and month in a specific date format "DD.MM.YYYY" in a WPF TextBox, you can use a combination of event handlers and string manipulation. Here's an example of how you can achieve this:

    1. Add a TextBox control to your WPF project XAML code:

    <TextBox x:Name="DateTextBox" TextChanged="DateTextBox_TextChanged" />

    1. In the code-behind file (e.g., MainWindow.xaml.cs), add the following event handler for the TextChanged event of the TextBox:
    private void DateTextBox_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox textBox = (TextBox)sender;
        string text = textBox.Text.Trim();
    
        // Remove any non-digit characters from the input
        text = Regex.Replace(text, @"\D", "");
    
        if (text.Length >= 3)
        {
            // Insert a dot after the day and month
            text = text.Insert(2, ".");
    
            // If the year is entered, append it after the dot
            if (text.Length > 5)
            {
                text = text.Insert(5, ".");
            }
        }
    
        textBox.Text = text;
        textBox.CaretIndex = text.Length; 
    }
    
    

    In this event handler, we remove any non-digit characters from the input using a regular expression (Regex). Then, we check if the text length is greater than or equal to 3, indicating that the user has entered the day and month. We insert a dot after the second character (day) and, if the year is entered, after the fifth character (month).

    By setting the TextBox's Text property, we update the displayed text with the added dots. Finally, we set the CaretIndex to the end of the text to maintain the cursor position.

    With this implementation, as the user types "01", it will automatically add the dot, and when they enter "02", it will add another dot after "01", resulting in "01.02".


  3. fatih uyanık 225 Reputation points
    2023-05-17T09:53:43.4866667+00:00

    Hi Can it be done with Regex? Or could it be with DateTimeInfo?


  4. fatih uyanık 225 Reputation points
    2023-05-17T10:05:07.0766667+00:00

    Hello

    Serkan, I want to learn one last thing. Can we make this code sensitive to the current culture?

    0 comments No comments

  5. don bradman 621 Reputation points
    2023-05-17T10:10:08.5766667+00:00

    You need to create a new instance of the DateTimeInfo class and specify the format of the date value as "dd.MM.yyyy". This format specifies that the day should be represented by two digits, followed by a dot mark, then the month represented by two digits, followed by another dot mark, and finally the year represented by four digits.

    DateTimeFormatInfo dateTimeFormat = new DateTimeFormatInfo();
    dateTimeFormat.ShortDatePattern = "dd.MM.yyyy";
    

    Next, you can use the PreviewTextInput event of the text box to automatically add the dot marks when the user types the day or the month. In the event handler, you can check the length of the text in the text box and add the dot mark if necessary.

    private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
    {
        TextBox textBox = sender as TextBox;
        int selectionStart = textBox.SelectionStart;
        int selectionLength = textBox.SelectionLength;
    
        string newText = textBox.Text.Remove(selectionStart, selectionLength);
        newText = newText.Insert(selectionStart, e.Text);
    
        if (newText.Length == 2 || newText.Length == 5)
        {
            newText += ".";
            textBox.Text = newText;
            textBox.SelectionStart = selectionStart + 1;
            textBox.SelectionLength = 0;
            e.Handled = true;
        }
    }
    
    

    Finally in your XAML

    <TextBox x:Name="DateTextBox" PreviewTextInput="TextBox_PreviewTextInput"/>

    Hope this is what you want.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.