Thu
20
Sep '07
This was an expiriment program for changing fonts, colors, draw shapes etc.
To alter the items, you need to use comboBox1.DrawMode
Inspired from MSDN link - http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode.aspx
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

Leave a passing comment »