Set focus on date field in DateTimePicker control in Windows form

家住魔仙堡 提交于 2019-12-11 05:37:38

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!