Home All Groups Group Topic Archive Search About

Autosize the last column in a ListView control using WndProc

Author
9 Jun 2009 10:33 AM
Co
Hi All,

I'm trying to override the wndproc to resize the last column of my
listview.
The code however doesn't do anything.
What am I doing wrong?

Public Class LvSort
    Inherits System.Windows.Forms.ListView

    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
        If m.Msg = WM_PAINT Then
            Explorer1.ListView.Columns
(Explorer1.ListView.Columns.Count - 1).Width = -2
        End If
    End Sub

End Class

Regards
Marco
The Netherlands

Author
9 Jun 2009 3:25 PM
Tom Dacon
A better way to do this would be to just handle the SizeChanged event of the
ListView. There's no need to override WndProc to do it, and even if you
chose to do so the WM_PAINT message is definitely the wrong message.

Tom Dacon
Dacon Software Consulting

Show quoteHide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:08f01104-f596-4a44-a013-8c681cfc74da@c9g2000yqm.googlegroups.com...
> Hi All,
>
> I'm trying to override the wndproc to resize the last column of my
> listview.
> The code however doesn't do anything.
> What am I doing wrong?
>
> Public Class LvSort
>    Inherits System.Windows.Forms.ListView
>
>    Protected Overrides Sub WndProc(ByRef m As Message)
>        MyBase.WndProc(m)
>        If m.Msg = WM_PAINT Then
>            Explorer1.ListView.Columns
> (Explorer1.ListView.Columns.Count - 1).Width = -2
>        End If
>    End Sub
>
> End Class
>
> Regards
> Marco
> The Netherlands
>
Author
9 Jun 2009 3:36 PM
Co
Show quote Hide quote
On 9 jun, 17:25, "Tom Dacon" <tda...@community.nospam> wrote:
> A better way to do this would be to just handle the SizeChanged event of the
> ListView. There's no need to override WndProc to do it, and even if you
> chose to do so the WM_PAINT message is definitely the wrong message.
>
> Tom Dacon
> Dacon Software Consulting
>
> "Co" <vonclausow***@gmail.com> wrote in message
>
> news:08f01104-f596-4a44-a013-8c681cfc74da@c9g2000yqm.googlegroups.com...
>
> > Hi All,
>
> > I'm trying to override the wndproc to resize the last column of my
> > listview.
> > The code however doesn't do anything.
> > What am I doing wrong?
>
> > Public Class LvSort
> >    Inherits System.Windows.Forms.ListView
>
> >    Protected Overrides Sub WndProc(ByRef m As Message)
> >        MyBase.WndProc(m)
> >        If m.Msg = WM_PAINT Then
> >            Explorer1.ListView.Columns
> > (Explorer1.ListView.Columns.Count - 1).Width = -2
> >        End If
> >    End Sub
>
> > End Class
>
> > Regards
> > Marco
> > The Netherlands

Tom,

could you show me an example of this method?

Marco
Author
9 Jun 2009 8:03 PM
Tom Dacon
Show quote Hide quote
"Co" <vonclausow***@gmail.com> wrote in message
news:f4aff691-c2f8-462d-af5e-4d4be4df1362@o18g2000yqi.googlegroups.com...
On 9 jun, 17:25, "Tom Dacon" <tda...@community.nospam> wrote:
> A better way to do this would be to just handle the SizeChanged event of
> the
> ListView. There's no need to override WndProc to do it, and even if you
> chose to do so the WM_PAINT message is definitely the wrong message.
>
> Tom Dacon
> Dacon Software Consulting
>
> "Co" <vonclausow***@gmail.com> wrote in message
>
> news:08f01104-f596-4a44-a013-8c681cfc74da@c9g2000yqm.googlegroups.com...
>
> > Hi All,
>
> > I'm trying to override the wndproc to resize the last column of my
> > listview.
> > The code however doesn't do anything.
> > What am I doing wrong?
>
> > Public Class LvSort
> > Inherits System.Windows.Forms.ListView
>
> > Protected Overrides Sub WndProc(ByRef m As Message)
> > MyBase.WndProc(m)
> > If m.Msg = WM_PAINT Then
> > Explorer1.ListView.Columns
> > (Explorer1.ListView.Columns.Count - 1).Width = -2
> > End If
> > End Sub
>
> > End Class
>
> > Regards
> > Marco
> > The Netherlands

Tom,

could you show me an example of this method?

Marco


Marco, this is right out of one of my own applications. It's a two-column
ListView, where the first column starts out at a default width but might get
dynamically resized by the user, and the second column always takes up the
rest of the space.

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Resize the last column of a two-column ListView
        ''' so that column two takes up all the rest of the space
        ''' (the first column either keeps its default size or
        '''  may be resized dynamically by the user)
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks>
        ''' </remarks>
        ''' -----------------------------------------------------------------------------
        Private Sub lvwExplorer_SizeChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) _
            Handles lvwExplorer.SizeChanged

            Dim ctl As ListView = CType(sender, ListView)

            Try
                Dim desiredWidth As Integer =
Math.Max((ctl.ClientSize.Width - ctl.Columns(0).Width - 1), 2)
                If ctl.Columns(1).Width <> desiredWidth Then
                    ctl.Columns(1).Width = desiredWidth
                End If
            Catch
                '   Silently absorb any exception.
            End Try

        End Sub

Tom