尝试清除组合框时ArgumentOutOfRangeException

时间:2015-02-18 21:29:27

标签: vb.net exception combobox

尝试在我的组合框上运行.Clear()时出现以下错误消息:

类型' System.ArgumentOutOfRangeException'的第一次机会异常。发生在System.Windows.Forms.dll中 {" InvalidArgument =' -1'的值对于' index'无效。 参数名称:index"}

奇怪的是,在进入新的'页面之前,它会执行.Clear()。在应用程序中,它没有问题。进入'页面'后,在组合框中绘制项目,然后尝试转到下一页'在应用程序中,它再次调用带有clear的函数,当它到达.Clear()时会爆炸。如果我在下面的代码中注释掉cbo.DrawMode = DrawMode.OwnerDrawFixed它也会正常运行,所以问题肯定是在组合框中绘制字符串(我在绘制字符串以改变它们的颜色) 。无论如何,完全难以理解如何解决这个问题,任何帮助将不胜感激。

代码如下:

- 我清楚的方法

Public Sub ClearCombos()
    'Clear Applicant Combos

    cboPrimary.Items.Clear() 'crashes when it hits this line
    cboJoin1.Items.Clear()
    cboJoin2.Items.Clear()
    cboJoin3.Items.Clear()
    cboJoin4.Items.Clear()
End Sub
  • 在组合框中绘制字符串

    Sub CheckForAgeOverage()
    
    c_applicants = {cboPrimary, cboJoin1, cboJoin2, cboJoin3, cboJoin4}
    
    Dim curdate As Date = Date.Now
    Dim age As Integer
    
    counter = 0
    'Check age of applicants
    For Each cbo As ComboBox In c_applicants
        If CKeyValuePair.GetComboBoxSelectedKey(c_applicants(counter), True) = instApplicant.applicantId Then
            age = Math.Floor(DateDiff(DateInterval.Month, DateValue(instApplicant.BirthDate), curdate) / 12)
            If age >= 70 Then
                overArray.Add(CKeyValuePair.GetComboBoxSelectedValue(c_applicants(counter)))
            End If
            cbo.DrawMode = DrawMode.OwnerDrawFixed
    
        Else
            For Each j As JoinsBU In instJoins
                If CKeyValuePair.GetComboBoxSelectedKey(c_applicants(counter), True) = j.Applicant.applicantId Then
                    age = Math.Floor(DateDiff(DateInterval.Month, DateValue(j.Applicant.BirthDate), curdate) / 12)
                    If age >= 70 Then
                        overArray.Add(CKeyValuePair.GetComboBoxSelectedValue(c_applicants(counter)))
                    End If
                    cbo.DrawMode = DrawMode.OwnerDrawFixed
                End If
            Next
        End If
        counter += 1
    Next
    End Sub
    
  • Comboboxes DrawItem事件:

    Private Sub cbo_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles cboPrimary.DrawItem, cboJoin1.DrawItem, cboJoin2.DrawItem, cboJoin3.DrawItem, cboJoin4.DrawItem
    
    Dim brush As Brush = Brushes.Black
    
    Dim text As String = (CType(sender, ComboBox)).Items(e.Index).ToString()
    counter = 0
    
    For Each s As String In overArray
        If text = overArray(counter) Then
            brush = Brushes.Red
        Else
            brush = Brushes.Black
        End If
        counter += 1
    Next
    
    e.Graphics.DrawString(sender.Items(e.Index).ToString(), e.Font, brush, _
    e.Bounds, StringFormat.GenericDefault)
    counter = 0
    
    End Sub
    

1 个答案:

答案 0 :(得分:1)

看起来这不应该发生,但显然是这样。实际错误可能在DrawItem处理程序的这一行中:

Dim text As String = (CType(sender, ComboBox)).Items(e.Index).ToString()

尝试将分配从Dim语句中分离出来,并检查e.Index的值以确保它是非负的。如果这是问题,你可以用if来解决它,以确保e.Index是非负的。