0

我对编码和视觉基础很陌生。今天我被分配去完成一个我遇到问题的程序。我需要开发一个应用程序,允许用户输入约会和需要完成的时间,但是我需要实施错误检查以确保没有两次相同,这就是我遇到问题的地方. 我不确定如何将 adatetimepicker.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
4

1 回答 1

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
于 2013-05-05T21:33:52.553 回答