首页 新闻 会员 周边

如何让listbox里的关键字变色

0
悬赏园豆:10 [已解决问题] 解决于 2009-04-09 16:05

现在用winfrom做了个字典

主题窗口是用listbox做的

现在想实现当用户输入某个关键字(比如a)的时候那么listbox中显示所有a都被特殊标志(比如所有a都变颜色)

在网上找了好久都没有找到好办法....急啊!

望哪位高手指点啊!!!

 

问题补充: 在线等......
E_man的主页 E_man | 初学一级 | 园豆:190
提问于:2009-04-09 09:30
< >
分享
最佳答案
1

Form1 放置 listBox1, button1, textBox1

textBox1 输入关键字,按 button1 ,关键字在 listBox1 中高亮为红色

        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();

        }
 

eaglet | 专家六级 |园豆:17139 | 2009-04-09 11:52
其他回答(2)
0

分数太少了..哈哈.多点分就可以告诉你..当年我也是非常困难才解决这个问题的啊

 

补充、你这个listbox,好像不可以实现这个。自己写个类似于listbox的控件吧..里面建议镶嵌richTextBox

 

richTextBox可以很轻松的实现字符变色

邢少 | 园豆:10926 (专家六级) | 2009-04-09 11:32
0

我是这样理解的:

有一个textbox让你输入,如果输入的值在listbox里,就变色显示。

如果你只是要“包含”,而不管位置的话,可以用Contains:((string)listBox1.Items[i]).Contains(textBox1.Text )

如果必须完全匹配,可以用 listBox1.Items.ToString().StartsWith(textBox1.Text )

然后用 listBox1_DrawItem()方法来上色就行了,可以参考一下下面的:

http://topic.csdn.net/u/20081006/10/7f0dda61-490c-48fd-ae84-1f4c8e93c74a.html

http://www.diybl.com/course/4_webprogram/asp.net/asp_netshl/2008125/97358.html

有所为,有所不为 | 园豆:1200 (小虾三级) | 2009-04-09 12:10
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册