none
Disabled combo box RRS feed

  • Question

  • I'm using c# and i have a combo box. I want to disable the combo box and when i do that, the combo box's color is changing. How may i keep that combo box color to white when disabling that combo. Pleas help.
    Tuesday, April 5, 2011 11:08 AM

Answers

  • When win forms control is disabled it's colors will be set to

    • BackgroundColor = SystemColors.Control;
    • ForegroundColor = SystemColors.GrayText;

    These are system colors defined in Windows color scheme. 

    For some controls however it is possible to change back color when it is disabled, for example TextBox:

                textBox1.Enabled = false;
    
                textBox1.BackColor = Color.Green;
    

    Seems ComboBox simply does not support this. If it is absolutely necessary to change the color of combo box when it is disabled, it is possible to employ following technique: replace it with a picture. Here is a working example:

          Bitmap bitmap = new Bitmap (comboBox1.Width,comboBox1.Height);
          comboBox1.DrawToBitmap(bitmap, comboBox1.ClientRectangle);
    
          comboBox1.Enabled = false;
          comboBox1.Visible = false;
    
          PictureBox picture = new PictureBox();
          picture.Image = bitmap;
          picture.Top = comboBox1.Top;
          picture.Left = comboBox1.Left;
          picture.Height = comboBox1.Height;
          picture.Width = comboBox1.Width;
    
          Controls.Add(picture);
    

    These approach has certain drawbacks:

    • Consumes more memory and resources (note: it is better to manually dispose bitmap object, when you no longer need picturebox)
    • Sometimes image could be blurred. You should test it on different settings (screen resolution, color depth, clear type, etc)

    still under construction...
    • Marked as answer by Paul Zhou Wednesday, April 13, 2011 2:23 AM
    Tuesday, April 5, 2011 1:05 PM