110,538
社区成员
发帖
与我相关
我的任务
分享
public Form1()
{
InitializeComponent();
this.comboBox1.Items.AddRange(new string[] { "hello", "world", "nihao" });
this.comboBox1.DrawItem += new DrawItemEventHandler(ComboBox1_DrawItem);
this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;
}
private void ComboBox1_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e)
{
ComboBox combo = sender as ComboBox;
string text = combo.Items[e.Index].ToString();
Brush brush = System.Drawing.Brushes.Black;
if ((e.State & DrawItemState.Selected) == DrawItemState.Selected)
{
this.Text = text;
brush = System.Drawing.Brushes.White;
}
e.DrawBackground();
e.Graphics.DrawString(text, combo.Font, brush, e.Bounds.X, e.Bounds.Y);
e.DrawFocusRectangle();
}