ComboBox.FindString() mystery

headkaze

Newcomer
Joined
Sep 5, 2006
Messages
23
I'm quite confused about the way ComboBox's FindString() method works.

Consider the following example:
C#:
ComboBox myComboBox = new ComboBox();

myComboBox.Items.AddRange(new string[] { "Test1", "Test2", "Test3", "" });

MessageBox.Show(myComboBox.FindString("Test1").ToString());
MessageBox.Show(myComboBox.FindString("Test2").ToString());
MessageBox.Show(myComboBox.FindString("Test3").ToString());
MessageBox.Show(myComboBox.FindString("").ToString());

The first three output as you would expect 0, 1, 2 but the last one output's 0. ***? Who wrote this method anyway. I mean I could understand it returning -1 or something like that but it is returning 0 which is "Test 1" which is not null, and not an empty string "".
 
An empty string is pretty much an edge case scenario as it returns the index of an item that begins with the specified text, and the items are checked in order (or so it appears). Trying to match against an empty string appears to match any entry - then again how often would you expect to find empty strings in a combobox under normal usage?
 
Back
Top