How can I make some items in a ListBox bold?

前端 未结 5 1982
隐瞒了意图╮
隐瞒了意图╮ 2020-12-31 03:28

In Visual c# Express Edition, is it possible to make some (but not all) items in a ListBox bold? I can\'t find any sort of option for this in the API.

5条回答
  •  抹茶落季
    2020-12-31 04:02

    Following is the code demonstrating the same.

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

    Sample Output

提交回复
热议问题