C#按名称在ComboBox中获取SelectedIndex

时间:2013-10-31 17:45:20

标签: c# winforms combobox

我必须按名称处理一些控件

this.Controls.Find(String.Format("lbl{0}", index),
            true)[0].Text = data[i].ToString();

但是当我尝试按名称获取Combobox时,它无法显示SelectedIndex属性

this.Controls.Find(String.Format("cmbDat{0}", index), true)[0].

我可以做什么?

1 个答案:

答案 0 :(得分:1)

您需要将其投放到ComboBox,因为Find()会返回Control,但不包含SelectedIndex属性。

请改为尝试:

ComboBox theComboBox = this.Controls.Find(String.Format("cmbDat{0}", index), true) as ComboBox;

// Verify the combo box was found before trying to use it
if(theComboBox != null)
{
    // Do whatever you want with the combo box here
    theComboBox.SelectedIndex = ???
    theComboBox.Text = ???
}