private void Form1_Load(object sender, EventArgs e) { listBox1.DrawMode = DrawMode.OwnerDrawFixed; listBox1.Items.Add("News today"); listBox1.Items.Add("News yesterday"); } private void button1_Click(object sender, EventArgs e) { listBox1.Refresh(); } private void listBox1_DrawItem(object sender, DrawItemEventArgs e) { // // Draw the background of the ListBox control for each item. e.DrawBackground(); string keyWords = textBox1.Text; string text = ((ListBox)sender).Items[e.Index].ToString(); List<int> keyWordIndexes = new List<int>(); //Keywords match if (!string.IsNullOrEmpty(keyWords)) { int index = 0; while (index >= 0) { index = text.IndexOf(keyWords, index); if (index >= 0) { keyWordIndexes.Add(index); index += keyWords.Length; } } } //If no keywords matched show black color if (keyWordIndexes.Count == 0) { e.Graphics.DrawString(text, e.Font, Brushes.Black, e.Bounds, StringFormat.GenericDefault); e.DrawFocusRectangle(); return; } //Draw //Keywords red color int start = 0; float x = 0; float y = e.Bounds.Y; foreach (int i in keyWordIndexes) { string normal = text.Substring(start, i - start); e.Graphics.DrawString(normal, e.Font, Brushes.Black, x, y);
x += e.Graphics.MeasureString(normal, e.Font).Width; string key = text.Substring(i, keyWords.Length); e.Graphics.DrawString(key, e.Font, Brushes.Red, x, y); x += e.Graphics.MeasureString(key, e.Font).Width; start = i + keyWords.Length; } if (start < text.Length) { string normal = text.Substring(start, text.Length - start); e.Graphics.DrawString(normal, e.Font, Brushes.Black, x, y); } e.DrawFocusRectangle(); }
{
listBox1.DrawMode = DrawMode.OwnerDrawFixed;
listBox1.Items.Add("News today");
listBox1.Items.Add("News yesterday");
} private void button1_Click(object sender, EventArgs e)
{
listBox1.Refresh();
} private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
//
// Draw the background of the ListBox control for each item. e.DrawBackground(); string keyWords = textBox1.Text; string text = ((ListBox)sender).Items[e.Index].ToString(); List<int> keyWordIndexes = new List<int>(); //Keywords match
if (!string.IsNullOrEmpty(keyWords))
{
int index = 0;
while (index >= 0)
{
index = text.IndexOf(keyWords, index); if (index >= 0)
{
keyWordIndexes.Add(index);
index += keyWords.Length;
}
}
} //If no keywords matched show black color
if (keyWordIndexes.Count == 0)
{
e.Graphics.DrawString(text, e.Font, Brushes.Black,
e.Bounds, StringFormat.GenericDefault);
e.DrawFocusRectangle();
return;
} //Draw
//Keywords red color int start = 0;
float x = 0;
float y = e.Bounds.Y; foreach (int i in keyWordIndexes)
{
string normal = text.Substring(start, i - start); e.Graphics.DrawString(normal, e.Font, Brushes.Black,
x, y);
x += e.Graphics.MeasureString(normal, e.Font).Width; string key = text.Substring(i, keyWords.Length); e.Graphics.DrawString(key, e.Font, Brushes.Red,
x, y); x += e.Graphics.MeasureString(key, e.Font).Width; start = i + keyWords.Length;
} if (start < text.Length)
{
string normal = text.Substring(start, text.Length - start); e.Graphics.DrawString(normal, e.Font, Brushes.Black,
x, y); } e.DrawFocusRectangle(); }