Subject: ComboBox BackColor change
Posted By: LoorD Post Date: 10/4/2005 5:45:03 AM
Hi all!

I have a piece of code that should change the backgorund of a ComboBox to different colors when enabled/disabled.

I do this in the EnabledChanged event of the ComboBox like this:

private void CmbTest_EnabledChanged(object sender, System.EventArgs e)
{
  if(cb.Enabled)
    CmbTest.BackColor = Color.FromKnownColor(KnownColor.Window);
  else
    CmbTest.BackColor = Color.LightYellow;
}


But that code doesn't really change the ComboBox's background to light yellow when disabled but it's overrideen somehow to use the default disabled color. How can I get the yellow background to show in a ComboBox control?

Thank you in advance!


Arto Kainu
Joensuu, Finland
Reply By: HosamSalem Reply Date: 10/5/2005 6:41:42 AM
Dear Friend,

First of all, i think the problem is that you want to act to the event of changing the Enabled/Disabled state
of the combobox (which is changed in the design time) using the event "ComboBox_EnabledChanged"
(which is executed in the runtime), so you got now the reason of the problem?
So, what is the turn around to overcome this problem?
You can so the following,

You can override the event "comboBox1_Layout", and write the following code:

private void comboBox1_Layout(object sender, System.Windows.Forms.LayoutEventArgs e)
{
    if(comboBox1.Enabled)
    {
        comboBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
    }
    else
    {
        comboBox1.BackColor = Color.Yellow;
    }
}

As a result, when the combobox is loaded on the form, this event check its state,
and it turns its backcolor according to this state

Try it and fell free to send your comments

Hope this helps

Reply By: LoorD Reply Date: 10/5/2005 7:11:28 AM
Thank you HosamSalem, but I already knew that. I really want to react to the enabled/dilsabled event during runtime, not design time.

But the syntax
comboBox1.BackColor = Color.Yellow;
doesn't change the color to the given color when the combobox is disabled. For some reason the combobox chooses to use the default disabled color even if I set the BackColor property to the color I want.

Arto Kainu
Joensuu, Finland
Reply By: HosamSalem Reply Date: 10/5/2005 7:56:38 AM
Hi Arto Kainu,

I have tried the following,

I have put a combobox and a buttom on a form for testing with the following code:

private void btnEnableDisable_Click(object sender, System.EventArgs e)
{
    comboBox1.Enabled = !comboBox1.Enabled;
}
private void comboBox1_EnabledChanged(object sender, System.EventArgs e)
{
    if(comboBox1.Enabled)
    {
        comboBox1.BackColor = SystemColors.Window;
    }
    else
    {
        comboBox1.BackColor = Color.Yellow;
    }
}
And as a result, the back color of the combobox is changed successfully after i click on the button.

I think you do the same thing but really don't figure out y it dosn't work :(

Hope this helps

Reply By: LoorD Reply Date: 10/6/2005 12:44:51 AM
First thanks for your time HosamSalem!

I just wondered that the control is in a GroupBox and I actually disable the group box and when it's disabled, also the ComboBox is disabled. Could that override the color somehow? Still all my TextBoxes in the same GroupBox use the correct disable color when disabled.

This is weird. Well, I have to satisfy with the current color. It's not that important.

Thanks again for your time, HosamSalem!

Arto Kainu
Joensuu, Finland

Go to topic 35449

Return to index page 459
Return to index page 458
Return to index page 457
Return to index page 456
Return to index page 455
Return to index page 454
Return to index page 453
Return to index page 452
Return to index page 451
Return to index page 450