Search This Blog

Monday 21 September 2015

How to change the ForeColor of individual items in a ComboBox in C# Winforms

How to change the ForeColor of individual items in a ComboBox in C# Winforms

//SET COMBOBOXBOX PROPERTY TO DRAWMODE =>Ownerdrawfixed or Ownerdravariable
 private void cmbColour_DrawItem(object sender, DrawItemEventArgs e)
        {
            try
            {    
               YourClassName  presenter =new YourClassName();    
                presenter.DrawColourCode(sender, e);
            }
            catch (Exception ex)
            {
           
                    throw ex;
             
            }
        }

   public void DrawColourCode(object sender, DrawItemEventArgs e)
        {

//HERR I AM USING DATABASE TO FETCH THE COLOURCODE, ,FOREGROUDCOLOUR //AND BACKGROUNDCOLOUR
            float size = 10;
            System.Drawing.Font myFont;
            string backgroundColorCode = Convert.ToString(((sender as ComboBox).Items[e.Index] as ClassEntityName).BackgroundColourCode);
            // Draw the background of the item.
            e.DrawBackground();
            Brush BrsFore = null;
            // Create a square filled with the item color. Vary the size
            // of the rectangle based on the length of the item name.
            Rectangle rectangle = new Rectangle(1, e.Bounds.Top, e.Bounds.Width, e.Bounds.Height);
            if (!string.IsNullOrEmpty(backgroundColorCode))
            {
                Color backgroundcolorCode = HexToColor(backgroundColorCode);
                e.Graphics.FillRectangle(new SolidBrush(backgroundcolorCode), rectangle);
            }
            else
            {
                Color backgroundcolorCode = HexToColor("#FFFFFF");
                e.Graphics.FillRectangle(new SolidBrush(backgroundcolorCode), rectangle);
            }
            // Draw each string in the array, using a different size, color,
            // and font for each item.
            var kitColour = ((sender as ComboBox).Items[e.Index] as ClassEntityName).ColourCode;
            string foreColour = ((sender as ComboBox).Items[e.Index] as ClassEntityName).ForegroundColourCode;
            if (!string.IsNullOrEmpty(foreColour))
            {
                Color foregroundcolorCode = HexToColor(foreColour);
                BrsFore = new SolidBrush(foregroundcolorCode);

                myFont = new Font("Times New Roman", size, FontStyle.Regular);
                if (!string.IsNullOrEmpty(((sender as ComboBox).Name)))
                {
                    e.Graphics.DrawString(kitColour, myFont, BrsFore, new RectangleF(e.Bounds.X + rectangle.Width - 115, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                }
                else
                {
                    e.Graphics.DrawString(kitColour, myFont, BrsFore, new RectangleF(e.Bounds.X + rectangle.Width - 108, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
                }
            }
            // Draw the focus rectangle if the mouse hovers over an item.
            e.DrawFocusRectangle();
        }

 public static Color HexToColor(string hexColor)
        {
            //Remove # if present
            if (hexColor.IndexOf('#') != -1)
                hexColor = hexColor.Replace("#", "");

            int red = 0;
            int green = 0;
            int blue = 0;

            if (hexColor.Length == 6)
            {
                //#RRGGBB
                red = int.Parse(hexColor.Substring(0, 2), NumberStyles.AllowHexSpecifier);
                green = int.Parse(hexColor.Substring(2, 2), NumberStyles.AllowHexSpecifier);
                blue = int.Parse(hexColor.Substring(4, 2), NumberStyles.AllowHexSpecifier);
            }
            else if (hexColor.Length == 3)
            {
                //#RGB
                red = int.Parse(hexColor[0].ToString() + hexColor[0].ToString(), NumberStyles.AllowHexSpecifier);
                green = int.Parse(hexColor[1].ToString() + hexColor[1].ToString(), NumberStyles.AllowHexSpecifier);
                blue = int.Parse(hexColor[2].ToString() + hexColor[2].ToString(), NumberStyles.AllowHexSpecifier);
            }

            return Color.FromArgb(red, green, blue);
        }