我正在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个项目。 这种行为的原因是什么?