Listview causing an exception in vs2005

I've just added a bit of code to the .net2 listview and its causing an exception in vs2005. The additional code just autosizes the last column which i learned how to do from the code project.

protected override void WndProc(ref System.Windows.Forms.Message message)
{
const int WM_PAINT = 0xf;
switch (message.Msg)
{
case WM_PAINT:
if (this.View == System.Windows.Forms.View.Details && this.Columns.Count > 0)
this.Columns[this.Columns.Count - 1].Width = -2;
break;
}

base.WndProc(ref message);
}

Heres the exception message it gives me

The control core.mylistview has thrown an unhandled exception in the designer and has been disables

Exception:
The file D:\Documents\Visual Studio 2005\projects\server\main.cs cannot be modified in the designer while building or debugging

stack trace:
at core.mylistview.WndProc(Message&message) in D:\Documents\Visual Studio 2005\projects\server\controls\listview.cs:line39

This happens when its compiled on either an x86 or an x64 machine. If I compile it in visual studio 2003 it works perfectly. Has anyone else come accross this and does anyone know a work around


Answer this question

Listview causing an exception in vs2005

  • techiedella

    Oops, sorry. Forgot where I was there for a while. It's a member of the Component class, so it will be used in exactly the same way in C#:

    if (!this.DesignMode) {}

  • dba123

    Np i've added the code and it seems to fix the bug, but I can't help but think its a bit of a hack but if it works then thats ok I guess :)

  • ninajean

    Ok i'll give it a go :) does it work the same in C#

  • Jessel

    Ok how would I go about doing that

  • Harsh Modi - MSFT

    If Not Me.DesignMode Then

  • Naids

    It sounds like your code is being executed at design time and causing an issue. You could try testing the DesignMode property and only executing the troublesome code if it is false.
  • Listview causing an exception in vs2005