0

我必须根据项目值更改动态创建的 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("");
            }
        }
    }

提前致谢。

4

3 回答 3

1

您可以更改 SelectedIndexChanged 中的颜色,例如:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
   ComboBox cbx = sender as ComboBox;
   if (cbx.Text == "OK")
   {
      cbx.ForeColor = Color.Black;
   }
   else
   {
      cbx.ForeColor = Color.Red;
   }

}

sender 是对受影响的 ComboBox 的引用。

于 2013-05-21T11:27:46.370 回答
1

首先改变你的for

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");

   cb[ii].SelectedIndex = 0;        //"OK" option will be selected
   cb[ii].ForeColor = Color.Black;  //set forecolor to black

   cb[ii].SelectedIndexChanged += ComboBoxSelectedIndexChanged;  
}

接下来添加此事件:

private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox senderComboBox = (ComboBox) sender;

    if (senderComboBox.SelectionLength > 0)
    {
        if (senderComboBox.SelectedItem.ToString() == "OK")
        {
            cb[ii].ForeColor = Color.Black;
        }
        else
        {
            cb[ii].ForeColor = Color.Red;
        }
    }
}

SelectedIndexChanged始终触发,无论索引已被用户或代码更改。如果您只想在用户更改选择时更改前景色,请使用SelectionChangeCommitted

于 2013-05-21T11:30:16.087 回答
0

在@gzaxx 和@JeffRSon 的帮助下,这是工作代码,

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].Items.Add("OK");
            cb[ii].Items.Add("NOT OK");

            cb[ii].SelectedIndex = 0; 
            cb[ii].ForeColor = Color.Black;  
            cb[ii].SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged);

   }
}

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox cbx = sender as ComboBox;
        if (cbx.Text == "OK")
        {
            cbx.ForeColor = Color.Black;
        }
        else
        {
            cbx.ForeColor = Color.Red;
        }
    }

}
于 2013-05-22T04:56:16.083 回答