17

在 Visual c# Express Edition 中,是否可以将 ListBox 中的某些(但不是全部)项目设为粗体?我在 API 中找不到任何类型的选项。

4

5 回答 5

32

您需要将列表框的 DrawMode 更改为 DrawMode.OwnerDrawFixed。查看 msdn 上的这些文章:
DrawMode Enumeration
ListBox.DrawItem Event
Graphics.DrawString Method

还可以在 msdn 论坛上查看这个问题:
关于 ListBox 项目的问题

一个简单的例子(两个项目 - Black-Arial-10-Bold):

 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();

        ListBox1.Items.AddRange(new Object[] { "First Item", "Second Item"});
        ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    }

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        e.DrawBackground();
        e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), new Font("Arial", 10, FontStyle.Bold), Brushes.Black, e.Bounds);
        e.DrawFocusRectangle();
    }
 }
于 2008-11-29T07:51:26.967 回答
2

为了增加 Mindaugas Mozūras 的解决方案,我遇到了一个问题,即我e.Bounds的尺寸不够大并且文本被截断。为了解决这个问题(感谢这里的帖子),您覆盖OnMeasureItem事件并将 DrawMode 更改为DrawMode.OwnerDrawVariable.

在设计师:

listBox.DrawMode = DrawMode.OwnerDrawVariable;

在处理程序中:

void listBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
     e.ItemHeight = 18;
}

解决了我的高度切断文本的问题。

于 2014-03-26T18:14:56.727 回答
1

一个更通用的示例,它使用 sender,并且实际上尊重前景色(例如,如果选择了项目,或者用户使用了另一种颜色集,其中黑色前景色不是真正可读的)和当前 ListBox 字体:

    private void listBoxDrawItem (object sender, DrawItemEventArgs e)
    {
        Font f = e.Font;
        if (e.Index == 1) //TODO: Your condition to make text bold
            f = new Font(e.Font, FontStyle.Bold);
        e.DrawBackground();
        e.Graphics.DrawString(((ListBox)(sender)).Items[e.Index].ToString(), f, new SolidBrush(e.ForeColor), e.Bounds);
        e.DrawFocusRectangle();
    }

您需要将 DrawMode 设置为 OwnerDrawFixed(例如,在设计器中)。

于 2015-10-01T01:06:31.820 回答
0

以下是演示相同的代码。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            foreach (FontFamily fam in FontFamily.Families)
            {
                listBox1.Items.Add(fam.Name);
            }
            listBox1.DrawMode = DrawMode.OwnerDrawFixed; // 属性里设置

        }

        private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), new Font(listBox1.Items[e.Index].ToString(), listBox1.Font.Size), Brushes.Black, e.Bounds);
            //e.DrawFocusRectangle();
        }
    }
}

样本输出

于 2013-12-09T11:19:56.893 回答
0

使所选项目加粗

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        ListBox1.Items.AddRange(new Object[] { "me", "myself", "bob"});

        // set the draw mode to fixed
        ListBox1.DrawMode = DrawMode.OwnerDrawFixed;
    }

    private void ListBox1_DrawItem(object sender, DrawItemEventArgs e)
    {
        // draw the background
        e.DrawBackground();

        // get the font
        Font font = new Font(e.Font, (e.State & DrawItemState.Selected) == DrawItemState.Selected ? FontStyle.Bold : FontStyle.Regular);

        // draw the text
        e.Graphics.DrawString(ListBox1.Items[e.Index].ToString(), font, new SolidBrush(ListBox1.ForeColor), e.Bounds);

        e.DrawFocusRectangle();
    }
}
于 2017-05-24T19:55:49.450 回答