【问题标题】:Custom ComboBox with Multi-line and Colored Items [closed]具有多行和彩色项目的自定义组合框 [关闭]
【发布时间】:2016-03-08 19:42:13
【问题描述】:

我一直在寻找与下图中的这个组合看起来或具有相似功能的组合。有没有已经可用的,或者你能指导我如何制作一个自定义的

【问题讨论】:

  • 这是一个自绘的ComboBox。
  • 使用FlowLayoutPanel 和自定义UserControl 创建要添加到其中的项目...

标签: .net vb.net winforms combobox


【解决方案1】:

您可以使用自绘的ComboBox。

例如,您可以:

  • 将DrawMode 属性设置为OwnerDrawFixed
  • 将ItemHeight属性设置为合适的高度
  • 处理DrawItem 事件并根据您所需的逻辑绘制项目。

示例代码:

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));
    }
}

截图:

【讨论】:

  • 感谢您的帮助,但是如何在组合框和项目列表中的悬停突出显示上绘制渐变选择
  • 不客气,你可以使用实心画笔或渐变画笔来填充矩形。
  • 我还添加了一些代码来处理项目状态,以便您绘制突出显示的项目。
  • @Will 也对答案投了反对票,因为您认为 question 离题是不公平和奇怪的。你可以投票赞成关闭。
  • @Will 感谢您的回复:) 我相信,当我们认为某个问题离题时,我们应该投票结束该问题,并且我们对该问题的想法不应影响答案并否决答案这不是我们应该走的路。 ٌ当你认为我的答案离题时,你拒绝了我的回答,你阻止我回答一个问题,因为你认为它离题,而我不这么认为。对答案投反对票意味着答案不正确、不相关或质量低下。我不认为你对答案有这样的想法:)
猜你喜欢
  • 2016-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-07-03
  • 2012-02-28
  • 1970-01-01
  • 1970-01-01
  • 2010-11-11
相关资源
最近更新 更多