我要如何改变一个列表框里面一个字的颜色我要、一个字、里面、颜色

2023-09-04 01:23:59 作者:利欲熏心

我做了一个接受一个单词,并搜索了一堆句子,看看其中是否包含这个词。之后,我有机会出现的句子,并强调这个词。我的计划是做一个列表框一个TextBox窗体并添加句子里面它。我的问题是如何突出字(通过改变我想的颜色),以便它能够被区分。

有没有preferable方式? 我选择列表框,所以我可以选择句话我在找。

修改的

据@Thorsten Dittmar方向上创建所有者绘制的列表框。

 公共部分类Form1中:形态
    {
        私人列表<字符串> _items;

        公共Form1中()
        {
            的InitializeComponent();
            _items =新的名单,其中,串>();
            _items.Add(1);
            _items.Add(二);
            _items.Add(三公);
            listBox1.DataSource = _items;
        }

        私人无效listBox1_DrawItem(对象发件人,DrawItemEventArgs E)
        {
            e.DrawBackground();
            e.DrawFocusRectangle();
            e.Graphics.DrawString(_items [e.Index]
                新的字体(FontFamily.GenericSansSerif,
                    8,FontStyle.Bold)
                    新SolidBrush(Color.Red),e.Bounds);
        }
    }
 

我如何去分割判决,以绘制只有一个字?

EDIT2 的

就是我终于做到了是使两个独立的组件,compine我的选择。 一个是列表框与所有句子有色和选项中选择一个 这些和其他的一个的 RichBox 有独立的彩色的话,因为是困难 要实现这一目标与列表框(对我来说至少)。

我做到了这一点的方法是使用一个布尔数组指向哪个字应该 着色每个句子。

 的for(int i = 0; I< words.Length;我++)
{
  如果(段[I])//<  - 布尔数组
  {
     rich.SelectionColor = Color.Red;
     rich.AppendText(字[I] +);
     rich.SelectionColor = Color.Black;
  }
  其他
  {
    rich.AppendText(字[I] +);
  }
}
 

解决方案

有做在Windows窗体中没有标准的方法。你不得不手动呈现列表项(创建所有者绘制的列表框)。在WPF中,这将是一件容易的事。

怎样修改超级列表框中某一行的文字的颜色

修改 以不同字体绘制一个字符串的一部分并不是一件容易的事。我想尝试如下:

介绍,告诉你大胆启动和大胆的结束标记 - 有点像在HTML中。让我们称他们为相同的HTML。所以,你的字符串可能看起来像这样:

 您好,我是< B>大胆< / B>文字< B>!< / B>
 

现在我想我的记号化字符串成是大胆的文字就是非粗体和文字。我会得到以下几部分组成:

 你好,我
胆大
文本
!
 

现在我会使用以下算法得出每个部分:

在当前位置x 绘制字符串在当前格式 将宽字符串的第1步中绘制增加位置x 更改格式,根据即将到来的字符串 转到1

在步骤2中的 Graphics.MeasureString 方法将被调用来获得字符串的宽度。

这样做了4样件以上的会导致:

您好,我是 您好,我是粗体 您好,我是粗体文字 您好,我是粗体文字的!

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.

Is there a preferable way? I chose ListBox so I can select the sentence I'm looking for.

Edit

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?

Edit2

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] + " ");
  }
}

解决方案

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:

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:

Draw string in current format at current position x increase position x by width of the string drawn in step 1 change formatting according to upcoming string goto 1

In step 2 the Graphics.MeasureString method would be called to get the width of the string.

Doing this for the 4 sample parts above would result in:

Hello, I am Hello, I am bold Hello, I am bold text Hello, I am bold text !