将datetimepicker与字符串进行比较

时间:2013-05-05 21:09:04

标签: vb.net visual-studio-2010

我是编码和视觉基础的新手。今天我被分配完成了一个我遇到麻烦的程序。我需要开发一个应用程序,允许用户输入约会和需要竞争的时间,但是我需要实施错误检查以确保没有两次是相同的,这是我遇到问题的地方。我不确定如何将datetimepicker.valuelistbox文本进行比较。我收到从string“”到Date类型的转换无效错误。非常感谢任何帮助!

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

1 个答案:

答案 0 :(得分:0)

“我不确定如何将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