vb.net - 如何将今天设置为时间选择器的默认日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8408056/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net - how to set today as a default date for time picker?
提问by user1083597
As the properties Value of the date/time picker does not allow to enter the DateTime.Now default value, I have tried to set it in the code:
由于日期/时间选择器的属性值不允许输入 DateTime.Now 默认值,我尝试在代码中设置它:
Private Sub DataFrom_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DataForm.ValueChanged
DataFrom.Value = DateTime.Now
End Sub
It indeed shows the current date on opening the form with the date/time picker. However one cannot set any other date from the drop down calendar (one can choose a date, what means that the calendar is dropped down allowing to point a date, but after clicking the choice the date/time picker value returns to the current date).
它确实显示了使用日期/时间选择器打开表单时的当前日期。然而,不能从下拉日历中设置任何其他日期(可以选择一个日期,这意味着日历下拉允许指向一个日期,但单击选择后,日期/时间选择器值返回到当前日期) .
Thank you in advance for some indications. Marek
预先感谢您提供一些指示。马立克
采纳答案by Chris
I believe you are setting it in the wrong place. If you are using the "Value Changed" event to set it, it will always change back because you're overriding the value that was just selected...
我相信你把它设置在错误的地方。如果您使用“值已更改”事件来设置它,它将始终更改回来,因为您正在覆盖刚刚选择的值...
You should rather set it in Form Load method where it will default once.
您应该在 Form Load 方法中设置它,它将默认设置一次。
回答by competent_tech
You need to set the value in the Form_Load event:
您需要在 Form_Load 事件中设置值:
Private Sub Form1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
DataFrom.Value = DateTime.Now
End Sub
回答by Hand-E-Food
You want to put that code in Form_Load:
您想将该代码放入Form_Load:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles Me.Load
DataFrom.Value = DateTime.Now
End Sub

