vb代码 - 取消消息框

时间:2017-05-05 19:44:32

标签: vb.net

我有这个代码弹出一个消息框,要求用户输入mm / dd / yyyy格式的日期。单击取消时,它表示输入有效日期。如果用户取消该框,则消息框应该停止/消失。但我不知道该怎么做。

以下是代码:

Try
    Dim docDate As String
    Dim batchNumber as String
    Dim dDate As Date
    Dim match As System.Text.RegularExpressions.Match

    'Define the regular expression to check dates
    Dim dateRegex As New System.Text.RegularExpressions.Regex(regExpression)
    'Prompt for the document date
    docDate = Microsoft.VisualBasic.Interaction.InputBox(“Enter the document date (mm/dd/yyyy):”, “Document Date”)
    'Match the input against the regex
    match = dateRegex.Match(docDate)

    'While it doesn't match the regex or a valid date continue to prompt for it
    Do While (Not match.Success Or Not Microsoft.VisualBasic.IsDate(docDate))
        'Alert the user of the problem
        Microsoft.VisualBasic.MsgBox(“Please enter a valid date in the format of mm/dd/yyyy.”)
        'Prompt for the document Date
        docDate = Microsoft.VisualBasic.Interaction.InputBox(“Enter the document date (mm/dd/yyyy):”, “Document Date”)
        'Match the input against the regex
        match = dateRegex.Match(docDate)
    Loop

    'Store the input dates in Date datatypes
    dDate = CDate(docDate)
    'Set the value into the global variables
    GBL_DOCUMENTDATE = dDate
    GBL_BATCH = batchNumber

Catch ex As Exception
    If (mapInterface = 1) Then
        Messagebox.Show(ex.Message, “DateInputTemplate Script Error”)
    End If
    Return False
End Try

2 个答案:

答案 0 :(得分:2)

If MessageBox.Show("Please enter a valid date in the format of mm/dd/yyyy.", "Question", MessageBoxButtons.OKCancel) = DialogResult.OK
    docDate = Microsoft.VisualBasic.Interaction.InputBox(“Enter the document date (mm/dd/yyyy):”, “Document Date”)
Else
    'Do nothing? Get rid of the else statement if you don't need it
End If

这样会弹出一个带有按钮OK和Cancel的消息框,如果用户单击OK,则会进入If语句块中的代码。

答案 1 :(得分:-1)

我将如何做到这一点。

    Dim valid as boolean
    Do
        valid = true
        docDate = Microsoft.VisualBasic.Interaction.InputBox(“Enter the document date (mm/dd/yyyy):”, “Document Date”)
        match = dateRegex.Match(docDate)
        If (docDate = "")
            'they clicked cancel leave the loop
            docDate = Date.Now().toString("MM\/dd\/yyyy")
            Exit Do
        ElseIf (Not match.Success Or Not Microsoft.VisualBasic.IsDate(docDate)) Then
            valid = false
            Microsoft.VisualBasic.MsgBox(“Please enter a valid date in the format of mm/dd/yyyy.”)
        End If
    Loop until valid

因此会要求输入文件日期。如果按下取消而不是输入日期并按下docDate变为空字符串,则if语句将检查此值并将docDate设置为今天的日期。 如果他们输入了一个日期并且输入的日期不是有效日期,那么将出现msgbox并且循环将再次开始并询问文档日期。