Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Override OnPaint from ListView-Control

215 views
Skip to first unread message

Stefan Urech

unread,
Feb 10, 2003, 2:33:24 AM2/10/03
to
Hey,
i like to override the OnPaint-Method from the ListView-Control.
Unfortunately it seems, that this Method is never called.

Is there something wrong with my Code, or do I need to override on other
control to capture the paint event?

Here my Code:

public class ListView4I : System.Windows.Forms.ListView
{
protected override void OnPaint( PaintEventArgs e)
{
MessageBox.Show("Painting is running...");
base.OnPaint( e );
}
}

thx surech

Mike Barthold

unread,
Feb 10, 2003, 3:15:29 AM2/10/03
to
no, you are right- the method NEVER gets called because of a simple fact:
maybe you noticed also that the listview has no "FlatStyle" property...
This is, because, the listview is NOT PAINTED BY THE DOTNET FRAMEWORK. It's
just supported. This is also the reason why the SetStyle(DoubleBuffer) does
NOT work on a listview --- it's a managed .net control, sure, but the
painting is NOT done by dotnet.

HTH mike

"Stefan Urech" <s.urech@[NOSPAM]fiveinfo.ch> schrieb im Newsbeitrag
news:b27kjs$ofm$1...@rex.ip-plus.net...

Stefan Urech

unread,
Feb 10, 2003, 4:04:52 AM2/10/03
to
Ok, are there any other possibilities to do some things before painting
the listview?

Mike Barthold schrieb:

Mike Barthold

unread,
Feb 10, 2003, 4:13:02 AM2/10/03
to
Tell me in a few words what you want to do --- i'm quite experienced with
listview so maybe i can help you if i know what kind of task you're
planning.

mike

"Stefan Urech" <s.urech@[NOSPAM]fiveinfo.ch> schrieb im Newsbeitrag

news:b27pvc$ofm$3...@rex.ip-plus.net...

Stefan Urech

unread,
Feb 10, 2003, 4:41:27 AM2/10/03
to
I like to change the background of each Item alternate, ex. white,
green, white, green, ... (zebra-Style).

Ok, that's no problem when i fill the ListView like this:

if ((lsvListe.Items.Count % 2) == 0)
listItem.BackColor = Color.FromArgb(206, 253, 206);
lsvListe.Items.Add( listItem );

But now i like to make my own ListView, which do that automatic.

That with the OnPaint-Method-Overriding was only an idea, i'm not sure,
if it would work...

Mike Barthold schrieb:

Mike Barthold

unread,
Feb 10, 2003, 4:46:42 AM2/10/03
to
ok , i see --- you try to build the same listview i did :)))

I solved this with another idea -> my intention was to easily publish a
datatable object to a listview.
dynamically, this means, i dont know the datatypes, column-count, etc. so I
enhanced the listview class with a "Publish" method giving it a datatable or
dataview object as a parameter.
This publish method does exactly that, what you have written here (i % 2 ==
0)...
it works fine for me.

Since the listview has no paint event its the only way to do it, except you
hook on the base messagequeue of the control and try to grep that WM_PAINT
message... (I always try to avoid such lowlevel things and keep using the
interfaces that are provided to me from .net).
If you don't want to change the interface of the listview by enhancing it
with a publish method you will have to hook.

HTH mike

"Stefan Urech" <s.urech@[NOSPAM]fiveinfo.ch> schrieb im Newsbeitrag

news:b27s3v$ofm$4...@rex.ip-plus.net...

Stefan Urech

unread,
Feb 10, 2003, 6:32:09 AM2/10/03
to
Thanx for the idea with the Hooks!
I did it like this:

protected override void WndProc(ref Message m)
{
base.WndProc(ref m);

int i = 0;

// WM-Paint abfangen
if (m.Msg == 0x000F)
{
foreach( ListViewItem item in this.Items)
{
if(i++ % 2 == 0) item.BackColor = Color.LightBlue;
}
}
}

As you said, it's goes a little bit too deep. But hey, it works!

thx and cu surech


Mike Barthold schrieb:

0 new messages