ListView控件改变字体颜色

原文:http://www.xue5.com/itedu/200802/107815.html

.net实例:C# winform中,简单实现Listview.Subitem.BackColor.ForeColor改变字体颜色,Listview.Subitem.BackColor 改变背景

 

做项目的时候,客户的查询结果中要在listview中亮显查询关键字.在网上找了半天,没有合适的代码.
于是就自己琢磨了一下.贴出代码,希望对大家有所帮助.

注意事项:

        一定要把listview的OwnerDraw属性设置为True(默认是False)。
       当设置OwnerDraw = True后,FullRowSelect 、HideSelection 属性好象实效了。

       添加DrawColumnHeader事件,绘制列标头用。        
       添加DrawSubItem事件,绘子项头用。

代码如下:

        private void lvKeyPerson_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
        {
            e.DrawBackground();
            e.DrawText();
        }
        private void lvKeyPerson_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
        {
            e.DrawBackground();
            //判断Subitem中是否存在关键字
            if (txtContent.Text.Trim().Length > 0 && e.SubItem.Text.IndexOf(txtContent.Text.Trim()) >= 0)
            {
                e.SubItem.BackColor = Color.Pink;  //设置背景色为粉红色
            }
            else
            {
                e.SubItem.ForeColor = Color.Black; //设置字体为红色
            }

            e.DrawText();
        }

如下图所示:

错误:图没有了。


 

posted on 2009-04-16 11:24  冷月孤峰  阅读(2273)  评论(2)    收藏  举报