Vb.net 将datetimepicker与字符串进行比较
我对编码和VisualBasic相当陌生。今天我被分配去完成一个我遇到麻烦的项目。我需要开发一个应用程序,允许用户输入约会和需要竞争的时间,但是我需要执行错误检查以确保没有两次是相同的,这就是我遇到问题的地方。我不确定如何将Vb.net 将datetimepicker与字符串进行比较,vb.net,visual-studio-2010,Vb.net,Visual Studio 2010,我对编码和VisualBasic相当陌生。今天我被分配去完成一个我遇到麻烦的项目。我需要开发一个应用程序,允许用户输入约会和需要竞争的时间,但是我需要执行错误检查以确保没有两次是相同的,这就是我遇到问题的地方。我不确定如何将datetimepicker.value与列表框文本进行比较。我正在获取从字符串“”到键入日期的转换无效错误。非常感谢您的帮助 Public Class Form1 Function TimeTaken() As Boolean Dim app As
datetimepicker.value
与列表框
文本进行比较。我正在获取从字符串
“”到键入日期
的转换无效错误。非常感谢您的帮助
Public Class Form1
Function TimeTaken() As Boolean
Dim app As String = TextBox1.Text
Dim timeofapp As String = DateTimePicker1.Value.ToShortTimeString
If CDate(ListBox2.Text) = CDate(DateTimePicker1.Value) Then
MsgBox("Two appointments are scheduled within the same time frame.", MsgBoxStyle.Exclamation)
TimeTaken = True
Else
TimeTaken = False
ListBox1.Items.Add(app)
ListBox2.Items.Add(timeofapp)
TextBox1.Text = ""
End If
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TimeTaken()
End Sub
End Class
“我不确定如何将datetimepicker.value与列表框文本进行比较”
您需要迭代ListBox.Items()属性中存储的所有值:
Function TimeTaken() As Boolean
Dim AlreadyTaken As Boolean = False ' assume not taken until proven otherwise below
Dim app As String = TextBox1.Text
Dim timeofapp As String = DateTimePicker1.Value.ToShortTimeString
For Each time As String In ListBox2.Items
If time = timeofapp Then
MsgBox("Two appointments are scheduled within the same time frame.", MsgBoxStyle.Exclamation)
AlreadyTaken = True
Exit For
End If
Next
If Not AlreadyTaken Then
ListBox1.Items.Add(app)
ListBox2.Items.Add(timeofapp)
TextBox1.Text = ""
End If
Return AlreadyTaken
End Function
令人惊叹的。如果该代码解决了您的问题,请随意单击复选标记,将该答案更改为“已接受”答案。