vb.net 中的 DateTimePicker 验证
Validation of DateTimePicker in vb.net
我正在尝试了解如何验证 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
MessageBox.Show("Date Saved")
End If
End Sub
谁能给我解决方案。
提前致谢。
请注意,DOTDOB 是我的 DateTimePicker
您的代码有效,但像这样更有效。
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
进行比较,但我认为您所表达的意图更好。如果您的代码有效,我认为它没有问题。如果它不起作用,请在问题中更好地解释它是如何失败的。
也许你问错了,DateTimePicker 控件总是有一个 Value 不同于 Form 初始化时给出的 Nothing。该值介于 MinDate 和 MaxDate 之间。如果您尝试将 Value 设置为 Nothing,则会出现错误。默认情况下,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
让它变得简单并且不关心标签,它只是为了显示值:
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
If DateTimePicker1.Value.Date = Date.Today Then
MessageBox.Show("Choose Date From DateTimePicker !")
Else
' Your code
end if
这对我有用,默认情况下 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
MessageBox.Show("Date Saved")
End If
End Sub
谁能给我解决方案。 提前致谢。 请注意,DOTDOB 是我的 DateTimePicker
您的代码有效,但像这样更有效。
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
进行比较,但我认为您所表达的意图更好。如果您的代码有效,我认为它没有问题。如果它不起作用,请在问题中更好地解释它是如何失败的。
也许你问错了,DateTimePicker 控件总是有一个 Value 不同于 Form 初始化时给出的 Nothing。该值介于 MinDate 和 MaxDate 之间。如果您尝试将 Value 设置为 Nothing,则会出现错误。默认情况下,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
让它变得简单并且不关心标签,它只是为了显示值:
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
If DateTimePicker1.Value.Date = Date.Today Then
MessageBox.Show("Choose Date From DateTimePicker !")
Else
' Your code
end if
这对我有用,默认情况下 DateTimePicker
值是今天的日期。
我是编程新手,知识不多,但这是我尝试过的并且对我有用。