DateTimePicker控件在特定场景下的使用
这个问题与客户为我正在设计的应用程序请求的特定功能有关。 基本上,客户希望 DateTimePicker 在选择日期后提示问题。
这听起来很简单,但是,我很难完成这个简单的任务。
- 如果我提示 OnCloseUp - 键盘输入将不会执行此事件
- 如果我提示 OnValueChanged - 每次日期触发该事件 更改了
- 如果我提示 OnLeave - 事件会稍微触发。 不触发时 例如,单击工具条。 我想避免使用这种方法,因为它仅在用户单击远离控件时触发。
所以基本上,我试图想出在用户从 dateTimePicker 控件中选择日期后提示用户的最佳方法。
我对构建自定义控件也没有任何问题。 我已经开始制作一个,因为我还需要允许 NULL 值。
This question is relating to a specific functionality that a client has requested for an application I am designing. Basically, the client wants the DateTimePicker to prompt a question after the date was selected.
This sounds simple, however, I am having difficulties accomplishing this simple task.
- If I prompt OnCloseUp - Keyboard entries will not execute this event
- If I prompt OnValueChanged - Event fires every time the date is
changed - If I prompt OnLeave - Event fires somewhat. Does not fire when
toolstrip is clicked for example. I would like to avoid this method, as it only fires once the user clicks away from the control.
So basically, I am trying to think of the best way to prompt a user AFTER they select a date from a dateTimePicker control.
I have no issues with building a custom control either. I have started making one since I also needed to allow NULL values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“选择日期”是指:
那么,将
OnCloseUp
和OnValidate
/OnLeave
结合起来怎么样?首先观察
OnValueChanged
事件。 如果发生火灾,请设置一个已更改的标志。如果他们用鼠标选择,您可以使用
OnCloseUp
弹出提示并重置更改的标志。 然后再次观察OnValueChanged
事件。当
OnValidate
或OnLeave
触发时,并且设置了您的标志(可能是在使用键盘更改日期之后),然后调出提示。"Select a date" means:
So, how about a combination of
OnCloseUp
andOnValidate
/OnLeave
?Start by watching for
OnValueChanged
events. Set a changed flag if one fires.If they select with the mouse, you can bring up the prompt with
OnCloseUp
and reset your changed flag. Then watch forOnValueChanged
events again.When
OnValidate
orOnLeave
fires, and your flag is set (presumably after changing the date with the keyboard), then bring up the prompt.我会使用 OnValueChanged 事件。 他们更改值后,提出问题。 如果他们回答错误(示例 - 问:您确定吗?答:否。),则重置日期选择器并将焦点返回到它。
这个例子有点乱,但是很有效。
I would use the OnValueChanged event. After they change the value, ask the question. If they answer wrong (Example - Q: Are you sure? A: No.) then reset the datepicker and return focus to it.
This example is a little messy but it works.
我最终编写了一个自定义控件来以正确的方式处理这个问题。
我创建了一个带有按钮的文本框,并添加了一个 MonthCalendar 控件。
文本框+按钮打开 MonthCalendar 控件。 现在选择日期时间的唯一方法是通过 MonthCalendar。 您无法从文本框中进行选择。 我还创建了一个在选择日期时触发的自定义事件。 它工作完美。 代码如下:
I ended up coding a custom control to handle this the right way.
I created a textbox with a button, and added a MonthCalendar control as well.
The textbox+button opens the MonthCalendar control. The only way to choose the datetime now is via the MonthCalendar. You cannot select from the Textbox. I also created a custom event that fires when the date is selected. It works perfectly. Code below: