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