簡體   English   中英

屏蔽的文本框導致VB.NET錯誤

[英]Masked textbox causing error in VB.NET

在我的代碼中使用maskedtextbox時,它返回一個異常:

從字符串“”到類型“日期”的轉換無效

我的代碼是:

Dim msg, first, second As String
Dim firstdate, seconddate As Date
first = MaskedTextBox1.Text
second = MaskedTextBox2.Text
firstdate = CDate(first)
seconddate = CDate(second)
msg = "Days from today: " & DateDiff(DateInterval.Month, firstdate, seconddate)
MsgBox(msg)

但是,如果使用文本框代替maskedtextbox,我的代碼可以正常工作:

Dim msg, first, second As String
Dim firstdate, seconddate As Date
first = TextBox3.Text
second = TextBox4.Text
firstdate = CDate(first)
seconddate = CDate(second)
msg = "Days from today: " & DateDiff(DateInterval.Month, firstdate, seconddate)
MsgBox(msg)

最好使用一種解析方法來驗證日期信息:

If DateTime.TryParse(first, firstdate) AndAlso _
   DateTime.TryParse(second, seconddate) Then
  msg = "Days from today: " & DateDiff(DateInterval.Day, firstdate, seconddate)
  MessageBox.Show(msg)
Else
  MessageBox.Show("Invalid dates entered.")
End If

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM