C# Combobox items backcolor change

0
29

Introduction

Changing combobx items backcolor which conditions what you want

Using the code

You can Handle DrawItem event and draw items based on its value

Before you begine set DrawMode Property to OwnerDrawFixed.

Blocks of code should be set as style “Formatted” like this:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Comboboxrenklendir
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

 

        private void cmbrenkli_DrawItem(object sender, DrawItemEventArgs e)
        {


           

            Color HighlightColor = Color.Yellow;
                        
            if (e.Index >=0)
            {
              
                
          
                string deger = cmbrenkli.Items[e.Index].ToString();

                if (deger == "30"|| deger == "50")
                {
                    e.Graphics.FillRectangle(new SolidBrush(HighlightColor), e.Bounds);
                }

               

                e.Graphics.DrawString(cmbrenkli.Items[e.Index].ToString(), e.Font, new SolidBrush(cmbrenkli.ForeColor), new Point(e.Bounds.X, e.Bounds.Y));

                e.DrawFocusRectangle();

            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            cmbrenkli.Items.Add("10");
            cmbrenkli.Items.Add("20");
            cmbrenkli.Items.Add("30");
            cmbrenkli.Items.Add("40");
            cmbrenkli.Items.Add("50");
        }
    }
}

History

LEAVE A REPLY