Autosize the last column in a ListView control using WndProc

C

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
 
T

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
 
C

Co

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

Tom,

could you show me an example of this method?

Marco
 
T

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

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top