我必须根据项目值更改动态创建的 30 个组合框* s * 的前景色和前色。因为,我有组合框的动态数组,我不能这样做......请帮助我。
代码示例:
public partial class Form1 : Form
{
ComboBox[] cb = new ComboBox[28];
private void Form1_Load(object sender, EventArgs e)
{
for (int ii = 0; ii < 28; ii++)
{
cb[ii] = new ComboBox();
cb[ii].Name = "cb"+ii.ToString();
cb[ii].Items.Add("OK");
cb[ii].Items.Add("NOT OK");
if (cb[ii].Items.ToString().Equals("OK"))
{
cb[ii].ForeColor = Color.Black;
}
else
{
cb[ii].ForeColor = Color.Red;
}
}
}
New code:
private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show("Event Raised");
ComboBox senderComboBox = (ComboBox)sender;
if (senderComboBox.SelectionLength > 0)
{
if (senderComboBox.SelectedItem.ToString() == "OK")
{
senderComboBox.Items.Add("");
senderComboBox.ForeColor = Color.Green;
senderComboBox.Items.Remove("");
}
else
{
senderComboBox.Items.Add("");
senderComboBox.ForeColor = Color.Red;
senderComboBox.Items.Remove("");
}
}
}
提前致谢。