本文整理汇总了C#中System.Windows.Forms.ComboBox.DrawItem事件的典型用法代码示例。如果您正苦于以下问题:C# ComboBox.DrawItem事件的具体用法?C# ComboBox.DrawItem怎么用?C# ComboBox.DrawItem使用的例子?那么恭喜您, 这里精选的事件代码示例或许可以为您提供帮助。您也可以进一步了解该事件所在类System.Windows.Forms.ComboBox
的用法示例。
在下文中一共展示了ComboBox.DrawItem事件的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InitializeComboBox
internal System.Windows.Forms.ComboBox ComboBox1;
private string[] animals;
// This method initializes the owner-drawn combo box.
// The drop-down width is set much wider than the size of the combo box
// to accomodate the large items in the list. The drop-down style is set to
// ComboBox.DropDown, which requires the user to click on the arrow to
// see the list.
private void InitializeComboBox()
{
this.ComboBox1 = new ComboBox();
this.ComboBox1.DrawMode =
System.Windows.Forms.DrawMode.OwnerDrawVariable;
this.ComboBox1.Location = new System.Drawing.Point(10, 20);
this.ComboBox1.Name = "ComboBox1";
this.ComboBox1.Size = new System.Drawing.Size(100, 120);
this.ComboBox1.DropDownWidth = 250;
this.ComboBox1.TabIndex = 0;
this.ComboBox1.DropDownStyle = ComboBoxStyle.DropDown;
animals = new string[]{"Elephant", "c r o c o d i l e", "lion"};
ComboBox1.DataSource = animals;
this.Controls.Add(this.ComboBox1);
// Hook up the MeasureItem and DrawItem events
this.ComboBox1.DrawItem +=
new DrawItemEventHandler(ComboBox1_DrawItem);
this.ComboBox1.MeasureItem +=
new MeasureItemEventHandler(ComboBox1_MeasureItem);
}
// If you set the Draw property to DrawMode.OwnerDrawVariable,
// you must handle the MeasureItem event. This event handler
// will set the height and width of each item before it is drawn.
private void ComboBox1_MeasureItem(object sender,
System.Windows.Forms.MeasureItemEventArgs e)
{
switch(e.Index)
{
case 0:
e.ItemHeight = 45;
break;
case 1:
e.ItemHeight = 20;
break;
case 2:
e.ItemHeight = 35;
break;
}
e.ItemWidth = 260;
}
// You must handle the DrawItem event for owner-drawn combo boxes.
// This event handler changes the color, size and font of an
// item based on its position in the array.
private void ComboBox1_DrawItem(object sender,
System.Windows.Forms.DrawItemEventArgs e)
{
float size = 0;
System.Drawing.Font myFont;
FontFamily family = null;
System.Drawing.Color animalColor = new System.Drawing.Color();
switch(e.Index)
{
case 0:
size = 30;
animalColor = System.Drawing.Color.Gray;
family = FontFamily.GenericSansSerif;
break;
case 1:
size = 10;
animalColor = System.Drawing.Color.LawnGreen;
family = FontFamily.GenericMonospace;
break;
case 2:
size = 15;
animalColor = System.Drawing.Color.Tan;
family = FontFamily.GenericSansSerif;
break;
}
// Draw the background of the item.
e.DrawBackground();
// Create a square filled with the animals color. Vary the size
// of the rectangle based on the length of the animals name.
Rectangle rectangle = new Rectangle(2, e.Bounds.Top+2,
e.Bounds.Height, e.Bounds.Height-4);
e.Graphics.FillRectangle(new SolidBrush(animalColor), rectangle);
// Draw each string in the array, using a different size, color,
// and font for each item.
myFont = new Font(family, size, FontStyle.Bold);
e.Graphics.DrawString(animals[e.Index], myFont, System.Drawing.Brushes.Black, new RectangleF(e.Bounds.X+rectangle.Width, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
// Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}
示例2: ArrayList
//引入命名空间
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(16, 40);
this.comboBox1.Size = new System.Drawing.Size(264, 31);
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(16, 16);
this.label1.Size = new System.Drawing.Size(100, 16);
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(16, 104);
this.comboBox2.Size = new System.Drawing.Size(264, 26);
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(24, 80);
this.label2.Text = "Color Combo Box";
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.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 % 2 == 0) {
e.ItemHeight = 45;
e.ItemWidth = 20;
} else {
e.ItemHeight = 25;
e.ItemWidth = 10;
}
}
}