Click here to Skip to main content
13,201,452 members (75,039 online)
Click here to Skip to main content
Add your own
alternative version


16 bookmarked
Posted 13 Mar 2006

Disable Column Resizing in ListView Report Mode

, 13 Mar 2006
Rate this:
Please Sign up or sign in to vote.
This article describes how to use the NativeWindow object to take control of the message processing of a ListView's column header. This allows the programmer to intercept messages and prevent or respond to things like column resizing.


I was recently tasked with figuring out a way to disable column resizing in a ListView control. After chasing leads on a Google search, reading documentation, and a little trial and error, I finally figured out how to do it. The solution is simple, elegant, and may be used as a basis for doing other things with the ListView control using the same technique.


When the ListView control is placed in report mode, an additional child window is added to the control to hold column headers. The Windows class for this child window is a SysHeader32 class. Unfortunately, it appears that Microsoft neglected to expose a wrapper for this control in the .NET Framework, and provided no simple means to access to it.

Ultimately, this means that a whole host of Windows messages are being sent to the SysHeader32 window of your ListView control that fires no events for your control and to which you cannot respond.


The solution is a three step process:

  1. Get a Windows handle for the ListView's SysHeader32 child window.
  2. Use the Windows handle to somehow take over message handling for its window.
  3. Write code to respond to the messages of interest to accomplish our goal.

Step 1

The first step is easy. Remember the good ol' days of Win32 API calls? Well...they still work as well in Visual Basic .NET as they did in VB6. Put the following declarations in a scope reachable by your form load event:

Private Declare Function GetWindow Lib "user32" Alias "GetWindow" _
       (ByVal hwnd As IntPtr, ByVal wCmd As Integer) As IntPtr
Private Const GW_CHILD As Integer = 5
Dim SysHdr32Handle As IntPtr

and in your form load event, add the following:

SysHdr32Handle = GetWindow(ListView1.Handle, GW_CHILD)

The code above assumes you've created a form window and drawn a ListView control on it. Since the ListView control only has one child window (the SysHeader32 window), we can rest easy knowing this call will obtain its Windows Handle.

Step 2

We now need a way to intercept messages being sent to the child window. Fortunately, the .NET Framework Class Library contains a class designed especially for this purpose - the NativeWindow class. The MSDN help description for the class states:

Provides a low-level encapsulation of a window handle and a window procedure.

To use the class, we need to create a new class that inherits the NativeWindow class so we can override the WndProc message handling function to do what we want. Add the following class definition to your form code:

Private Class ListViewHeader
    Inherits System.Windows.Forms.NativeWindow
    Private ptrHWnd As IntPtr

    Protected Overrides Sub WndProc(ByRef m As _
        ' We'll add this in step 3

    End Sub

    Protected Overrides Sub Finalize()
    End Sub

    Public Sub New(ByVal ControlHandle As IntPtr)
        ptrHWnd = ControlHandle
    End Sub
End Class

Of course, we'll need a form global variable declaration to work with, and we'll need to initialize our instance of the class. Put the following declaration in your form class declaration:

Private ListViewHeader1 As ListViewHeader

Then add the following line of code to your form load event:

SysHdr32Handle = _
  GetWindow(ListView1.Handle, GW_CHILD) ' <-- added this in step 1
ListViewHeader1 = New ListViewHeader(SysHdr32Handle)

Step 3

Now that we have a way to receive messages, we can add code to do something about them. In this simple example, we are going to intercept WM_SETCURSOR and WM_LBUTTONDOWN messages and throw them away to prevent the user from seeing a resize column cursor and from being able to resize the columns. Modify the WndProc method of our ListViewHeader class as follows:

Protected Overrides Sub WndProc(ByRef m _
          As System.Windows.Forms.Message)
    Select Case m.Msg
        Case Is = &H20  ' WM_SETCURSOR
            m.Msg = 0
        Case Is = &H201  ' WM_LBUTTONDOWN
            m.Msg = 0
    End Select

End Sub


The above example gives you a bare-bones method of getting a handle on those SysHeader32 messages. As such, the example is rather crude. For instance, what if you wanted to disable column resizing for two ListView controls? As written above, you'd have to create two distinct classes. A robust implementation would add events to the class definition and fire them when Windows messages are received in the WndProc method, thus allowing the creator of the class instance to deal with them. For now, however, this will get you started.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jeremy A. Wilson
Software Developer
United States United States
I've worked in the industry since 1992. After the dot com crash of 2001, I went back to school and graduated from the University of North Texas in 2005. I now live in the Dallas area with my wife and two children, and work as a senior software engineer for a local company.

I first learned to program on a Commodore 64 when I was 12 years old. The rest is history...

You may also be interested in...

Comments and Discussions

QuestionThis doesn't work on the Compact Framework 2.0 Pin
abdekker1233-Jul-12 6:23
memberabdekker1233-Jul-12 6:23 
AnswerRe: This doesn't work on the Compact Framework 2.0 Pin
bobbbbbbbbby17-Dec-12 6:56
memberbobbbbbbbbby17-Dec-12 6:56 
QuestionHow to - Disable double click and alow column click Pin
Martin Lowery14-Feb-10 2:52
memberMartin Lowery14-Feb-10 2:52 
Generaldobbeltclick between columnheads Pin
Megabitdk21-Apr-08 2:58
memberMegabitdk21-Apr-08 2:58 
GeneralRe: dobbeltclick between columnheads Pin
TheLarrikin28-Sep-09 7:47
memberTheLarrikin28-Sep-09 7:47 
QuestionPlaying with View mode Pin
klaus@gubbernet.com21-Nov-06 22:27
memberklaus@gubbernet.com21-Nov-06 22:27 
QuestionPrevent certain columns resizing Pin
Neil22027114-Nov-06 7:25
memberNeil22027114-Nov-06 7:25 
AnswerRe: Prevent certain columns resizing alt approach Pin
pggcoding22-Jun-13 19:50
memberpggcoding22-Jun-13 19:50 
QuestionThe code? Pin
Clorange27-Sep-06 20:18
memberClorange27-Sep-06 20:18 
Generalusing in Derived class Pin
Mohsen Kokabi4-Jul-06 15:09
memberMohsen Kokabi4-Jul-06 15:09 
GeneralRe: using in Derived class Pin
esskar26-Sep-07 1:25
memberesskar26-Sep-07 1:25 
GeneralThanks a lot Pin
dl4gbe14-Mar-06 1:13
memberdl4gbe14-Mar-06 1:13 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.171020.1 | Last Updated 13 Mar 2006
Article Copyright 2006 by Jeremy A. Wilson
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid