Set focus on date field in DateTimePicker control in Windows form
I've a DateTimePic开发者_开发知识库ker
control in my code. And I have used a custom format.
this.dateTimePickerDate.CustomFormat = "dd/MM/yyyy";
this.dateTimePickerDate.Value = System.DateTime.Now;
Now the requirement is to focus on the date part of the control. For example if this control shows 30/06/2011, after the form_load, then 30
from the date part should be selected or the cursor will be after the 0
in 30
.
Thanks in advance.
Maybe this code helps:
dateTimePicker1.GotFocus += new EventHandler(dateTimePicker1_GotFocus);
void dateTimePicker1_GotFocus(object sender, EventArgs e)
{
SendKeys.Send("{RIGHT 1}");
}
And on form load set focus to datetimepicker.
As suggested here, I don't think that's possible.
In that link is also suggested a tricky workaround (i.e. programmatically press the arrow to the right when the datetimepicker has got the focus) but that could lead to unexpected side-effects.
精彩评论