【发布时间】:2016-03-08 19:42:13
【问题描述】:
【问题讨论】:
-
这是一个自绘的
ComboBox。 -
使用
FlowLayoutPanel和自定义UserControl创建要添加到其中的项目...
标签: .net vb.net winforms combobox
【问题讨论】:
ComboBox。
FlowLayoutPanel 和自定义UserControl 创建要添加到其中的项目...
标签: .net vb.net winforms combobox
您可以使用自绘的ComboBox。
例如,您可以:
示例代码:
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));
}
}
截图:
【讨论】: