vb.net 组合框 FindString 包含

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26904345/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:27:59  来源:igfitidea点击:

ComboBox FindString Contains

vb.net

提问by Wine Too

I have such entries in Winforms ComboBox:

我在 Winforms ComboBox 中有这样的条目:

Font 8 pt
Font 9 pt
Font 10 pt
Font 12 pt
Font 14 pt

Then I have search string " 9 ".
Is here native way to find index by search string without looping?

然后我有搜索字符串“9”。
这是通过搜索字符串查找索引而不循环的本地方法吗?

I try this:

我试试这个:

Dim a As Integer = myComboBox.FindString(" 9 ", 0)

... but without good result.

......但没有好的结果。

回答by Tim Schmelter

First, no, there is no available method in the framework that searches for sub-string in combobox items and returns the index of the first item which contains the search parameter.

首先,不,框架中没有可用的方法来搜索组合框项目中的子字符串并返回包含搜索参数的第一个项目的索引。

But even ComboBox.FindStringuses a loop as you can see in the source.

但即使ComboBox.FindString使用循环,正如您在源代码中看到的那样

So there is nothing bad in using one, you could write an extension method for this:

所以使用一个没有什么不好的,你可以为此编写一个扩展方法:

public static class ControlExtensions
{
    public static int FindSubStringIndex(this ComboBox combo, string subString, StringComparison comparer = StringComparison.CurrentCulture)
    {
        // Sanity check parameters
        if(combo == null) throw new ArgumentNullException("combo");
        if (subString  == null) {
            return -1;
        }

        for (int index = 0; index < combo.Items.Count; index++)
        {
            object obj = combo.Items[index];
            if(obj == null) continue;
            string item = Convert.ToString(obj, CultureInfo.CurrentCulture);
            if (string.IsNullOrWhiteSpace(item) && string.IsNullOrWhiteSpace(subString))
                return index;
            int indexInItem = item.IndexOf(subString, comparer);
            if (indexInItem >= 0)
                return index;
        }

        return -1;
    }
}

Now you can use it in this way:

现在你可以这样使用它:

int index = combo.FindSubStringIndex("9");

Whoops, VB.NET:

哎呀,VB.NET:

Public Module ControlExtensions
    <System.Runtime.CompilerServices.Extension> _
    Public Function FindSubStringIndex(combo As ComboBox, subString As String, Optional comparer As StringComparison = StringComparison.CurrentCulture) As Integer
        ' Sanity check parameters
        If combo Is Nothing Then
            Throw New ArgumentNullException("combo")
        End If
        If subString Is Nothing Then
            Return -1
        End If

        For index As Integer = 0 To combo.Items.Count - 1
            Dim obj As Object = combo.Items(index)
            If obj Is Nothing Then
                Continue For
            End If
            Dim item As String = Convert.ToString(obj, CultureInfo.CurrentCulture)
            If String.IsNullOrWhiteSpace(item) AndAlso String.IsNullOrWhiteSpace(subString) Then
                Return index
            End If
            Dim indexInItem As Integer = item.IndexOf(subString, comparer)
            If indexInItem >= 0 Then
                Return index
            End If
        Next

        Return -1
    End Function
End Module