Click here to Skip to main content
11,924,995 members (47,455 online)
Click here to Skip to main content
Add your own
alternative version


65 bookmarked

Autosize the last column in a ListView control using WndProc

, 24 Nov 2002
Rate this:
Please Sign up or sign in to vote.
Shows a quick and dirty way to auto-size the last column of a ListView control.

Sample Image - ListViewAutoSize.jpg


It has always been a pet-peeve of mine, that I prefer grid or list controls to auto size their columns to fit the control canvas. Some of you may remember the early OCX and ActiveX controls that implemented auto-size last column properties to resolve this problem. Essentially, the last column in the grid or list view is automatically expanded to take up any available space. As the form is resized, or column widths are resized by the user at runtime, the last column size is automatically increased or decreased to keep the column aligned with the control right edge.

I love the Windows Explorer look, and I love the ListView control features and appearance - we just have to do something with that last column! Having scoured the internet trying to find examples or clues, the end result that, finding a solution proved to be difficult, but implementing the solution was actually very simple.

Setting the Column Size

In fact, the ListView class does provide a ColumnHeader.Width property. The MSDN documentation describes two special values related to auto-sizing the column, as follows: "To adjust the width of the longest item in the column, set the Width property to -1. To auto size to the width of the column heading, set the Width property to -2".

A special feature of setting the Width to -2, is that it ALSO automatically expands the last column to the right-edge of the control. This would seem to solve the problem, except that it does it a one-time auto-size that is not preserved if the user resizes any of the column widths at runtime, or the ListView control is set to resize with the form.

Responding to Change

When I first approached this problem, I figured the answer would lie in responding to some event when the user resized a column or the control was resized, or sub-classing the ListView to gain access to some protected interfaces. In fact, the ColumnHeaderCollection and ColumnHeader classes used to provide the implementation of the ListView.Columns member are completely buried inside the parent control.

My next approach was to look to the Win32 API, but faced the same obstacles. I could find no examples of how to access the ListView column headers directly.

The final step to any tricky problem like this was to look to the Window message loop. A handy feature of .NET controls is they expose the WndProc method for handling messages received by the control. By analyzing the messages that were pumped through the ListView control while columns widths were resized, or the control was resized, I discovered that the hidden ColumnHeader class sends a WM_PAINT message to the parent control to notify it during a column resize, and as a final step when the user finishes resizing the column. The WM_PAINT method is also the last message processed when the user resizes the ListView control.

The Solution

To implement the the auto-size of the last column, subclass the ListView control, and override the WndProc method.

protected override void WndProc( ref Message message )
    const int WM_PAINT = 0xf ;

    // if the control is in details view mode and columns
    // have been added, then intercept the WM_PAINT message
    // and reset the last column width to fill the list view
    switch ( message.Msg )
    case WM_PAINT:
        if ( this.View == View.Details && this.Columns.Count > 0 )
            this.Columns[this.Columns.Count - 1].Width = -2 ;
        break ;

    // pass messages on to the base control for processing
    base.WndProc( ref message ) ;


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Chris Beckett
Web Developer
United States United States
Chris Beckett has been contributing to the analysis, design and development of distributed enterprise-level business systems for more than 16 years, with more than 10 years in a technical leadership role. He has delivered systems in the government, banking, broadcasting, entertainment, manufacturing, and finance industries.

Chris Beckett continues to be an active .NET architect, designer and developer, and a strong advocate for best practices in Quality Assurance and Lifecycle Management.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 3 Pin
Zasky19-Jan-12 2:20
memberZasky19-Jan-12 2:20 
GeneralNicely done - can use a NativeWindow to avoid subclassing the ListView Pin
SBendBuckeye13-Jan-10 15:49
memberSBendBuckeye13-Jan-10 15:49 
Generalproblem when columns reordered Pin
zobis30-Nov-09 8:48
memberzobis30-Nov-09 8:48 
QuestionHow can I Align to Right? Pin
serhhio24-Jun-09 0:21
memberserhhio24-Jun-09 0:21 
QuestionNice concept!can i use it in VC++? Pin
"_$h@nky_"8-Oct-08 3:39
member"_$h@nky_"8-Oct-08 3:39 
Generalwork-around having just 1 column Pin
esskar30-Jan-08 7:09
memberesskar30-Jan-08 7:09 
GeneralGreat Solution Pin
Rakesh B Singh4-Jun-07 0:06
memberRakesh B Singh4-Jun-07 0:06 
GeneralUse the ClientSize property to get the internal width Pin
Todd Beaulieu17-May-07 5:30
memberTodd Beaulieu17-May-07 5:30 
GeneralSimplest & effective Alternative Pin
Ashutosh Bhawasinka29-Apr-07 18:00
memberAshutosh Bhawasinka29-Apr-07 18:00 
QuestionRe: Simplest & effective Alternative Pin
hullihulli17-Apr-09 12:16
memberhullihulli17-Apr-09 12:16 
GeneralRe: Simplest & effective Alternative Pin
serhhio24-Jun-09 0:19
memberserhhio24-Jun-09 0:19 
GeneralRe: Simplest & effective Alternative Pin
Zasky19-Jan-12 2:36
memberZasky19-Jan-12 2:36 
GeneralUnhandled exception in the designer and has been disabled Pin
puyopuy22-Apr-07 21:36
memberpuyopuy22-Apr-07 21:36 
GeneralRe: Unhandled exception in the designer and has been disabled Pin
Ashutosh Bhawasinka29-Apr-07 18:03
memberAshutosh Bhawasinka29-Apr-07 18:03 
GeneralBetter way:2 lines solution Pin
dfer31-Jan-07 7:12
memberdfer31-Jan-07 7:12 
GeneralRe: Better way:2 lines solution Pin
flyingxu3-Jun-09 16:27
memberflyingxu3-Jun-09 16:27 
GeneralRe: Better way:2 lines solution Pin
Rotted Frog20-Jun-09 23:25
memberRotted Frog20-Jun-09 23:25 
GeneralRe: Better way:2 lines solution Pin
serhhio24-Jun-09 0:23
memberserhhio24-Jun-09 0:23 
GeneralVery Nice Pin
natrajdev5-Nov-06 21:21
membernatrajdev5-Nov-06 21:21 
GeneralVery Nice Pin
natrajdev5-Nov-06 21:21
membernatrajdev5-Nov-06 21:21 
cmaissan7-Jun-06 15:04
membercmaissan7-Jun-06 15:04 
Generalother constants Pin
samuraj19-Oct-05 6:53
susssamuraj19-Oct-05 6:53 
GeneralRe: other constants Pin
<span style='color:#6699cc; '>MadHatter ¢</span>8-Aug-06 7:59
memberMadHatter ¢8-Aug-06 7:59 
Generalsubclassing not necessary Pin
ghost12079-Jun-05 3:44
memberghost12079-Jun-05 3:44 
GeneralRe: subclassing not necessary Pin
VBProEd1-Jul-05 11:34
memberVBProEd1-Jul-05 11:34 
GeneralRe: subclassing not necessary Pin
Anonymous11-Aug-05 12:06
sussAnonymous11-Aug-05 12:06 
GeneralRe: subclassing not necessary Pin
Grenticha25-Dec-05 1:55
memberGrenticha25-Dec-05 1:55 
GeneralSetting a minimum column Width Pin
Abhijeet Patel3-Mar-05 8:14
memberAbhijeet Patel3-Mar-05 8:14 
GeneralRe: Setting a minimum column Width Pin
memberANIL KUMAR SHARMA (INDIA)25-May-06 22:51 
AnswerRe: Setting a minimum column Width Pin
Michelangelo_PM26-Jun-09 6:10
memberMichelangelo_PM26-Jun-09 6:10 
GeneralWhile reducing size, an unnecessary vertical scrollbar appears Pin
Quatschkopp20-Feb-05 7:35
memberQuatschkopp20-Feb-05 7:35 

I implemented the code above and generally it works. But when I reduce the size of my ListView-Control a vertical scrollbar appears unnecessarily. This scrollbar remains until I increase the size again.

Does exists a workaround for this problem? Anybody out there with the same problem?

Thanks Stefan
GeneralRe: While reducing size, an unnecessary vertical scrollbar appears Pin
Grenticha25-Dec-05 1:53
memberGrenticha25-Dec-05 1:53 
GeneralAllowColumnReorder Pin
zilch23-Jun-04 17:20
memberzilch23-Jun-04 17:20 
GeneralNice and simple Pin
PingIT25-Mar-04 18:49
memberPingIT25-Mar-04 18:49 
GeneralRe: A one line solution Pin
Chris Beckett3-Mar-04 6:09
memberChris Beckett3-Mar-04 6:09 
GeneralLayout event and MouseMove event Pin
Anonymous11-Aug-05 12:04
sussAnonymous11-Aug-05 12:04 
QuestionHow do you make two seperate listviews sync when one is scrolled? Pin
FocusedWolf9-Jan-04 11:53
memberFocusedWolf9-Jan-04 11:53 
GeneralProblem when change column order !!! Pin
spikenguyen10-Dec-03 14:13
sussspikenguyen10-Dec-03 14:13 
GeneralRe: WM_SIZE and WM_NOTIFY instead of WM_PAINT Pin
Anonymous24-Nov-03 23:55
sussAnonymous24-Nov-03 23:55 
GeneralNICE !!! Pin
Simon Segal28-May-03 16:20
memberSimon Segal28-May-03 16:20 
GeneralLabelEdit No Longer Works! Pin
glancep1-Jan-03 17:45
sussglancep1-Jan-03 17:45 
GeneralRe: LabelEdit No Longer Works! Pin
Michael Coyle8-May-08 21:16
memberMichael Coyle8-May-08 21:16 
GeneralCool Pin
Christian Graus25-Nov-02 13:17
memberChristian Graus25-Nov-02 13:17 
GeneralOops...not very efficient Pin
Satch25-Nov-02 8:14
memberSatch25-Nov-02 8:14 
GeneralRe: Oops...not very efficient Pin
Satch25-Nov-02 8:20
memberSatch25-Nov-02 8:20 
GeneralRe: Oops...not very efficient Pin
Christian Graus25-Nov-02 13:18
memberChristian Graus25-Nov-02 13:18 
GeneralRe: Oops...not very efficient Pin
Satch25-Nov-02 15:17
sussSatch25-Nov-02 15:17 
GeneralRe: Oops...not very efficient Pin
Christian Graus25-Nov-02 15:46
memberChristian Graus25-Nov-02 15:46 
Rashid Thadha24-Nov-02 23:22
memberRashid Thadha24-Nov-02 23:22 
.S.Rod.25-Nov-02 5:37
member.S.Rod.25-Nov-02 5:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 25 Nov 2002
Article Copyright 2002 by Chris Beckett
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid