c# - 如何使 ListBox 中的某些项目变为粗体?

标签 c# .net visual-studio listbox

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

最佳答案

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

另请参阅 msdn 论坛上的这个问题:
Question on ListBox items

一个简单的例子(两个项目 - 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();
    }
 }

关于c# - 如何使 ListBox 中的某些项目变为粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/327310/

相关文章:

c# - 如何绑定(bind)到文本框

c# - 自定义 Api 授权忽略 AllowAnonymous

c# - 在 c# 中创建一个 IDisposable 类,它在完成时清理一个 SqlConnection

visual-studio - 在 Visual Studio 2017 中更新 PowerShell 版本

c++ - 内存泄漏,如何使用 MS Visual Studio 2012 检测导致内存泄漏的原因?

visual-studio - 不允许重载功能的第二个C链接C2733

c# - 在 OOP 中表达类型组合

c# - 实现接口(interface)的开销

c# - WCF 真的会取代.NET Remoting 吗?

c# - 如何从 C# 中的另一个事件停止 TCP 服务器