vb.net - ComboBox FindString Contains -


i have such entries in winforms combobox:

font 8 pt font 9 pt font 10 pt font 12 pt font 14 pt 

then have search string " 9 ".
here native way find index search string without looping?

i try this:

dim integer = mycombobox.findstring(" 9 ", 0) 

... without result.

first, no, there no available method in framework searches sub-string in combobox items , returns index of first item contains search parameter.

but combobox.findstring uses loop can see in source.

so there nothing bad in using one, write extension method 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;         }          (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 can use in way:

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

whoops, vb.net:

public module controlextensions     <system.runtime.compilerservices.extension> _     public function findsubstringindex(combo combobox, substring string, optional comparer stringcomparison = stringcomparison.currentculture) integer         ' sanity check parameters         if combo nothing             throw new argumentnullexception("combo")         end if         if substring nothing             return -1         end if          index integer = 0 combo.items.count - 1             dim obj object = combo.items(index)             if obj nothing                 continue             end if             dim item string = convert.tostring(obj, cultureinfo.currentculture)             if string.isnullorwhitespace(item) andalso string.isnullorwhitespace(substring)                 return index             end if             dim indexinitem integer = item.indexof(substring, comparer)             if indexinitem >= 0                 return index             end if         next          return -1     end function end module 

Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -