我想知道是否可以创建一个组合框(下拉列表)并在下拉列表中绘制特定单元格。 所以,如果我在下拉列表中有五个项目,那么第二个项目和最后一个项目将以蓝色绘制,其他项目将以灰色绘制。 有可能吗?
System.Windows.Forms.ComboBox comboBox;
comboBox = new System.Windows.Forms.ComboBox();
comboBox.AllowDrop = true;
comboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
comboBox.FormattingEnabled = true;
comboBox.Items.AddRange(excel.getPlanNames());
comboBox.Location = new System.Drawing.Point(x, y);
comboBox.Name = "comboBox1";
comboBox.Size = new System.Drawing.Size(sizeX, sizeY);
comboBox.TabIndex = 0;
group.Controls.Add(comboBox);
感谢任何帮助..
答案 0 :(得分:2)
您可以使用DrawItem
活动。
首先,您必须将DrawMode
的{{1}}设置为ComboBox
然后将OwnerDrawFixed
设置为以下内容:
DrawItem
此示例将使默认背景颜色为绿色,黑色文本,突出显示的项目将具有红色背景和黑色文本,位于索引1和3的项目。
您还可以使用private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
Font font = (sender as ComboBox).Font;
Brush backgroundColor;
Brush textColor;
if (e.Index == 1 || e.Index == 3)
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
backgroundColor = Brushes.Red;
textColor = Brushes.Black;
}
else
{
backgroundColor = Brushes.Green;
textColor = Brushes.Black;
}
}
else
{
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
backgroundColor = SystemBrushes.Highlight;
textColor = SystemBrushes.HighlightText;
}
else
{
backgroundColor = SystemBrushes.Window;
textColor = SystemBrushes.WindowText;
}
}
e.Graphics.FillRectangle(backgroundColor, e.Bounds);
e.Graphics.DrawString((sender as ComboBox).Items[e.Index].ToString(), font, textColor, e.Bounds);
}
变量设置单个项目的字体。