在 ComboBoxCell.Items 集合中使用 IndexOf 在不区分大小写的比较中引发异常

问题描述

我没有为我的问题找到任何好的解决方案。一点解释。我有带有几列的 DataGridView。在第一列中,单元格显示某个文件的路径。从第三列开始,我有几个 ComboBoxCells。

在组合框的下拉菜单中,我希望自动选择与第一个单元格中的文件路径部分相等(不区分大小写)的值。所有这一切都发生在 Form.Load
之后,用户做出自己的选择。

Dim Cmbcell As DataGridViewComboBoxCell = CType(DgvRow.Cells(2),DataGridViewComboBoxCell)
Dim ResultString As String
If HasDropDownThisValue(DgvRow.Cells(0).Value.ToString,Cmbcell,ResultString) Then
    Cmbcell.Value = Cmbcell.Items(Cmbcell.Items.IndexOf(ResultString))
End If

这是If语句中使用的方法

Private Function HasDropDownThisValue(ByVal GivenValue As String,ByRef comboCeel As DataGridViewComboBoxCell,ByRef ReturnValue As String) As Boolean
    ReturnValue = ""
    Dim boolret As Boolean = False
    Dim comparestring As String
    Dim StringArr() As String = Split(GivenValue,CStr(Path.DirectorySeparatorChar),-1,VisualBasic.CompareMethod.Text)
    comparestring = StringArr(0)
    For i As Integer = 1 To UBound(StringArr)

        If comboCeel.Items.Cast(Of String).Contains(comparestring,StringComparer.OrdinalIgnoreCase) Then
            ReturnValue = comparestring
            boolret = True
            Exit For
        Else
            comparestring = String.Format("{0}{1}{2}",comparestring,Path.DirectorySeparatorChar,StringArr(i))
        End If
    Next
    Return boolret
End Function

行:

Cmbcell.Value = Cmbcell.Items(Cmbcell.Items.IndexOf(ResultString))

当大小写不匹配时抛出异常。如何避免?
我可能可以执行以下操作:Cmbcell.Value = ResultString,但我更喜欢在 Items 集合中预定义值。

也许这样的事情是可能的(在函数中使用):

comboCeel.Items.Cast(Of String).Contains(comparestring,StringComparer.OrdinalIgnoreCase)

解决方法

由于您需要不区分大小写的匹配,您可以使用 IndexOf(),将 StringComparison 选项设置为 StringComparison.OrdinalIgnoreCase,在 DataGridViewComboBoxCell.ObjectCollection (在这种情况下,它表示字符串的集合)。

我建议在 Dim ResultString As String = Nothing 方法中设置 String.Empty 而不是将其重置为 "" (HasDropDownThisValue):如果该方法返回 False,您然后设置[ComboBoxCell].Value = ResultString(即Nothing),这样组合框选择被清除。
否则,使用 IndexOf() 在集合中查找匹配的字符串。尽管如此,如果没有找到匹配项,结果值将是 Nothing,因此 ComboBox 被清除;否则设置找到的值:

(局部变量重命名以符合标准命名约定)

Dim resultString As String = Nothing
Dim cmbCell = CType(DgvRow.Cells(2),DataGridViewComboBoxCell)

If HasDropDownThisValue(DgvRow.Cells(0).Value?.ToString(),cmbCell,resultString) Then
    resultString = cmbCell.Items.OfType(Of String).
        FirstOrDefault(Function(i) i.IndexOf(resultString,StringComparison.OrdinalIgnoreCase) >= 0)
End If
cmbCell.Value = resultString

如您所见,将 resultString 作为引用传递变得非常无用。您可以更改方法以返回字符串而不是布尔值,因为无论如何我们都需要使用该字符串(无论 HasDropDownThisValue 是否找到匹配项)。

另外,你不应该传递 ComboBox Cell ByRef:这个对象已经是一个引用类型,你应该传递它 ByVal(或者不指定修饰符,因为 {{1} } 是默认值)。