In WinForms how do the datepicker cursor move when typing the date?

5

The question is about the DateTimePicker control of WinForms. When we enter the date, instead of choosing the calendar, the cursor does not scroll. That is, we enter the two digits of the day, and instead of the cursor move to the month, it continues on the day. If we continue typing, it overwrites the value of the day.

How can I change this behavior so that as I type the day it automatically passes to the month and then to the year? The goal is that when typing the date, enter for example 06042018 directly and it will be 06/04/2018.

I tried to find some control property that enabled this, but it did not. I also thought about trying to do this as the KeyUp event, but could not find anything that would set the cursor position.

How can I implement this behavior in DateTimePicker?

    
asked by anonymous 06.04.2018 / 06:05

1 answer

1

A workaround is to use the SendKeys.Send() feature in the ValueChanged event. So each time the value is changed in one of the properties, the selection will navigate to the next area.

using System;
using System.Windows.Forms;

namespace WindowsFormsApp
{
    public partial class Form1 : Form 
    {   
        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
                SendKeys.Send("{Right}");
        }

        public Form1()
        {
            InitializeComponent();    
        }    
    }    
}
    
25.07.2018 / 17:55