<small id='b9MFL'></small><noframes id='b9MFL'>

    <tfoot id='b9MFL'></tfoot>

      • <bdo id='b9MFL'></bdo><ul id='b9MFL'></ul>
    1. <legend id='b9MFL'><style id='b9MFL'><dir id='b9MFL'><q id='b9MFL'></q></dir></style></legend>
    2. <i id='b9MFL'><tr id='b9MFL'><dt id='b9MFL'><q id='b9MFL'><span id='b9MFL'><b id='b9MFL'><form id='b9MFL'><ins id='b9MFL'></ins><ul id='b9MFL'></ul><sub id='b9MFL'></sub></form><legend id='b9MFL'></legend><bdo id='b9MFL'><pre id='b9MFL'><center id='b9MFL'></center></pre></bdo></b><th id='b9MFL'></th></span></q></dt></tr></i><div id='b9MFL'><tfoot id='b9MFL'></tfoot><dl id='b9MFL'><fieldset id='b9MFL'></fieldset></dl></div>

      如何更改列表框中单词的颜色

      时间:2023-10-07
    3. <tfoot id='4jPhA'></tfoot>
    4. <small id='4jPhA'></small><noframes id='4jPhA'>

      <legend id='4jPhA'><style id='4jPhA'><dir id='4jPhA'><q id='4jPhA'></q></dir></style></legend>
            <tbody id='4jPhA'></tbody>
            <bdo id='4jPhA'></bdo><ul id='4jPhA'></ul>

                <i id='4jPhA'><tr id='4jPhA'><dt id='4jPhA'><q id='4jPhA'><span id='4jPhA'><b id='4jPhA'><form id='4jPhA'><ins id='4jPhA'></ins><ul id='4jPhA'></ul><sub id='4jPhA'></sub></form><legend id='4jPhA'></legend><bdo id='4jPhA'><pre id='4jPhA'><center id='4jPhA'></center></pre></bdo></b><th id='4jPhA'></th></span></q></dt></tr></i><div id='4jPhA'><tfoot id='4jPhA'></tfoot><dl id='4jPhA'><fieldset id='4jPhA'></fieldset></dl></div>
                本文介绍了如何更改列表框中单词的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我用一个文本框制作了一个表单,它接受一个单词并搜索一堆句子以查看其中是否包含该单词.之后我必须出现这些句子并突出显示单词.我的计划是制作一个 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:

                1. 在当前位置 x 以当前格式绘制字符串
                2. 将位置 x 增加第 1 步中绘制的字符串的宽度
                3. 根据即将出现的字符串更改格式
                4. 转到 1

                在第 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:

                你好,我是
                你好,我是大胆
                你好,我是粗体文字
                你好,我是粗体文字

                这篇关于如何更改列表框中单词的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:从 ListBox 中单击的项目中获取数据 下一篇:ListBox.SelectedIndexChanged 第一次没有触发

                相关文章

                <small id='BhK9W'></small><noframes id='BhK9W'>

                <i id='BhK9W'><tr id='BhK9W'><dt id='BhK9W'><q id='BhK9W'><span id='BhK9W'><b id='BhK9W'><form id='BhK9W'><ins id='BhK9W'></ins><ul id='BhK9W'></ul><sub id='BhK9W'></sub></form><legend id='BhK9W'></legend><bdo id='BhK9W'><pre id='BhK9W'><center id='BhK9W'></center></pre></bdo></b><th id='BhK9W'></th></span></q></dt></tr></i><div id='BhK9W'><tfoot id='BhK9W'></tfoot><dl id='BhK9W'><fieldset id='BhK9W'></fieldset></dl></div>
                  <bdo id='BhK9W'></bdo><ul id='BhK9W'></ul>

                  1. <tfoot id='BhK9W'></tfoot>
                  2. <legend id='BhK9W'><style id='BhK9W'><dir id='BhK9W'><q id='BhK9W'></q></dir></style></legend>