2016-03-24 53 views
1

我在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 
+0

你可能會尋找'MsgBox.Show的格式(「類型正確的月份「) – schudel

+0

@Sharon:我添加了一個答案,可以幫助你以簡化的方式做這些事情 –

回答

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 

優點:

  • 可以避免我們檢查多個條件選擇
  • 用戶可以輸入January的三個(「1月」,「jan」,「january」)中的任何一個。以前它只接受January區分大小寫)。
  • 正如您在問題中所要求的;您可以顯示在case else定製的信息會執行,如果所有其他條件潔具假
+0

嗨,非常感謝!它工作時,我正確鍵入月份,當我不消息框確實出現,但如果我點擊好的程序帶我回到編碼和說theres與天= System.DateTime.DaysInMonth(年,monthint)的錯誤 –

+0

它說月份必須在一到十二之間。參數名稱:月 –

+0

@SharonMeyer:我已經更新了該錯誤的解決方案的答案,請看看 –

1

它需要

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

如果wanne使用IF這裏,爲select看到previos答案由非幸運

順便說一句,你還可以在更短的方式(如果你想)通過簡單地使用像這樣的代碼這樣 :

Private 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 

「魔術師」情況與DateTime.ParseExact在MMMM短語的第二個參數,因爲這意味着你每月的字面表示