Winforms ComboBox奇怪的行为

时间:2017-11-13 20:39:39

标签: winforms debugging combobox

我正在DataSource设置ComboBox

 comboBox.DataSource = new string[2]{"a", "b"};

然后我设置了SelectedIndex

comboBox.SelectedIndex = 1;

它抛出异常:InvalidArgument=Value of '1' is not valid for 'SelectedIndex'.
确切的代码:

ComboBox comboBox = new ComboBox
{
    DropDownStyle = ComboBoxStyle.DropDownList,
    Left = currentValueLeft,
    Top = currentTop + height + DEFAULT_MARGIN,
    Height = txtvalue.Height,
    Width = txtvalue.Width,
    FlatStyle = FlatStyle.Flat
};
comboBox.DataSource = new string[2] { "a", "b" };
comboBox.SelectedIndex = 1;
pnall.Controls.Add(comboBox);

我调试并意识到调试器显示我有0个项目。 这种行为的原因是什么?

0 个答案:

没有答案