NinethSense oWnZ mE!: Praveen’s drawing book

Choose a Topic:

Thu
20
Sep '07

Change attributes of items in a combobox

This was an expiriment program for changing fonts, colors, draw shapes etc.

To alter the items, you need to use comboBox1.DrawMode

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
            comboBox1.DropDownHeight = 200;
            comboBox1.Items.Add(“Hello”);
            comboBox1.Items.Add(“W o r l d”);
            comboBox1.Items.Add(“And MEEEEEEEEEEEEEEEEEeeeeee”);
 
        }
 
        
 
        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            e.DrawBackground();
            if (e.Index == 0)
            {
                g.FillRectangle(Brushes.Yellow, 10, 10, 70, 20);
                g.DrawString(“test”, new Font(“arial”, 10), Brushes.Green, 10, 10);
            }
            if (e.Index == 1)
            {
                g.DrawArc(new Pen(Color.Brown), 20, 20, 30, 30, 30, 120);
                g.DrawString(“test”, new Font(“courier new”, 10), Brushes.Red, 10, 70);
            }
            if (e.Index == 3)
            {
                g.DrawArc(new Pen(Color.Brown), 20, 40, 30, 30, 30, 120);
                g.DrawString(“tesT”, new Font(“courier new”, 10), Brushes.Red, 10, 90);
            }
        }
 
        private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            switch (e.Index)
            {
                case 0: e.ItemHeight = 50;
                    break;
                case 1: e.ItemHeight = 80;
                    break;
                case 2: e.ItemHeight = 30;
                    break;
            }
        }
    }
}



Inspired from MSDN link - http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode.aspx
The CodeProject Microsoft Developer Network Official ASP.NET Forums Microsoft .NET Framework Community Microsoft Most Valuable Professional Kidoos forums Microsoft Visual Studio Developer Home Professional Information Technology Solutions Microsoft Research Home Trivandrum Microsoft Users Group Community Website