| Printable Version
Owner Draw Combo Box
By Shripad Kulkarni
| Source Code : OD_LV.zip |
|
Many times in applications,
standard controls and their properties provided aren't enough.They
include most of the functionality you want, but may need to add custom
properties.
In the case where
visual part of the control needs to be handled from your program, you
can achieve this by using the OwnerDraw property of the control.
OwnerDraw gives you
the ability to add your own custom drawing code. The
code below shows the example of a ComboBox with owner draw property ...
|
|
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace OD_LV
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ComboBox comboBox1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
static int delta = 5 ;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.SuspendLayout();
//
// comboBox1
//
this.comboBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.comboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.Simple;
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(496, 397);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged
+= new System.EventHandler(this.comboBox1_SelectedIndexChanged);
this.comboBox1.DrawItem
+= new System.Windows.Forms.DrawItemEventHandler(this.comboBox1_DrawItem);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(496, 397);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.comboBox1});
this.Name = "Form1";
this.Text = "Owner Draw Combo Box";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.ItemHeight = 35 ;
comboBox1.Items.Add("STRING1");
comboBox1.Items.Add("STRING2");
comboBox1.Items.Add("STRING3");
comboBox1.Items.Add("STRING4");
comboBox1.Items.Add("STRING5");
comboBox1.Items.Add("STRING6");
comboBox1.Items.Add("STRING7");
}
............................
............................
............................
............................
............................
............................
............................
}
}
|
|
|
| The
DrawItem Method |
private void comboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
Icon i = new Icon("D:\smk\OD_LV\TICK.ico");
Rectangle rc = new Rectangle(e.Bounds.X +delta, e.Bounds.Y+delta ,
e.Bounds.Width-delta , e.Bounds.Height-delta);
Console.WriteLine(e.State.ToString());
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center ;
e.Graphics.DrawRectangle(new Pen(new SolidBrush(Color.Black) , 2) , rc);
string str = (string)comboBox1.Items[e.Index];
int x=0;
if ( e.State == ( DrawItemState.Selected | DrawItemState.NoAccelerator
| DrawItemState.NoFocusRect) ||
e.State == DrawItemState.Selected )
{
e.Graphics.FillRectangle(new SolidBrush(Color.CornflowerBlue) , rc);
e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Cyan), rc ,sf);
e.Graphics.DrawIcon(i, e.Bounds.X , e.Bounds.Y+5);
}
else
{
e.Graphics.FillRectangle(new SolidBrush(Color.White) , rc);
e.Graphics.DrawString( str , new Font("Ariel" , 12) , new SolidBrush(Color.Black), rc ,sf);
}
}
|
|