Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 4th, 2005, 05:45 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default ComboBox BackColor change

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:

Code:
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
 
Old October 5th, 2005, 06:41 AM
Registered User
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old October 5th, 2005, 07:11 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
Code:
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
 
Old October 5th, 2005, 07:56 AM
Registered User
 
Join Date: Oct 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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

 
Old October 6th, 2005, 12:44 AM
Registered User
 
Join Date: Oct 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
change TextBox.BackColor property onFocus event drasko ASP.NET 1.0 and 1.1 Basics 8 January 26th, 2009 12:23 AM
Change Textbox Backcolor for certain records only camehere Access VBA 5 April 28th, 2008 06:28 AM
Change of event using 2 combobox yogeshyl Excel VBA 0 July 31st, 2007 05:02 AM
can I change the forecolor and backcolor of text saffower VS.NET 2002/2003 4 October 11th, 2004 02:38 PM
Dynamic Backcolor change in VB caltito Access VBA 0 August 22nd, 2004 09:16 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.