ListView Header Color

  • Thread starter Thread starter > Adrian
  • Start date Start date
A

> Adrian

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.)
 
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
 
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.
 
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.




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.
 
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_DrawColumnHeader);
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();
}
 
Morten ,
DrawListViewSubItemEventArgs
DrawListViewColumnHeaderEventArgs
"Could not be found" in spite of referenceing System.Windows.Forms
Adrian.


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_DrawColumnHeader);
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();
}
 
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 said:
Morten ,
DrawListViewSubItemEventArgs
DrawListViewColumnHeaderEventArgs
"Could not be found" in spite of referenceing System.Windows.Forms
Adrian.


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




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.




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.
 
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


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 said:
Morten ,
DrawListViewSubItemEventArgs
DrawListViewColumnHeaderEventArgs
"Could not be found" in spite of referenceing System.Windows.Forms
Adrian.


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




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.




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
 
Back
Top