OwnerDraw ComboBox : ComboBox « GUI Windows Forms « C# / CSharp Tutorial






using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

public class Form1 : System.Windows.Forms.Form {
    private System.Windows.Forms.ComboBox comboBox1 = new System.Windows.Forms.ComboBox();
    private System.Windows.Forms.Label label1 = new System.Windows.Forms.Label();
    private System.Windows.Forms.ComboBox comboBox2 = new System.Windows.Forms.ComboBox();
    private System.Windows.Forms.Label label2 = new System.Windows.Forms.Label();
    ArrayList colorArray = new ArrayList();
    ArrayList fontArray = new ArrayList();
    public Form1() {
        this.SuspendLayout();

        this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable;
        this.comboBox1.ItemHeight = 25;
        this.comboBox1.Location = new System.Drawing.Point(16, 40);
        this.comboBox1.Size = new System.Drawing.Size(264, 31);
        this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
        this.comboBox1.MeasureItem += new System.Windows.Forms.MeasureItemEventHandler(this.comboBox1_MeasureItem);
        this.comboBox1.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);

        this.label1.Location = new System.Drawing.Point(16, 16);
        this.label1.Size = new System.Drawing.Size(100, 16);
        this.label1.Text = "Font Combo Box";

        this.comboBox2.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
        this.comboBox2.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
        this.comboBox2.ItemHeight = 20;
        this.comboBox2.Location = new System.Drawing.Point(16, 104);
        this.comboBox2.Size = new System.Drawing.Size(264, 26);
        this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected);
        this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox2_DrawItem);

        this.label2.Location = new System.Drawing.Point(24, 80);
        this.label2.Text = "Color Combo Box";

        this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
        this.ClientSize = new System.Drawing.Size(312, 157);
        this.Controls.AddRange(new System.Windows.Forms.Control[] {
                                      this.label2,
                                      this.label1,
                                      this.comboBox1,
                                      this.comboBox2});
        this.Load += new System.EventHandler(this.Form1_Load);
        this.ResumeLayout(false);

    }
    [STAThread]
    static void Main() {
        Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e) {
        colorArray.Add(new SolidBrush(Color.Yellow));
        colorArray.Add(new SolidBrush(Color.Black));
        colorArray.Add(new SolidBrush(Color.Azure));
        colorArray.Add(new SolidBrush(Color.Firebrick));
        colorArray.Add(new SolidBrush(Color.DarkMagenta));

        comboBox2.Items.Add("A");
        comboBox2.Items.Add("B");
        comboBox2.Items.Add("C");
        comboBox2.Items.Add("D");
        comboBox2.Items.Add("E");

        fontArray.Add(new Font("Ariel", 15, FontStyle.Bold));
        fontArray.Add(new Font("Courier", 12, FontStyle.Italic));
        fontArray.Add(new Font("Veranda", 14, FontStyle.Bold));
        fontArray.Add(new Font("System", 10, FontStyle.Strikeout));
        fontArray.Add(new Font("Century SchoolBook", 15, FontStyle.Underline));

        comboBox1.Items.Add("W");
        comboBox1.Items.Add("H");
        comboBox1.Items.Add("P");
        comboBox1.Items.Add("D");
        comboBox1.Items.Add("L");

    }

    private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
        Graphics g = e.Graphics;
        Rectangle r = e.Bounds;
        Font fn = null;

        if (e.Index >= 0) {
            fn = (Font)fontArray[e.Index];
            string s = (string)comboBox1.Items[e.Index];
            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), r);

            if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
                e.Graphics.DrawString(s, fn, new SolidBrush(Color.Black), r, sf);
                e.DrawFocusRectangle();
            } else {
                e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
                e.Graphics.DrawString(s, fn, new SolidBrush(Color.Red), r, sf);
                e.DrawFocusRectangle();
            }
        }
    }

    private void comboBox2_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) {
        Graphics g = e.Graphics;

        Rectangle r = e.Bounds;

        if (e.Index >= 0) {
            Rectangle rd = r;
            rd.Width = 100;

            Rectangle rt = r;

            SolidBrush b = (SolidBrush)colorArray[e.Index];
            g.FillRectangle(b, rd);

            StringFormat sf = new StringFormat();
            sf.Alignment = StringAlignment.Near;

            Console.WriteLine(e.State.ToString());
            e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black), 2), r);

            if (e.State == (DrawItemState.NoAccelerator | DrawItemState.NoFocusRect)) {
                e.Graphics.FillRectangle(new SolidBrush(Color.White), r);
                e.Graphics.DrawString(b.Color.Name, new Font("Ariel", 8, FontStyle.Bold), new SolidBrush(Color.Black), r, sf);
                e.DrawFocusRectangle();
            } else {
                e.Graphics.FillRectangle(new SolidBrush(Color.LightBlue), r);
                e.Graphics.DrawString(b.Color.Name, new Font("Veranda", 12, FontStyle.Bold), new SolidBrush(Color.Red), r, sf);
                e.DrawFocusRectangle();
            }
        }
    }

    private void MyItemSelected(object sender, System.EventArgs e) {
        ComboBox cb = null;
        if (sender.Equals(comboBox1))
            cb = comboBox1;
        else
            cb = comboBox2;
        int x = cb.SelectedIndex;

        if (sender.Equals(comboBox1)) {
            Console.WriteLine("Item Selected is = " + (string)cb.Items[x]);
        } else {
            SolidBrush br = (SolidBrush)colorArray[x];
            Console.WriteLine("Color Selected is = " + br.Color.Name);
        }
    }

    private void comboBox1_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) {
        if (e.Index % 2 == 0) {
            e.ItemHeight = 45;
            e.ItemWidth = 20;
        } else {
            e.ItemHeight = 25;
            e.ItemWidth = 10;
        }
    }
}








23.10.ComboBox
23.10.1.Add Items to ComboBoxAdd Items to ComboBox
23.10.2.Get Items in a ComboBoxGet Items in a ComboBox
23.10.3.ComboBox selection changed eventComboBox selection changed event
23.10.4.ComboBox focus leave eventComboBox focus leave event
23.10.5.ComboBox text changedComboBox text changed
23.10.6.Find item in a ComboBoxFind item in a ComboBox
23.10.7.OwnerDraw ComboBox
23.10.8.AutoComplete ComboBoxAutoComplete ComboBox