Listview.Subitem.BackColor.ForeColor改变字体颜色和背景

注意事项:

        一定要把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-08-25 17:57  chuncn  阅读(3030)  评论(0编辑  收藏  举报

导航