Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/amazon-web-services/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 设置组合框的SelectedIndex时ArgumentOutOfRange异常_Vb.net_Winforms_Visual Studio 2010 - Fatal编程技术网

Vb.net 设置组合框的SelectedIndex时ArgumentOutOfRange异常

Vb.net 设置组合框的SelectedIndex时ArgumentOutOfRange异常,vb.net,winforms,visual-studio-2010,Vb.net,Winforms,Visual Studio 2010,错误显示: InvalidArgument=值“6”对于“SelectedIndex”无效 参数名称:SelectedIndex 我不知道那是什么意思。这是我的项目,这是我第一次尝试这个日期到组合框 Private intDaysInMonth(11) As Integer Private Sub cmbMonth_Click() cmbDay.Items.Clear() Call PopulateDays() End Sub Private Function IsLeapY

错误显示:

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

我不知道那是什么意思。这是我的项目,这是我第一次尝试这个日期到组合框

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

正确的答案是使用DateTimePicker,但如果确实必须使用ComboBox,则以下操作应该有效

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

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