我希望在组合框中为每个框(不是文本项)着色,这些框有不同的项目,例如:山有红色框,海有蓝色框等。我已经尝试了,但它真的是混乱的一堆颜色我试过了
我需要突出显示每个组合框项目以显示它的预期颜色,当点击该组合框时它没有显示其预期的颜色
下面的代码
private void cmbRegion_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
string text = ((ComboBox)sender).Items[e.Index].ToString();
Brush brush;
if (text.Equals("Mountain"))
{
brush = Brushes.Black;
cmbRegion.BackColor = Color.Red;
}
else if (text.Equals("Sea"))
{
brush = Brushes.Black;
cmbRegion.BackColor = Color.Blue;
}
else
{
brush = Brushes.Black;
cmbRegion.BackColor = Color.White;
}
// Draw the text
e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
}
可以做到吗??
答案 0 :(得分:0)
修改MSDN example以符合您的要求。这里重要的属性是DrawMode。
private void Form1_Load(object sender, EventArgs e)
{
InitializeComboBox();
}
internal ComboBox ComboBox1;
private string[] items;
// 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.BackColor = Color.Red;
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;
items = new string[] { "Mountain", "Sea", "Other" };
ComboBox1.DataSource = items;
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);
this.ComboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;
}
private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Color itemBackgroundColor = new Color();
switch (ComboBox1.SelectedIndex)
{
case 0:
itemBackgroundColor = Color.Red;
break;
case 1:
itemBackgroundColor = Color.Blue;
break;
case 2:
itemBackgroundColor = Color.Green;
break;
}
ComboBox1.BackColor = itemBackgroundColor;
}
// If you set the Draw property to DrawMode.OwnerDrawVariable,
// you must handle the MeasureItem event. This event handler
private void ComboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
{
// No change to item height.
e.ItemHeight = e.ItemHeight;
}
// 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, DrawItemEventArgs e)
{
string text = ((ComboBox)sender).Items[e.Index].ToString();
Color itemBackgroundColor = new Color();
switch (e.Index)
{
case 0:
itemBackgroundColor = Color.Red;
break;
case 1:
itemBackgroundColor = Color.Blue;
break;
case 2:
itemBackgroundColor = Color.Green;
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.
e.Graphics.FillRectangle(new SolidBrush(itemBackgroundColor), e.Bounds);
// Draw each string in the array, using a different size, color,
// and font for each item.
e.Graphics.DrawString(items[e.Index], e.Font, Brushes.Black, e.Bounds);
// Draw the focus rectangle if the mouse hovers over an item.
e.DrawFocusRectangle();
}