How can I make some items in a ListBox bold?

前端 未结 5 1964
隐瞒了意图╮
隐瞒了意图╮ 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:19

    Make the selected item bold

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

提交回复
热议问题