0

我是 Visual Basic 的新手,我需要一些关于我正在开发的程序的帮助。

我有 3 个单独的文本框,分别代表日、月和年,并且没有接受任何其他字符,然后是数字。现在我想编写一个代码,以便程序每次检查当前日期并提醒您输入的日期是否已过期,例如输入 D: 11 M: 11 Y: 2012。

有人能帮我一下吗?

4

1 回答 1

0

航空代码。

Dim theYear As Integer, theMonth As Integer, theDay As Integer 
' get text from textboxes and convert to numbers 
If Not Int32.TryParse(txtYear.Text, theYear) Then 
  MsgBox "Please enter a valid year!"
  Exit Sub
End If
If Not Int32.TryParse(txtMonth.Text, theMonth) Then 
  MsgBox "Please enter a valid month!"
  Exit Sub
End If
If Not Int32.TryParse(txtDay.Text, theDay) Then 
  MsgBox "Please enter a valid day!"
  Exit Sub
End If  
' combine numbers into a Date 
Dim theDate As Date = Nothing 
Try
  theDate As New Date(theYear, theMonth, theDay) 
Catch e As Exception 
   'year is less than 1 or greater than 9999. 
  '-or- 
  'month is less than 1 or greater than 12. 
  '-or- 
  'day is less than 1 or greater than the number of days in month. 
  MsgBox "Please enter a valid date!"
  Exit Sub
End Try 
 ' check whether date is in the past 
Dim nowDate As Date = DateTime.Now
If DateTime.Compare(theDate, nowDate) < 0 Then
  MsgBox "That date has expired" 
End If 

文档链接:

于 2012-11-13T13:58:00.340 回答