DateTimePicker焦点行为问题 [英] DateTimePicker focus behavior question

查看:640
本文介绍了DateTimePicker焦点行为问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以Windows形式使用DateTimePicker,并且遇到了一些奇怪的行为.我将格式"设置为短",而"ShowUpDown"设置为真",但否则控件上没有任何特殊设置.我的测试人员已向我报告,并且我已经确认,如果他们编辑一个MM/DD/YYYY日期的字段,段,日期部分(无论其名称如何),则该字段将被突出显示并随时具有焦点焦点重新回到该DateTemePicker控件,即使是在新记录(其中Value属性已更改的地方)上也是如此.例如:

1)我本来的日期是"2011年2月13日";
2)我将该日期更改为"2/12/2011",然后跳出而不移至"2011"部分;
3)保存记录;
4)添加一条新记录,将值"重置为"2/13/2011";
5)Tab进入控件;
6)该字段的"13"(日期)部分突出显示并具有焦点.

我在这里找到 http://www.eggheadcafe.com/software/aspnet/31385087/location -in-datetimepicker.aspx [ ^ ],DateTimePicker不会公开突出显示的内容的位置,因此我无法以编程方式进行设置.很好,除了我真的希望焦点集中在月份部分,而不是最后一次编辑的内容.

除了处置和重新实例化控件之外,其他有关如何执行此操作的建议?

I am trying to use a DateTimePicker in a Windows form, and have come across some odd behavior. I have the Format set to "Short", and ShowUpDown set to "True", but otherwise there is nothing special set on the control. My testers have reported to me, and I''ve confirmed, that if they edit one field, segment, datepart, whatever its called, of a MM/DD/YYYY date, that same field will be highlighted and have the focus any time the focus comes back around to that DateTemePicker control, even on new records (where the Value property has been changed). For example:

1) I originally have the date "2/13/2011";
2) I change that date to "2/12/2011", and Tab out without moving to the "2011" part;
3) Save the record;
4) Add a new record, which resets the Value to "2/13/2011";
5) Tab into the control;
6) The "13" (date) part of the field is highlighted and has focus.

I found here http://www.eggheadcafe.com/software/aspnet/31385087/location-in-datetimepicker.aspx[^] that DateTimePicker does not expose the location of what is highlighted, so that is not something I can programmatically set. This is fine, except that I really want the focus to be on the month part rather than whatever happened to have been edited last.

Any suggestions on how to do this other than by disposing of and reinstantiating the control?

推荐答案

我过去曾遇到过这种情况.尽管您可以发送鼠标或键盘消息来解决此问题,但是如果您不介意使用丑陋的hack,但可以使用的话,则要容易一些.
I have run into this in the past. While you can send mouse or keyboard messages to resolve this, it''s a little easier if you don''t mind using what is an ugly hack, but one that works.

var format = dateTimePicker1.Format;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker1.Format = format;



诀窍是通过更改Format 然后将其设置回原来的值来强制控件在内部进行自身重置.上面的代码假定它不是Custom.



The trick is to force the control to internally reset itself by changing the Format and then setting it back to what it was. The code above assumes that it is something other than Custom.


这篇关于DateTimePicker焦点行为问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆