How can I make some items in a ListBox bold?

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

    A more generic example that uses sender, and actually respects foreground color (if the item is selected, for example, or the user uses some another color set, where black foreground color is not really readable) and current ListBox font:

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

    You need to have DrawMode set to OwnerDrawFixed (for example, in designer).

提交回复
热议问题