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

C# / CSharp Tutorial
1. Language Basics
2. Data Type
3. Operator
4. Statement
5. String
6. struct
7. Class
8. Operator Overload
9. delegate
10. Attribute
11. Data Structure
12. Assembly
13. Date Time
14. Development
15. File Directory Stream
16. Preprocessing Directives
17. Regular Expression
18. Generic
19. Reflection
20. Thread
21. I18N Internationalization
22. GUI Windows Forms
23. 2D
24. Design Patterns
25. Windows
26. XML
27. ADO.Net
28. Network
29. Directory Services
30. Security
31. unsafe
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
C# / CSharp Tutorial » GUI Windows Forms » ComboBox 
22. 10. 7. OwnerDraw ComboBox
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(1640);
        this.comboBox1.Size = new System.Drawing.Size(26431);
        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(1616);
        this.label1.Size = new System.Drawing.Size(10016);
        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(16104);
        this.comboBox2.Size = new System.Drawing.Size(26426);
        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(2480);
        this.label2.Text = "Color Combo Box";

        this.AutoScaleBaseSize = new System.Drawing.Size(513);
        this.ClientSize = new System.Drawing.Size(312157);
        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 % == 0) {
            e.ItemHeight = 45;
            e.ItemWidth = 20;
        else {
            e.ItemHeight = 25;
            e.ItemWidth = 10;
        }
    }
}
22. 10. ComboBox
22. 10. 1. Add Items to ComboBoxAdd Items to ComboBox
22. 10. 2. Get Items in a ComboBoxGet Items in a ComboBox
22. 10. 3. ComboBox selection changed eventComboBox selection changed event
22. 10. 4. ComboBox focus leave eventComboBox focus leave event
22. 10. 5. ComboBox text changedComboBox text changed
22. 10. 6. Find item in a ComboBoxFind item in a ComboBox
22. 10. 7. OwnerDraw ComboBox
22. 10. 8. AutoComplete ComboBoxAutoComplete ComboBox
w_w__w_.__j__a___va2___s_.___c_o___m___ | Contact Us
Copyright 2003 - 08 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.