我必须按他们的名字处理一些控件
this.Controls.Find(String.Format("lbl{0}", index),
true)[0].Text = data[i].ToString();
但是当我尝试按名称获取组合框时,它无法显示 SelectedIndex 属性
this.Controls.Find(String.Format("cmbDat{0}", index), true)[0].
我能为谁做?
您需要将其转换为 a ComboBox
,因为Find()
返回 a 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 = ???
}