ComboBox上的FindStringExact返回错误的索引

时间:2010-11-22 20:32:13

标签: c# .net combobox

我在WinForm应用程序中有两个ComboBox,它们显示在系统上检测到的COM端口。它们都绑定到包含每个COM端口的字符串的单个数组。通过调用SerialPort.GetPortNames()来填充数组。我为ComboBox创建了新的BindingContexts,以避免它们共享相同数据源的问题,导致其中一个更改影响另一个。

我尝试通过FindStringExact()在ComboBox中搜索此值来选择以前保存的值。

在调试器中运行时,我注意到FindStringExact()返回的索引与该字符串在源数组中的位置不同。

例如(取自立即窗口),这里是ComboBoxes的DataSource

comboBoxController1ComPort.DataSource

{string[0x00000003]}
    [0x00000000]: "COM3"
    [0x00000001]: "COM10"
    [0x00000002]: "COM12"

我原本以为搜索COM3会返回索引0,COM10索引1和COM12索引2.但是,我得到不同的值:

comboBoxController1ComPort.FindStringExact("COM3")
0x00000002

comboBoxController1ComPort.FindStringExact("COM10")
0x00000000

comboBoxController1ComPort.FindStringExact("COM12")
0x00000001

comboBoxController1ComPort.FindStringExact("COM1")
0xffffffff

我无法理解为什么我从FindStringExact()获取不同的索引到DataSource属性返回的索引。有人可以帮忙吗?

由于

1 个答案:

答案 0 :(得分:1)

看起来这些项目是sorted ...