In Winforms how do the datepicker cursor walk when typing the date?

Asked

Viewed 292 times

5

The question is about Winforms' Datetimepicker control. When we type the date, instead of choosing the calendar, the cursor does not walk. That is, we typed the two digits of the day, and instead of the cursor passing to the month, it continues on the day. If we keep typing it overwrites the value of the day.

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

I tried to find some property control that enables this, but it doesn’t. I also thought about trying to do this as the Keyup event, but I couldn’t find anything that set the cursor position.

How can I implement this operation in Datetimepicker?

  • When I was asked something like this, I went with the maskedtextbox, but the Datetimepicker is interesting. I’ll wait in case someone’s already done it, otherwise I’ll try to do it

1 answer

1

An outline solution is to use the SendKeys.Send() in the event of ValueChanged. 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();    
        }    
    }    
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.