我用一个文本框制作了一个表单,它接受一个单词并搜索一堆句子以查看其中是否包含该单词.之后我必须出现这些句子并突出显示单词.我的计划是制作一个 ListBox并在其中添加句子.我的问题是如何突出显示这个词(通过改变我想的颜色)以便区分它.
I made a Form with a TextBox that accepts a word and searches a bunch of sentences to see if any of them contains that word .After that I have to appear those sentences and highlight the word .My plan is to make a ListBox and add the sentences inside of it. My problem is how to highlight the word (by changing the color I suppose) so it can be distinguished.
有没有更好的方法?我选择了 ListBox,所以我可以选择我要查找的句子.
Is there a preferable way? I chose ListBox so I can select the sentence I'm looking for.
编辑
根据@Thorsten Dittmar 的指示创建一个所有者绘制的列表框.
According to @Thorsten Dittmar directions a create an owner drawn list box.
public partial class Form1 : Form
{
private List<string> _items;
public Form1()
{
InitializeComponent();
_items = new List<string>();
_items.Add("One");
_items.Add("Two");
_items.Add("Three");
listBox1.DataSource = _items;
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.DrawFocusRectangle();
e.Graphics.DrawString(_items[e.Index],
new Font(FontFamily.GenericSansSerif,
8, FontStyle.Bold),
new SolidBrush(Color.Red), e.Bounds);
}
}
我将如何拆分句子以便只画一个单词?
How I'm going to split the sentence in order to draw only one word?
编辑2
我最终这样做的方式是制作两个单独的组件,以组合我的选项.一个是 ListBox
,所有的句子都是彩色的,并且可以选择一个其中一个和另一个是 RichBox
有单独的彩色单词,因为很难用 ListBox
实现这一点(至少对我来说).
The way I finally did it was to make two seperate components, to compine my options.
One was a ListBox
with all the sentences colored and the option to select one
of those and the other one a RichBox
with separate colored words since is to difficult
to achieve that with the ListBox
(for me a least).
我做到这一点的方法是使用一个布尔数组来指向哪个单词在每个句子中涂上颜色.
The way I accomplished that was by using a boolean array pointing which word should be colored in each sentence.
for (int i = 0; i < words.Length; i++)
{
if (segments[i]) //<-boolean array
{
rich.SelectionColor = Color.Red;
rich.AppendText(words[i] + " ");
rich.SelectionColor = Color.Black;
}
else
{
rich.AppendText(words[i] + " ");
}
}
在 Windows 窗体中没有标准的方法.您必须手动呈现列表项(创建所有者绘制的列表框).在 WPF 中,这将是一件容易的事.
There is no standard way of doing it in Windows Forms. You'd have to render the list items manually (create an owner drawn list box). In WPF this would be an easy task.
编辑
仅用不同的字体绘制字符串的一部分并不是一件容易的事.我会尝试如下:
EDIT
Drawing only part of a string in a different font is not an easy task. What I'd try is the following:
引入告诉您粗体开始"和粗体结束"的标记 - 有点像 HTML.让我们将它们称为与 HTML 中相同的名称.所以你的字符串可能看起来像这样:
Introduce tokens that tell you "bold start" and "bold end" - a bit like in HTML. Let's call them the same as in HTML. So your string could look like this:
Hello, I am <b>bold</b> text<b>!</b>
现在我将我的字符串标记为非粗体文本和粗体文本.我会得到以下部分:
Now I'd tokenize my string into text that is non-bold and text that is bold. I'd get the following parts:
Hello, I am
bold
text
!
现在我将使用以下算法绘制每个部分:
Now I'd draw each part using the following algorithm:
在第 2 步中,将调用 Graphics.MeasureString
方法来获取字符串的宽度.
In step 2 the Graphics.MeasureString
method would be called to get the width of the string.
对上面的 4 个示例部分执行此操作会导致:
Doing this for the 4 sample parts above would result in:
你好,我是
你好,我是大胆
你好,我是粗体文字
你好,我是粗体文字!
这篇关于如何更改列表框中单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!