问题
I've a DateTimePicker
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.
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/6534459/set-focus-on-date-field-in-datetimepicker-control-in-windows-form