日期时间挑选下来 [英] Date Time Picker Up Down
问题描述
如何在DateTimePicker中将上/下控制控制为仅几分钟?
我认为有三种方法可以达到此目的。
1.编写自己的TimePicker控件,指定可以更改的内容。这是最难的解决方案,但是当您完全控制更改时最完整。
2.继承DateTimePicker
并覆盖OnValueChanged
,以便所有更改可以改变其他分钟被拒绝的内容。
这样的实现可能如下所示:
公共 类 MyPicker
继承 DateTimePicker
私有上一个作为 DateTime
公共 < span class =code-keyword> Sub New ()
previous = Value
结束 Sub
受保护 覆盖 Sub OnValueChanged(eventargs As System.EventArgs)
如果 previous.Minute = Value.Minute 那么值=之前
之前=值
结束 Sub
结束 类
3.将活动结束正常的DateTimePicker
实例,用于执行解决方案2中描述的逻辑。
可能看起来像这样:
< pre lang =vb> 公共 类 Form1
私有上一个 As DateTime
公共 Sub New ()
InitializeComponent()
previous = DateTimePicker1.Value
结束 Sub
私有 Sub DateTim ePicker1_ValueChanged(发件人作为系统。对象,e As System.EventArgs)句柄 DateTimePicker1.ValueChanged
如果 previous.Minute = DateTimePicker1.Value .Minute 然后 DateTimePicker1.Value =上一个
previous = DateTimePicker1.Value
结束 Sub
结束 Class
请注意,解决方案2和3仍然允许用户选择其他字段,它们是正确的永远不允许改变。这可能让用户觉得不直观,所以我建议寻求解决方案1.
希望这会有所帮助,
Fredrik
How do I control the up/down controls to only minutes in DateTimePicker?
As I see it there are three ways of achieving this.
1. Write your own TimePicker control where you dictate what can be changed. This is the hardest solution but the most complete as you get complete control of the changes.
2. InheritDateTimePicker
and overrideOnValueChanged
in such a way that all changes that change something other that the minute are rejected.
Such an implementation might look something like this:
Public Class MyPicker Inherits DateTimePicker Private previous As DateTime Public Sub New() previous = Value End Sub Protected Overrides Sub OnValueChanged(eventargs As System.EventArgs) If previous.Minute = Value.Minute Then Value = previous previous = Value End Sub End Class
3. Wire up the events on a normalDateTimePicker
instance to do the logic described in solution 2.
That might look something like this:
Public Class Form1 Private previous As DateTime Public Sub New() InitializeComponent() previous = DateTimePicker1.Value End Sub Private Sub DateTimePicker1_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DateTimePicker1.ValueChanged If previous.Minute = DateTimePicker1.Value.Minute Then DateTimePicker1.Value = previous previous = DateTimePicker1.Value End Sub End Class
Note that solution 2 and 3 still allows the user to select other fields, it's jut that they are never allowed to be changed. This might feel un-intuitive to the user so I recommend going for solution 1.
Hope this helps,
Fredrik
这篇关于日期时间挑选下来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!