2017-05-05 63 views
0

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

下面的代码:

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 
+3

使用DateTimePicker控件,你不必验证任何东西。请阅读[问]并参加[游览]。 – Plutonix

+2

...东西,东西,正则表达式,现在你有两个问题... – LarsTech

+0

我建议阅读精美的文档,它会向你解释当用户选择“取消”时InputBox的返回值。然后,相应地调整循环中的条件。 – Craig

回答

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 

所以这会弹出按钮确定和取消一个消息框,如果用户单击确定,然后继续在If语句块中的代码。

-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将会出现并且循环将再次开始并询问文档日期。