尝试清除组合框时出现 ArgumentOutOfRangeException
ArgumentOutOfRangeException when trying to clear combobox
我在组合框上尝试 运行.Clear() 时出现以下错误消息:
'System.ArgumentOutOfRangeException' 类型的第一次机会异常发生在 System.Windows.Forms.dll
{"InvalidArgument='-1' 的值对 'index' 无效。
参数名称:index"}
奇怪的是,它在转到应用程序中的新 'page' 之前执行了 .Clear(),并且没有问题。一旦我在 'page' 上,在组合框中绘制项目并尝试转到应用程序的下一个 'page',它再次调用带有 clear 的函数并在到达时爆炸.Clear()。如果我在下面的代码中注释掉 cbo.DrawMode = DrawMode.OwnerDrawFixed 它也会像往常一样 运行s ,所以问题肯定是在组合框中绘制字符串(我正在绘制字符串改变它们的颜色)。无论如何,完全不知道如何解决这个问题,我们将不胜感激。
代码如下:
-我的清除方法
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
组合框 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
这看起来不应该发生,但显然是这样。实际错误可能在 DrawItem 处理程序的这一行中:
Dim text As String = (CType(sender, ComboBox)).Items(e.Index).ToString()
尝试将赋值从 Dim 语句中分离出来,并检查 e.Index 的值以确保它是非负的。如果这是问题所在,您可能可以使用 if 来解决它,以确保 e.Index 是非负数。
我在组合框上尝试 运行.Clear() 时出现以下错误消息:
'System.ArgumentOutOfRangeException' 类型的第一次机会异常发生在 System.Windows.Forms.dll {"InvalidArgument='-1' 的值对 'index' 无效。 参数名称:index"}
奇怪的是,它在转到应用程序中的新 'page' 之前执行了 .Clear(),并且没有问题。一旦我在 'page' 上,在组合框中绘制项目并尝试转到应用程序的下一个 'page',它再次调用带有 clear 的函数并在到达时爆炸.Clear()。如果我在下面的代码中注释掉 cbo.DrawMode = DrawMode.OwnerDrawFixed 它也会像往常一样 运行s ,所以问题肯定是在组合框中绘制字符串(我正在绘制字符串改变它们的颜色)。无论如何,完全不知道如何解决这个问题,我们将不胜感激。
代码如下:
-我的清除方法
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
组合框 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
这看起来不应该发生,但显然是这样。实际错误可能在 DrawItem 处理程序的这一行中:
Dim text As String = (CType(sender, ComboBox)).Items(e.Index).ToString()
尝试将赋值从 Dim 语句中分离出来,并检查 e.Index 的值以确保它是非负的。如果这是问题所在,您可能可以使用 if 来解决它,以确保 e.Index 是非负数。