vb.net中日期时间选择器的验证
我试图找出如何验证DateTimePicker,即,如果DateTimePicker为空。这是密码vb.net中日期时间选择器的验证,vb.net,validation,datetimepicker,Vb.net,Validation,Datetimepicker,我试图找出如何验证DateTimePicker,即,如果DateTimePicker为空。这是密码 Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click If DTDOB.Value = Nothing Then MessageBox.Show("Enter the date") Else
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If DTDOB.Value = Nothing Then
MessageBox.Show("Enter the date")
Else
MessageBox.Show("Date Saved")
End If
End Sub
有人能给我解决办法吗。
提前谢谢。
注意DOTDOB是我的日期时间选择器您的代码可以工作,但更像这样
Dim Input As String = DTDOB.Value.ToString()
Dim dateTime2 As DateTime
If DateTime.TryParse(Input, dateTime2) Then
Console.WriteLine(dateTime2)
Else
Console.WriteLine("Invalid")
End If
您的代码可以工作,但更像这样
Dim Input As String = DTDOB.Value.ToString()
Dim dateTime2 As DateTime
If DateTime.TryParse(Input, dateTime2) Then
Console.WriteLine(dateTime2)
Else
Console.WriteLine("Invalid")
End If
.Net
DateTime
变量,例如DTDOB.Value
,是值类型。这意味着它们永远不能为null
在VB.Net中,与值类型一起使用时,Nothing
关键字将等同于该类型的默认值。对于DateTime
值,这与DateTime.MinValue
或0001年1月1日午夜相同
因此,您可以编写此代码来与
DateTime.MinValue
进行比较,但我认为您所做的更好。如果你的代码有效,我看不出有什么问题。如果它不起作用,请在问题中更好地解释它是如何失败的。.NetDateTime
变量,例如DTDOB.Value
,都是值类型。这意味着它们永远不能为null
在VB.Net中,与值类型一起使用时,Nothing
关键字将等同于该类型的默认值。对于DateTime
值,这与DateTime.MinValue
或0001年1月1日午夜相同
因此,您可以编写此代码来与
DateTime.MinValue
进行比较,但我认为您所做的更好。如果你的代码有效,我看不出有什么问题。如果它不起作用,请在问题中更好地解释它是如何失败的。也许你把问题放错地方了,DateTimePicker控件的值总是与初始化表单时给出的值不同。此值介于MinDate和MaxDate之间。如果试图将该值设置为“无”,则会出现错误。默认情况下,DateTimePicker值是当前日期和时间
一种方法是将DTDOB.Value的值保存在变量或它的标记中,并将其与触发按钮的单击事件时的值进行比较。也许您把问题放错地方了,DateTimePicker控件在初始化表单时始终具有与给定值不同的值。此值介于MinDate和MaxDate之间。如果试图将该值设置为“无”,则会出现错误。默认情况下,DateTimePicker值是当前日期和时间 一种方法是将DTDOB.Value的值保存在变量或它的标记中,并将其与触发按钮的单击事件时的值进行比较。(作者在注释中发布了一小段代码,该注释已被删除)。 您已经将CustomFormat设置为空字符串,以显示尚未选择值,这很好。如果要检查该属性,可以使用
.Text
属性,也可以检查.CustomFormat
是否为“”
这里有一个可能的方法。选择后,更改为其他格式:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DTDOB.CustomFormat = " " 'An empty SPACE
DTDOB.Format = DateTimePickerFormat.Custom
End Sub
Private Sub DTDOB_ValueChanged(sender As Object, e As EventArgs) Handles DTDOB.ValueChanged
' once the user has selected a value, change the format so that a date will be displayed
DTDOB.CustomFormat = "MM/dd/yy"
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If DTDOB.CustomFormat = " " Then
MessageBox.Show("Enter the date")
Else
' ... do something with the Value() property ...
Debug.Print(DTDOB.Value.ToString)
End If
End Sub
(作者在一条评论中发布了一小段代码,后来被删除了。)
您已经将CustomFormat设置为空字符串,以显示尚未选择值,这很好。如果要检查该属性,可以使用.Text
属性,也可以检查.CustomFormat
是否为“”
这里有一个可能的方法。选择后,更改为其他格式:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DTDOB.CustomFormat = " " 'An empty SPACE
DTDOB.Format = DateTimePickerFormat.Custom
End Sub
Private Sub DTDOB_ValueChanged(sender As Object, e As EventArgs) Handles DTDOB.ValueChanged
' once the user has selected a value, change the format so that a date will be displayed
DTDOB.CustomFormat = "MM/dd/yy"
End Sub
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
If DTDOB.CustomFormat = " " Then
MessageBox.Show("Enter the date")
Else
' ... do something with the Value() property ...
Debug.Print(DTDOB.Value.ToString)
End If
End Sub
使其变得简单,不在乎标签,只是为了显示值:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DTDOB.CustomFormat = " " 'An empty SPACE DTDOB.Format = DateTimePickerFormat.Custom
DTDOB.Tag = DTDOB.Value 'Label displays the actual Value
Label1.Text = DTDOB.Tag
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DTDOB.Value = DTDOB.Tag Then
MessageBox.Show("Enter the Date")
Else
MessageBox.Show("Date Saved")
End If
End Sub
End Class
使其变得简单,不在乎标签,只是为了显示值:
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
DTDOB.CustomFormat = " " 'An empty SPACE DTDOB.Format = DateTimePickerFormat.Custom
DTDOB.Tag = DTDOB.Value 'Label displays the actual Value
Label1.Text = DTDOB.Tag
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If DTDOB.Value = DTDOB.Tag Then
MessageBox.Show("Enter the Date")
Else
MessageBox.Show("Date Saved")
End If
End Sub
End Class
Value属性不能为null/nothing,这不需要太多验证。DateTimePicker.Value不能为nothing。请参阅:日期时间值不能为
null
,但可以为Nothing
null
和Nothing
在VB.Net中与值类型一起使用时是不同的。好的,但是如果datepicker为null,我想显示一个消息框。我该怎么办?@MohitKamat你太看重这个了。。。它不会是空的。如果使用Visual Studio设计器,VS将确保控件本身此时不为null。如果控件存在,它将强制执行值
属性介于您设置的MinDate
和MaxDate
之间。此处将存在某种类型的值。value属性不能为null/nothing,这不会留下太多的验证。DateTimePicker.value不能为nothing。请参阅:日期时间值不能为null
,但可以为Nothing
null
和Nothing
在VB.Net中与值类型一起使用时是不同的。好的,但是如果datepicker为null,我想显示一个消息框。我该怎么办?@MohitKamat你太看重这个了。。。它不会是空的。如果使用Visual Studio设计器,VS将确保控件本身此时不为null。如果控件存在,它将强制执行值
属性介于您设置的MinDate
和MaxDate
之间。这里会有某种价值。这效率太低了,一点都不好笑。由于区域性/国际化和时区问题,将日期时间值转换为字符串或从字符串转换为日期时间值的速度非常慢。这效率太低了,一点都不好笑。由于区域性/国际化和时区问题,将日期时间值转换为字符串/从字符串转换日期时间值的速度非常慢。实际上,我已经在DateTimePicker属性本身中设置了最小值和最大值。在这种情况下,您已经得到了DTDOB的保证。值
在该范围内。控件不允许您设置超出范围的值。实际上,我已经在DateTimePicker属性本身中设置了最小值和最大值。在这种情况下,您已经得到了保证DTDOB.value