错误值的消息框

时间:2016-03-24 06:17:11

标签: vb.net

我正在vb.net上做一个项目,我必须确定一个月内的天数,以及给定年份是否跳跃。所有编码都完成了。所有我不能做的是编码一个消息框,如果月份不正确,将会出现。

Dim year, days As Double
Dim leap As Boolean
Dim monthint As Integer
Dim monthstr As String

days = 0
leap = False

year = txtTheyear.Text
monthstr = txtThemonth.Text

If monthstr = "January" Then
    monthint = 1
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If


If monthstr = "February" Then
    monthint = 2
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If


If monthstr = "March" Then
    monthint = 3
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "April" Then
    monthint = 4
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "May" Then
    monthint = 5
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "June" Then
    monthint = 6
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "July" Then
    monthint = 7
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "August" Then
    monthint = 8
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "September" Then
    monthint = 9
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "October" Then
    monthint = 10
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "November" Then
    monthint = 11
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)
End If



If monthstr = "December" Then
    monthint = 12
    leap = Date.IsLeapYear(year)
    days = System.DateTime.DaysInMonth(year, monthint)

    Lstbx1.Items.Add(year)
    Lstbx1.Items.Add(leap)
    Lstbx1.Items.Add(days)

End If





With Lstbx1.Items
    .Clear()
    .Add("Year" & vbTab & vbTab & "Is it a leap year" & vbTab & "Month" & vbTab & "Number of days")
    .Add("----" & vbTab & vbTab & "-----------------" & vbTab & "-----" & vbTab & "--------------")
    .Add(txtTheyear.Text & vbTab & vbTab & leap & vbTab & vbTab & txtThemonth.Text & vbTab & days)
End With

我试过一个消息框的编码:

If monthstr= "January" or "February" ...
Then ...
Else MsgBox ("Type the month correctly")
End if

2 个答案:

答案 0 :(得分:1)

需要

If monthstr="January" or monthstr="February" ...

如果你想在IF使用selectPrivate Sub btCreate_Click(sender As Object, e As RoutedEventArgs) Handles btStartButton.Click 'replace constants with values from your textboxes Dim yearPart As String = "2010" Dim monthpart As String = "January" Dim dt As DateTime Try dt = DateTime.ParseExact("01 " + monthpart + " " + yearPart, "dd MMMM yyyy", Globalization.CultureInfo.InvariantCulture) 'MsgBox(dt.Year & " " & dt.Month & " " & Date.IsLeapYear(dt.Year)) Lstbx1.Items.Add(dt.Year) Lstbx1.Items.Add(Date.IsLeapYear(dt.Year))) Lstbx1.Items.Add(dt.Month) Catch ex As Exception MsgBox("Incorrect Entry..! Type the month correctly") End Try End Sub 请看非常幸运的普遍答案

顺便说一句,您也可以用更短的方式编写代码(如果您愿意) 通过简单地使用这样的东西:

SimpleDateFormat

"魔法"发生在MMMM短语中的DateTime.ParseExact的第二个参数,因为这是一种表示您月份的文字表示的格式

答案 1 :(得分:0)

整个场景的简化代码:

    Dim isSuccess As Boolean = True '<-- variable for checking whether the process completed successfully
    Lstbx1.Items().Clear()
    Select Case monthstr
        Case "January", "jan", "january"
            monthint = 1
        Case "February", "feb", "february"
            monthint = 2
        Case "March", "mar", "march"
            monthint = 3
            'Write cases for other months too
        Case "December", "dec", "december"
            monthint = 12
        Case Else
            MsgBox("Incorrect Entry..! Type the month correctly")
    End Select
    'No wdo the operation here;
    If Not isSuccess Then
        leap = Date.IsLeapYear(year)
        days = System.DateTime.DaysInMonth(year, monthint)
    End If
    ' Here you can populate the List

使用这样的优点:

  • 使用select
  • 可以避免检查多个条件
  • 用户可以输入January的三个中的任何一个(&#34;一月&#34;,&#34; jan&#34;,&#34;一月&#34;)以前它只接受January区分大小写。)
  • 正如您在问题中所要求的那样;如果所有其他条件都为false,则可以在case else中显示自定义消息