设置ComboBox的SelectedIndex时出现ArgumentOutOfRangeException

时间:2019-10-07 16:07:23

标签: vb.net winforms visual-studio-2010

错误提示:

  

InvalidArgument =值'6'对于'SelectedIndex'无效   参数名称:SelectedIndex

我不知道那是什么意思。这是给我的项目的,这是我第一次将此日期尝试到ComboBox上。

Private intDaysInMonth(11) As Integer

Private Sub cmbMonth_Click()
    cmbDay.Items.Clear()
    Call PopulateDays()
End Sub

Private Function IsLeapYear(ByVal intYear As Integer) As Boolean
    IsLeapYear = IsDate("29/02/" & intYear)
End Function

Private Sub PopulateDays()
    If cmbMonth.SelectedIndex = -1 Then
        If IsLeapYear(cmbYear.Text) Then
            intDaysInMonth(1) = 29
        Else
            intDaysInMonth(1) = 28
        End If
    End If
    For intI = 1 To intDaysInMonth(cmbMonth.SelectedIndex)
        cmbDay.Items.Add(CStr(intI))
    Next intI
End Sub

Private Sub cmbYear_Click()
    Call PopulateDays()
End Sub

Private Sub Form_Load()
    Dim intI As Integer
    cmbDay.Items.Clear()
    cmbMonth.Items.Clear()
    cmbYear.Items.Clear()
    For intI = 0 To 11
        intDaysInMonth(intI) = 31
        cmbMonth.Items.Add(Format(CDate("01/" & intI + 1 & "/2011"), "mmmm"))
    Next intI
    intDaysInMonth(1) = 28
    intDaysInMonth(3) = 30
    intDaysInMonth(5) = 30
    intDaysInMonth(8) = 30
    intDaysInMonth(10) = 30
    For intI = 1959 To 2019
        cmbYear.Items.Add(CStr(intI))
    Next intI
    cmbMonth.SelectedIndex = Format(Now, "MM") - 1
    cmbYear.SelectedIndex = Format(Now, "yyyy") - 1959
    cmbDay.SelectedIndex = Format(Now, "dd") - 1
End Sub

预期的输出必须在ComboBox中显示月/日/年,但是程序运行时会显示未处理ArgumentOutOfRangeException。

1 个答案:

答案 0 :(得分:0)

正确的答案是使用DateTimePicker,但如果您确实必须使用ComboBoxes,则应该可以使用以下功能。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    FillCombo(1959, 61, ComboBox1) 'Year combo
    FillCombo(1, 12, ComboBox2) ' Month combo
End Sub

Private Sub FillCombo(StartNumber As Integer, NumberOfItems As Integer, c As ComboBox)
    'Enumerable.Range(starNumber, number of items)
    Dim items As Integer() = Enumerable.Range(StartNumber, NumberOfItems).ToArray
    Dim strItems As String()
    '.AddRange takes an array of objects and will not covert a value type to a reference type
    'Therefore the conversion to an array of String which is a reference type
    If c.Name = "ComboBox2" Then
        'This is an example of lambda expresions used with Linq
        strItems = items.Select(Function(month) CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(month)).ToArray
    Else
        strItems = items.Select(Function(yr) CStr(yr)).ToArray()
    End If
    c.Items.AddRange(strItems)
End Sub

Private Sub ComboBox2_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox2.SelectionChangeCommitted
    Dim NumberOfDays As Integer
    Select Case ComboBox2.SelectedItem.ToString
        Case "September", "April", "June", "November"
            NumberOfDays = 30
        Case "January", "March", "May", "July", "August", "October", "December"
            NumberOfDays = 31
        Case "February"
            If DateTime.IsLeapYear(CInt(ComboBox1.Text)) Then
                NumberOfDays = 29
            Else
                NumberOfDays = 28
            End If
    End Select
    FillCombo(1, NumberOfDays, ComboBox3)
End Sub

Private Sub ComboBox3_SelectionChangeCommitted(sender As Object, e As EventArgs) Handles ComboBox3.SelectionChangeCommitted
    Dim dateString = $"{ComboBox2.Text}  {ComboBox3.SelectedItem}, {ComboBox1.Text}"
    Label1.Text = dateString
End Sub