I have two ComboBoxes in a WinForm application that display COM ports detected on the system. They’re both bound to a single array containing strings for each COM port. The array is populated by a call to SerialPort.GetPortNames(). I created new BindingContexts for the ComboBoxes to avoid the issue with them sharing the same data source causing changes in one to affect the other.
I attempt to select a previously saved value by searching for this value in the ComboBox with FindStringExact().
Whilst running in the debugger, I noticed that the index being returned by FindStringExact() was a different index to where that string is in the source array.
For example (taken from the Immediate Window), here is the DataSource for the ComboBoxes:
comboBoxController1ComPort.DataSource
{string[0x00000003]}
[0x00000000]: "COM3"
[0x00000001]: "COM10"
[0x00000002]: "COM12"
I would have thought that searching for COM3 would have returned index 0, COM10 index 1 and COM12 index 2. However, I get different values:
comboBoxController1ComPort.FindStringExact("COM3")
0x00000002
comboBoxController1ComPort.FindStringExact("COM10")
0x00000000
comboBoxController1ComPort.FindStringExact("COM12")
0x00000001
comboBoxController1ComPort.FindStringExact("COM1")
0xffffffff
I cannot understand why I’m getting different indexes from FindStringExact() to those returned by the DataSource property. Can anyone help?
Thanks
Looks like the items are sorted…