using System.Collections; using System.ComponentModel; using System.Windows.Forms;
{
/// <summary>
/// Summary description for MyComboBox.
/// </summary>
{
private System.Windows.Forms.ComboBox comboBox1
; private System.Windows.Forms.Label label1
; private System.Windows.Forms.ComboBox comboBox2
; private System.Windows.Forms.Label label2
; ArrayList colorArray
= new ArrayList
() ; ArrayList fontArray
= new ArrayList
() ; /// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components
= null;
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
{
{
{
components.Dispose();
}
}
base.Dispose( disposing
); }
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
{
this.comboBox1 = new System.Windows.Forms.ComboBox(); this.label1 = new System.Windows.Forms.Label(); this.comboBox2 = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); //
// comboBox1
//
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable; this.comboBox1.ItemHeight = 25; this.comboBox1.Location = new System.Drawing.Point(16,
40); this.comboBox1.Name = "comboBox1"; this.comboBox1.Size = new System.Drawing.Size(264,
31); this.comboBox1.TabIndex = 0; 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); //
// label1
//
this.label1.Location = new System.Drawing.Point(16,
16); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(100,
16); this.label1.TabIndex = 1; this.label1.Text = "Font Combo Box"; //
// comboBox2
//
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.Name = "comboBox2"; this.comboBox2.Size = new System.Drawing.Size(264,
26); this.comboBox2.TabIndex = 0; this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.MyItemSelected); this.comboBox2.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.comboBox2_DrawItem); //
// label2
//
this.label2.Location = new System.Drawing.Point(24,
80); this.label2.Name = "label2"; this.label2.TabIndex = 2; this.label2.Text = "Color Combo Box"; //
// MyComboBox
//
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.Name = "MyComboBox"; this.Text = "OwnerDraw ComboBox"; this.Load += new System.EventHandler(this.MyComboBox_Load);
}
#endregion
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
{
Application
.Run(new MyComboBox
()); }
{
// Fill an array with the different color you wish
// to display in the ComboBox
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));
// Add blank items to the list, since the text we will display
// will be the name of the Color we are painting
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
comboBox2.Items.Add("");
// Fill an array with the different fonts that you will use to display
// items in the other comboBox
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));
// Add the items that in the listBox
comboBox1.Items.Add("Washington");
comboBox1.Items.Add("Houston");
comboBox1.Items.Add("Phoenix");
comboBox1.Items.Add("Dallas");
comboBox1.Items.Add("London");
}
private void comboBox1_DrawItem
(object sender,
System.Windows.Forms.DrawItemEventArgs e
) {
// Override this function to draw items in the Font comboBox
// Get the Graphics Object (aka. CDC or Device Context Object )
// passed via the DrawItemEventArgs parameter
Graphics g = e.Graphics ;
// Get the bounding rectangle of the item currently being painted
Rectangle r = e.Bounds ;
{
// Get the Font object, at the specifid index in the fontArray
fn = (Font)fontArray[e.Index];
// Get the text that we wish to display
// Set the string format options
StringFormat sf
= new StringFormat
(); sf.Alignment = StringAlignment.Near;
Console.WriteLine(e.State.ToString());
// Draw the rectangle
e
.Graphics.DrawRectangle(new Pen
(new SolidBrush
(Color
.Black),
2 ), r
);
if ( e
.State == ( DrawItemState
.NoAccelerator | DrawItemState
.NoFocusRect)) {
// if the item is not selected draw it with a different color
e
.Graphics.FillRectangle(new SolidBrush
(Color
.White) , r
); e
.Graphics.DrawString( s , fn ,
new SolidBrush
(Color
.Black), r ,sf
); e.DrawFocusRectangle();
}
{
// if the item is selected draw it with a different color
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
) {
// Override this function to draw items in the Color comboBox
// Get the Graphics Object (aka. CDC or Device Context Object )
// passed via the DrawItemEventArgs parameter
Graphics g = e.Graphics ;
// Get the bounding rectangle of the item currently being painted
Rectangle r = e.Bounds ;
{
Rectangle rd = r ;
rd.Width = 100 ;
Rectangle rt = r ;
r.Left = rd.Right ;
// Get the brush object, at the specifid index in the colorArray
SolidBrush b = (SolidBrush)colorArray[e.Index];
// Fill a portion of the rectangle with the selected brush
g.FillRectangle(b , rd);
// Set the string format options
StringFormat sf
= new StringFormat
(); sf.Alignment = StringAlignment.Near;
Console.WriteLine(e.State.ToString());
// Draw the rectangle
e
.Graphics.DrawRectangle(new Pen
(new SolidBrush
(Color
.Black),
2 ), r
);
if ( e
.State == ( DrawItemState
.NoAccelerator | DrawItemState
.NoFocusRect)) {
// if the item is not selected draw it with a different color
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();
}
{
// if the item is selected draw it with a different color
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();
}
}
}
{
// UnBox the sender. Since both the ComboBox controls use the same
// events to handle the selection of item from the list
if ( sender
.Equals(comboBox1
)) cb = comboBox1;
cb = comboBox2;
// Get the index of the Item Selected
int x
= cb
.SelectedIndex ;
// Get the Text of the item selected
if ( sender
.Equals(comboBox1
)) {
str
= "Item Selected is = " + (string)cb
.Items[x
]; }
{
SolidBrush br = (SolidBrush)colorArray[x];
str = "Color Selected is = " + br.Color.Name;
}
MessageBox.Show(str,"ComboBox Item");
}
private void comboBox1_MeasureItem
(object sender,
System.Windows.Forms.MeasureItemEventArgs e
) {
// For the comboBox with OwnerDrawVariable property
// Display every second item with a height of 45
{
e.ItemHeight = 45 ;
e.ItemWidth = 20 ;
}
{
// Display all other items with a height of 25
e.ItemHeight = 25 ;
e.ItemWidth = 10 ;
}
}
}
}