【问题标题】:ArgumentOutOfRangeException when setting SelectedIndex of ComboBox设置 ComboBox 的 SelectedIndex 时出现 ArgumentOutOfRangeException
【发布时间】:2020-02-04 23:16:57
【问题描述】:

错误提示:

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 未处理。

【问题讨论】:

  • 您是否可以在cmbDay.SelectedIndex 填充到PopulateDays 之前设置它?如果cmbDay 中没有任何内容,则SelectedIndex 为6 将无效。您可以在cmbMonth_Click 中设置SelectedIndex。仅供参考,点击事件可能不是最好的(例如,如果有人使用键盘)。还有其他事件,例如 SelectedIndexChangedSelectionChangeCommitted,可能会更好。
  • 另外,你必须使用组合框吗? DateTimePicker 更适合从用户那里获取日期。
  • 哪一行会报错? DateTime 内置了一个静态 IsLeapYear 函数。
  • @JeffBridgman, SelectionChangeCommitted 仅在用户进行选择时引发,因此在这种情况下需要SelectedIndexChanged
  • 该代码看起来像是用 VB6 编写的。您可以在 VB.NET 中对其进行许多改进。例如,当Date 具有与Integer 相同部分的属性时,将Date 的一部分格式化为String 是愚蠢的。您似乎还试图在没有 Handles 子句的情况下处理 Click 事件(这是错误的事件开始)。我猜你复制了网络代码,但并没有真正理解它的作用。

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


【解决方案1】:

正确的答案是使用 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2017-08-21
    • 2011-04-18
    • 1970-01-01
    • 2013-05-20
    • 1970-01-01
    相关资源
    最近更新 更多