本文介绍了带有多行和彩色项目的自定义 ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在寻找一种与下图中的组合看起来或具有相似特征的组合.有没有已经可用的,或者你能指导我如何定制一个
解决方案
您可以使用所有者绘制的 ComboBox
.
例如您可以:
- 设置
I have been looking for a combo that looks or have similar features with this one in the image below. Is there any already available one, or can you direct me to how to make a custom one
解决方案You can use an owner-drawn
ComboBox
.For example you can:
- Set
DrawMode
property toOwnerDrawFixed
- Set
ItemHeight
property to a suitable height - Handle
DrawItem
event and draw items based on your required logic.
Sample Code:
private void Form1_Load(object sender, EventArgs e) { this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed; this.comboBox1.ItemHeight = 40; var db = new TestDBEntities(); this.comboBox1.DataSource = db.Products.ToList(); } private void comboBox1_DrawItem(object sender, DrawItemEventArgs e) { if (e.Index > -1) { var name = ((Product)this.comboBox1.Items[e.Index]).Name; var id = ((Product)this.comboBox1.Items[e.Index]).Id; ; var price = ((Product)this.comboBox1.Items[e.Index]).Price; ; if ((e.State & DrawItemState.ComboBoxEdit) == DrawItemState.ComboBoxEdit) e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); else if ((e.State & DrawItemState.Focus) == DrawItemState.Focus) e.Graphics.FillRectangle(SystemBrushes.InactiveCaption, e.Bounds); else e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); e.Graphics.DrawString(name, new Font(this.comboBox1.Font, FontStyle.Bold), Brushes.Blue, new Rectangle(e.Bounds.Left, e.Bounds.Top, e.Bounds.Width, this.comboBox1.ItemHeight / 2)); e.Graphics.DrawString(string.Format("Id:{0}", id), this.comboBox1.Font, Brushes.Red, new Rectangle(e.Bounds.Left, e.Bounds.Top + this.comboBox1.ItemHeight / 2, e.Bounds.Width / 2, this.comboBox1.ItemHeight / 2)); e.Graphics.DrawString(string.Format("Price:{0}", price), this.comboBox1.Font, Brushes.Red, new Rectangle(e.Bounds.Left + e.Bounds.Width / 2, e.Bounds.Top + this.comboBox1.ItemHeight / 2, e.Bounds.Width, this.comboBox1.ItemHeight / 2)); } }
Screenshot:
这篇关于带有多行和彩色项目的自定义 ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- Set