C# 如何在 ListBox 中加粗一些项目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/327310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I make some items in a ListBox bold?
提问by Martin Doms
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.
在 Visual c# Express Edition 中,是否可以在 ListBox 中加粗一些(但不是全部)项目?我在 API 中找不到任何选项。
采纳答案by Mindaugas Mozūras
You need to change listbox's DrawMode to DrawMode.OwnerDrawFixed. Check out these articles on msdn:
DrawMode Enumeration
ListBox.DrawItem Event
Graphics.DrawString Method
您需要将列表框的 DrawMode 更改为 DrawMode.OwnerDrawFixed。查看 msdn 上的这些文章:
DrawMode Enumeration
ListBox.DrawItem Event
Graphics.DrawString Method
Also look at this question on msdn forums:
Question on ListBox items
也看看 msdn 论坛上的这个问题:
Question on ListBox items
A simple example (both items - Black-Arial-10-Bold):
一个简单的例子(两个项目 - 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();
}
}
回答by roman
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();
}
}
}
回答by jsirr13
To add to Mindaugas Mozūras's solution, I had a problem where my e.Bounds
wasn't large enough and text was getting cut off. To solve this problem (thanks to a post here), you override the OnMeasureItem
event and changes your DrawMode to DrawMode.OwnerDrawVariable
.
要添加到 Mindaugas Mozūras 的解决方案中,我遇到了一个问题,即我的e.Bounds
尺寸不够大并且文本被截断了。为了解决这个问题(感谢这里的帖子),您覆盖OnMeasureItem
事件并将您的 DrawMode 更改为DrawMode.OwnerDrawVariable
.
In designer:
在设计师:
listBox.DrawMode = DrawMode.OwnerDrawVariable;
In handler:
在处理程序中:
void listBox_MeasureItem(object sender, MeasureItemEventArgs e)
{
e.ItemHeight = 18;
}
Solved my issues of having the height cut off text.
解决了我的高度截断文本的问题。
回答by Skydev
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:
一个更通用的例子,它使用发件人,实际上尊重前景色(例如,如果项目被选中,或者用户使用其他颜色集,其中黑色前景色不是真的可读)和当前 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();
}
You need to have DrawMode set to OwnerDrawFixed (for example, in designer).
您需要将 DrawMode 设置为 OwnerDrawFixed(例如,在设计器中)。
回答by Jim Roton
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();
}
}