[.net] highlighting characters in an ListBox item

Started by
3 comments, last by TheTroll 16 years, 9 months ago
Hi, I am using C#.Net 2 WinApps and I have a ListBox which contains so many items such as Apple, Orange, Grapes, etc...And If i search for a particular character (for ex: "a" or "pes") among these listbox items, how can i able to highlight those particular chars only for each item in the ListBox????
Advertisement
I am working on a solution for you. Should have it later tonight.

theTroll
This will do what you are trying to do. You with have to tweak the character spacing in the words, but that is it.

Make sure you set the ListBox DrawMode to System.Windows.Forms.DrawMode.OwnerDrawFixed;

               private void listBox1_DrawItem(object sender, DrawItemEventArgs e)        {            Brush textBrushRed = SystemBrushes.ControlText;            textBrushRed = Brushes.Red;            Brush textBrushBlack = SystemBrushes.ControlText;            textBrushBlack = Brushes.Black;            e.DrawBackground();            Brush textBrush = SystemBrushes.ControlText;            Font drawFont = e.Font;            string test = "is";            float startRect_Height = e.Bounds.Height;            float startRect_Width = e.Bounds.Width;            float startRect_X = e.Bounds.X;            float startRect_Y = e.Bounds.Y;            float currentRect_X = 0;            SizeF space_size = e.Graphics.MeasureString(" ", drawFont);            float space_spacing = space_size.Width;                        string s = listBox1.Items[e.Index].ToString();            if ( s.Contains(test))            {                                if ((e.State & DrawItemState.Selected) > 0)                    drawFont = new Font(drawFont.FontFamily, drawFont.Size, FontStyle.Bold);                if (s.Length > test.Length)                {                    System.Collections.ArrayList list = new System.Collections.ArrayList();                    string[] stringSeparators = new string[] { test };                    currentRect_X = startRect_X;                    if (s.StartsWith(test))                        list.Add(test);                    string[] strArray = s.Split(stringSeparators, StringSplitOptions.RemoveEmptyEntries);                    if (strArray.Length > 0)                    {                        for (int count = 0; count < strArray.Length; count++)                        {                            list.Add(strArray[count]);                            list.Add(test);                        }                        if(list.Count > 1)                            list.RemoveAt(list.Count - 1);                    }                    if (s.EndsWith(test))                        list.Add(test);                    RectangleF rect = new RectangleF(startRect_X, startRect_Y, startRect_Width, startRect_Height);                    foreach (string str in list)                    {                        SizeF size = e.Graphics.MeasureString(str, drawFont);                        rect.Width = size.Width;                        if (str == test)                        {                            e.Graphics.DrawString(str, drawFont, textBrushRed, rect);                        }                        else                        {                            e.Graphics.DrawString(str, drawFont, textBrushBlack, rect);                        }                        rect.X += size.Width - space_spacing;                    }                                                        }                else                {                    e.Graphics.DrawString(s, drawFont, textBrushRed, e.Bounds);                }            }            else if ((e.State & DrawItemState.Selected) > 0)            {                textBrush = SystemBrushes.HighlightText;                e.Graphics.DrawString(s, drawFont, textBrush, e.Bounds);            }            else                e.Graphics.DrawString(s, drawFont, textBrush, e.Bounds);                    }


Hope it helps... Sorry it took so long, but took a little while to get the string parsing and drawing figured out.
theTroll

[Edited by - TheTroll on July 10, 2007 11:48:14 AM]
Hi, Thanks for the code....it works fine...But i want to get, those items/chars, selected/highlighted (like we select strings using mouse)...But this only changes the color of that particular chars...any idea abt it???

Thanks in advance.........
I now believe this is homework. We don't give answers for homework, we will help but not give you the answers. People learn more if they do their own work.

The answer you are looking for is in my code. My code changes the foreground color, you are looking for changing the brush to do a highlight, a pretty simple change.

theTroll

This topic is closed to new replies.

Advertisement