469,353 Members | 2,041 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,353 developers. It's quick & easy.

ListView Header Color

How do I change the ListView Header Color?
If you know how to do it, please give a code example.
Adrian.

(I placed this question in another newsgroup by error.)

Feb 8 '07 #1
7 31077
What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.

--

P. de Ridder, drs econ., bsc psych.
Pearltree Software Development
www.pearltree.nl
www.pearltree.us
"Adrian <" <no*@home.anywherewrote in message
news:45********************@dreader2.news.tiscali. nl...
How do I change the ListView Header Color?
If you know how to do it, please give a code example.
Adrian.

(I placed this question in another newsgroup by error.)

Feb 8 '07 #2
Hi Adrian,

You need to specify OwnerDraw = true on the ListView properties and
subscribe to the DrawColumnHeader event. You are then responsible for
displaying the header information any way you chose. You also have to
subscribe to the DrawItem event and draw the list items.

When working with ownerdrawn controls, remember that the event state is a
combination of several states when testing for wether to draw a
highlighted or selected item.
On Thu, 08 Feb 2007 16:22:47 +0100, Adrian < <no*@home.anywherewrote:
What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 9 '07 #3
How to deal with these errors?

DrawListViewItemEventArgs' could not be found
DrawListViewSubItemEventArgs' could not be found
DrawListViewColumnHeaderEventArgs' could not be found

I am referencing
System.Drawing
System.Drawing.Design

There are no other System.Drawing libraries to reference

Adrian.


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
Hi Adrian,

You need to specify OwnerDraw = true on the ListView properties and
subscribe to the DrawColumnHeader event. You are then responsible for
displaying the header information any way you chose. You also have to
subscribe to the DrawItem event and draw the list items.

When working with ownerdrawn controls, remember that the event state is a
combination of several states when testing for wether to draw a
highlighted or selected item.
On Thu, 08 Feb 2007 16:22:47 +0100, Adrian < <no*@home.anywherewrote:
What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.


--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 9 '07 #4
You don't need the Drawing namespaces, just the regular
System.Windows.Forms namespace
Upon further reading it looks like the DrawItem event isn't needed,
so using the DrawColumnHeader event and DrawSubItem event the code below
works
The code also checks
ListView lv = new ListView();
public Form1()
{
InitializeComponent();

lv.View = View.Details;
lv.OwnerDraw = true;
lv.Size = new Size(200, 100);

lv.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(lv_DrawColumn Header);
lv.DrawSubItem += new
DrawListViewSubItemEventHandler(lv_DrawSubItem);

lv.Columns.Add("Col1");
lv.Columns.Add("Col1");
lv.Columns.Add("Col1");

this.Controls.Add(lv);

lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
}

void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgse)
{
if ((e.ItemState & ListViewItemStates.Focused) 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight,
e.Bounds);
e.Graphics.DrawString(e.Item.Text, lv.Font,
SystemBrushes.HighlightText, e.Bounds);
}
else
{
e.DrawBackground();
e.DrawText();
}
}

void lv_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.GreenYellow, e.Bounds);
e.DrawText();
}


On Fri, 09 Feb 2007 13:35:50 +0100, Adrian < <no*@home.anywherewrote:
How to deal with these errors?

DrawListViewItemEventArgs' could not be found
DrawListViewSubItemEventArgs' could not be found
DrawListViewColumnHeaderEventArgs' could not be found

I am referencing
System.Drawing
System.Drawing.Design

There are no other System.Drawing libraries to reference

Adrian.


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
Hi Adrian,

You need to specify OwnerDraw = true on the ListView properties and
subscribe to the DrawColumnHeader event. You are then responsible for
displaying the header information any way you chose. You also have to
subscribe to the DrawItem event and draw the list items.

When working with ownerdrawn controls, remember that the event state is a
combination of several states when testing for wether to draw a
highlighted or selected item.
On Thu, 08 Feb 2007 16:22:47 +0100, Adrian < <no*@home.anywherewrote:
>What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.




--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 9 '07 #5

Morten ,
DrawListViewSubItemEventArgs
DrawListViewColumnHeaderEventArgs
"Could not be found" in spite of referenceing System.Windows.Forms
Adrian.
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
You don't need the Drawing namespaces, just the regular
System.Windows.Forms namespace
Upon further reading it looks like the DrawItem event isn't needed,
so using the DrawColumnHeader event and DrawSubItem event the code below
works
The code also checks
ListView lv = new ListView();
public Form1()
{
InitializeComponent();

lv.View = View.Details;
lv.OwnerDraw = true;
lv.Size = new Size(200, 100);

lv.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(lv_DrawColumn Header);
lv.DrawSubItem += new
DrawListViewSubItemEventHandler(lv_DrawSubItem);

lv.Columns.Add("Col1");
lv.Columns.Add("Col1");
lv.Columns.Add("Col1");

this.Controls.Add(lv);

lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
}

void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
{
if ((e.ItemState & ListViewItemStates.Focused) 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight,
e.Bounds);
e.Graphics.DrawString(e.Item.Text, lv.Font,
SystemBrushes.HighlightText, e.Bounds);
}
else
{
e.DrawBackground();
e.DrawText();
}
}

void lv_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.GreenYellow, e.Bounds);
e.DrawText();
}


On Fri, 09 Feb 2007 13:35:50 +0100, Adrian < <no*@home.anywherewrote:
How to deal with these errors?

DrawListViewItemEventArgs' could not be found
DrawListViewSubItemEventArgs' could not be found
DrawListViewColumnHeaderEventArgs' could not be found

I am referencing
System.Drawing
System.Drawing.Design

There are no other System.Drawing libraries to reference

Adrian.


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
Hi Adrian,

You need to specify OwnerDraw = true on the ListView properties and
subscribe to the DrawColumnHeader event. You are then responsible for
displaying the header information any way you chose. You also have to
subscribe to the DrawItem event and draw the list items.

When working with ownerdrawn controls, remember that the event state is a
combination of several states when testing for wether to draw a
highlighted or selected item.
On Thu, 08 Feb 2007 16:22:47 +0100, Adrian < <no*@home.anywherewrote:
>What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.




--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 9 '07 #6
I have got it to run. It would not run on VS2003, but it will run on V2005.
I did make some changes though. However the header back color is read only,
so I am stuck, because specifically that I wanted to change.

Adrian

"Adrian <" <no*@home.anywherewrote in message
news:45********************@dreader2.news.tiscali. nl...
>
Morten ,
DrawListViewSubItemEventArgs
DrawListViewColumnHeaderEventArgs
"Could not be found" in spite of referenceing System.Windows.Forms
Adrian.
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
You don't need the Drawing namespaces, just the regular
System.Windows.Forms namespace
Upon further reading it looks like the DrawItem event isn't needed,
so using the DrawColumnHeader event and DrawSubItem event the code below
works
The code also checks
ListView lv = new ListView();
public Form1()
{
InitializeComponent();

lv.View = View.Details;
lv.OwnerDraw = true;
lv.Size = new Size(200, 100);

lv.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(lv_DrawColumn Header);
lv.DrawSubItem += new
DrawListViewSubItemEventHandler(lv_DrawSubItem);

lv.Columns.Add("Col1");
lv.Columns.Add("Col1");
lv.Columns.Add("Col1");

this.Controls.Add(lv);

lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
}

void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgs
e)
{
if ((e.ItemState & ListViewItemStates.Focused) 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight,
e.Bounds);
e.Graphics.DrawString(e.Item.Text, lv.Font,
SystemBrushes.HighlightText, e.Bounds);
}
else
{
e.DrawBackground();
e.DrawText();
}
}

void lv_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.GreenYellow, e.Bounds);
e.DrawText();
}


On Fri, 09 Feb 2007 13:35:50 +0100, Adrian < <no*@home.anywherewrote:
How to deal with these errors?

DrawListViewItemEventArgs' could not be found
DrawListViewSubItemEventArgs' could not be found
DrawListViewColumnHeaderEventArgs' could not be found

I am referencing
System.Drawing
System.Drawing.Design

There are no other System.Drawing libraries to reference

Adrian.


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
Hi Adrian,

You need to specify OwnerDraw = true on the ListView properties and
subscribe to the DrawColumnHeader event. You are then responsible for
displaying the header information any way you chose. You also have to
subscribe to the DrawItem event and draw the list items.

When working with ownerdrawn controls, remember that the event state is
a
combination of several states when testing for wether to draw a
highlighted or selected item.
On Thu, 08 Feb 2007 16:22:47 +0100, Adrian < <no*@home.anywhere>
wrote:
What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.

--
Happy Coding!
Morten Wennevik [C# MVP]


Feb 10 '07 #7
Sorry, you need to mention if you are running .Net 1.1 or we will assume
you are using .Net 2.0 or .Net 3.0. I don't know what you mean by
Read-Only header, the color should be whatever brush you are using when
calling Graphics.FillRectangle(Brush, e.Bounds), assuming you have set
ListView.OwnerDraw = true
On Sat, 10 Feb 2007 13:52:42 +0100, Adrian < <no*@home.anywherewrote:
I have got it to run. It would not run on VS2003, but it will run on
V2005.
I did make some changes though. However the header back color is read
only,
so I am stuck, because specifically that I wanted to change.

Adrian

"Adrian <" <no*@home.anywherewrote in message
news:45********************@dreader2.news.tiscali. nl...
>>
Morten ,
DrawListViewSubItemEventArgs
DrawListViewColumnHeaderEventArgs
"Could not be found" in spite of referenceing System.Windows.Forms
Adrian.
"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:op***************@tr024.bouvet.no...
You don't need the Drawing namespaces, just the regular
System.Windows.Forms namespace
Upon further reading it looks like the DrawItem event isn't needed,
so using the DrawColumnHeader event and DrawSubItem event the code below
works
The code also checks
ListView lv = new ListView();
public Form1()
{
InitializeComponent();

lv.View = View.Details;
lv.OwnerDraw = true;
lv.Size = new Size(200, 100);

lv.DrawColumnHeader += new
DrawListViewColumnHeaderEventHandler(lv_DrawColum nHeader);
lv.DrawSubItem += new
DrawListViewSubItemEventHandler(lv_DrawSubItem) ;

lv.Columns.Add("Col1");
lv.Columns.Add("Col1");
lv.Columns.Add("Col1");

this.Controls.Add(lv);

lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
lv.Items.Add(new ListViewItem(new string[] { "One", "Two",
"Three" }));
}

void lv_DrawSubItem(object sender, DrawListViewSubItemEventArgs
e)
> {
if ((e.ItemState & ListViewItemStates.Focused) 0)
{
e.Graphics.FillRectangle(SystemBrushes.Highlight,
e.Bounds);
e.Graphics.DrawString(e.Item.Text, lv.Font,
SystemBrushes.HighlightText, e.Bounds);
}
else
{
e.DrawBackground();
e.DrawText();
}
}

void lv_DrawColumnHeader(object sender,
DrawListViewColumnHeaderEventArgs e)
{
e.Graphics.FillRectangle(Brushes.GreenYellow, e.Bounds);
e.DrawText();
}


On Fri, 09 Feb 2007 13:35:50 +0100, Adrian < <no*@home.anywhere
wrote:
How to deal with these errors?

DrawListViewItemEventArgs' could not be found
DrawListViewSubItemEventArgs' could not be found
DrawListViewColumnHeaderEventArgs' could not be found

I am referencing
System.Drawing
System.Drawing.Design

There are no other System.Drawing libraries to reference

Adrian.


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in
message
news:op***************@tr024.bouvet.no...
Hi Adrian,

You need to specify OwnerDraw = true on the ListView properties and
subscribe to the DrawColumnHeader event. You are then responsible for
displaying the header information any way you chose. You also haveto
subscribe to the DrawItem event and draw the list items.

When working with ownerdrawn controls, remember that the event state
is
a
combination of several states when testing for wether to draw a
highlighted or selected item.
On Thu, 08 Feb 2007 16:22:47 +0100, Adrian < <no*@home.anywhere>
wrote:
>
What I am after (my post isn't clear) is
the background color. Standard it is
ControlLight, which I would like
to change. Hope someone can help.

Adrian.


--
Happy Coding!
Morten Wennevik [C# MVP]




--
Happy Coding!
Morten Wennevik [C# MVP]
Feb 12 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by andrewcw | last post: by
5 posts views Thread by Yura Moldavanov | last post: by
3 posts views Thread by Simon Abolnar | last post: by
1 post views Thread by Lespaul36 | last post: by
reply views Thread by Diogo Alves - Software Developer | last post: by
reply views Thread by > Adrian | last post: by
1 post views Thread by John Rogers | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.