Results 1 to 19 of 19

Thread: [VB6] Lock ListView Columns

  1. #1

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    [VB6] Lock ListView Columns

    This code prevents the specified column(s) of a ListView control from being resized by the user. It does that by subclassing the ListView control and watching for the HDN_BEGINTRACK and HDN_DIVIDERDBLCLICK notification codes. Additionally, it also provides appropriate feedback to the user by displaying the "Unavailable" cursor when the mouse is over the locked column divider. That is done by subclassing the ListView's Header control and handling the WM_SETCURSOR message.


    The modLockLVCols.bas file in the attached project below has been inspired by the codes in the following threads:

    Preventing certain Listview columns from sizing...

    [RESOLVED] Prevent User From Resizing Column Width in ListView


    Also included in the attachment is frmLockLVColsDemo.frm:


    Name:  Lock ListView Columns.png
Views: 9815
Size:  14.0 KB
    Attached Files Attached Files
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  2. #2
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    5,204

    Re: [VB6] Lock ListView Columns

    tnk you very much bonnie very useful

  3. #3
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by Bonnie West View Post
    This code prevents the specified column(s) of a ListView control from being resized by the user. It does that by subclassing the ListView control and watching for the HDN_BEGINTRACK and HDN_DIVIDERDBLCLICK notification codes. Additionally, it also provides appropriate feedback to the user by displaying the "Unavailable" cursor when the mouse is over the locked column divider. That is done by subclassing the ListView's Header control and handling the WM_SETCURSOR message.


    The modLockLVCols.bas file in the attached project below has been inspired by the codes in the following threads:

    Preventing certain Listview columns from sizing...

    [RESOLVED] Prevent User From Resizing Column Width in ListView


    Also included in the attachment is frmLockLVColsDemo.frm:


    Name:  Lock ListView Columns.png
Views: 9815
Size:  14.0 KB
    hi...
    possible to use this code in a vba userform and listview in VBA for Excel?
    Tks.

  4. #4

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by luca90 View Post
    hi...
    possible to use this code in a vba userform and listview in VBA for Excel?
    Tks.
    Sorry, but I have not tested that code in VBA. The Subclass function, however, will let you know if it works there. So, try it and see what happens.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  5. #5
    PowerPoster
    Join Date
    Feb 2006
    Posts
    24,482

    Re: [VB6] Lock ListView Columns

    Glad to see that you included visual feedback.

  6. #6

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by dilettante View Post
    Glad to see that you included visual feedback.
    Yeah, I got that idea from Chris001, who has been wondering how to accomplish that:

    Quote Originally Posted by Chris001 View Post
    Unfortunately I still haven't found a way to hide the double-arrow cursor when moving over a locked column divider. WM_SETCURSOR doesn't 'fire' when the cursor is moved over a column divider.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  7. #7
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by Bonnie West View Post
    Sorry, but I have not tested that code in VBA. The Subclass function, however, will let you know if it works there. So, try it and see what happens.
    HI...
    THE CLASS WORK PERFECT ALSO IN VBA FOR deXCEL!!!!!!!!!!!!!!!!!

    But i dont like the signal on the cursor...
    possible to change with the standard cursor?

  8. #8

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by luca90 View Post
    THE CLASS WORK PERFECT ALSO IN VBA FOR deXCEL!!!!!!!!!!!!!!!!!
    Thanks for testing!

    Quote Originally Posted by luca90 View Post
    But i dont like the signal on the cursor...
    possible to change with the standard cursor?
    Yes. Just add this constant declaration for the standard arrow cursor:

    Code:
    Private Const IDC_ARROW As Long = 32512
    and replace the line:

    Code:
    m_hCursor = LoadCursorW(0&, IDC_NO)
    with:

    Code:
    m_hCursor = LoadCursorW(0&, IDC_ARROW)
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  9. #9
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by Bonnie West View Post
    Thanks for testing!



    Yes. Just add this constant declaration for the standard arrow cursor:

    Code:
    Private Const IDC_ARROW As Long = 32512
    and replace the line:

    Code:
    m_hCursor = LoadCursorW(0&, IDC_NO)
    with:

    Code:
    m_hCursor = LoadCursorW(0&, IDC_ARROW)

    oPS!

    i have 12 column in lv1 and i i filled the array in this mode:
    Subclass ListView1.hWnd, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

    is this correct?

    Why?...
    all column are lockhed but the last not(?!), i can enlarge only the column 12.(the column 12 have size (0))

    tkx u in other case for code.

    perphs i can understand, see here:
    http://www.vbforums.com/showthread.p...ns-from-sizing

    see post #33
    Last edited by luca90; Dec 31st, 2013 at 06:23 AM.

  10. #10

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by luca90 View Post
    i have 12 column in lv1 and i i filled the array in this mode:
    Subclass ListView1.hWnd, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11

    is this correct?

    Why?...
    all column are lockhed but the last not(?!), i can enlarge only the column 12.(the column 12 have size (0))

    . . .

    perphs i can understand, see here:
    http://www.vbforums.com/showthread.p...ns-from-sizing

    see post #33
    I'm sorry, but could you please attach a screenshot of your ListView control here (click the Insert Image button on the editor's toolbar)? Please also state which column(s) of your ListView that you want to be locked.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  11. #11
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by Bonnie West View Post
    I'm sorry, but could you please attach a screenshot of your ListView control here (click the Insert Image button on the editor's toolbar)? Please also state which column(s) of your ListView that you want to be locked.
    after the last visible column a just have other 5 columns with size(0) and are hide(are only a service column) not important to show tath.
    Attached Images Attached Images  

  12. #12

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by luca90 View Post
    after the last visible column a just have other 5 columns with size(0) and are hide(are only a service column) not important to show tath.
    So, your ListView control has a total of 15 columns, of which 10 are visible and 5 are hidden. Is that right?

    I'll ask this again: which column(s) do you want to be locked?
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  13. #13
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by Bonnie West View Post
    So, your ListView control has a total of 15 columns, of which 10 are visible and 5 are hidden. Is that right?

    I'll ask this again: which column(s) do you want to be locked?
    So, your ListView control has a total of 15 columns, of which 10 are visible and 5 are hidden. Is that right?
    YES!.

    which column(s) do you want to be locked?
    Only the visible columns (are 10)

  14. #14

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by luca90 View Post
    Only the visible columns (are 10)
    Well, in that case, here's how you would call the Subclass function:

    Code:
    Subclass ListView1.hWnd, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
    Remember, the first column has an index of 0.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  15. #15
    New Member
    Join Date
    Jan 2015
    Posts
    1

    Re: [VB6] Lock ListView Columns

    Thanks very much for this Bonnie!

    Just a quick comment... Like luca90 I'm using your code in XL VBA. I've noticed that if any of the locked columns have a width of zero, then the "resize" pointer still shows when hovering between the two (non zero width) columns either side of the zero width column. Not really a problem, as the locking functionality persists.

    Once again thank you. Up till now I've had to rely on various form events to trigger a sub which reset all my column widths. Flicker city!

  16. #16
    PowerPoster
    Join Date
    Mar 2005
    Posts
    2,580

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by Bonnie West View Post
    Thanks for testing!



    Yes. Just add this constant declaration for the standard arrow cursor:

    Code:
    Private Const IDC_ARROW As Long = 32512
    and replace the line:

    Code:
    m_hCursor = LoadCursorW(0&, IDC_NO)
    with:

    Code:
    m_hCursor = LoadCursorW(0&, IDC_ARROW)
    hi friend, sorry if i post on old th3d

    but have prb when exit from form with unload me...
    i have see other form are freezed, and ivba ide work not correctlly..
    perphs i need to destroyed a object opened from lock listview routine?

    in other case the code work!

  17. #17

    Thread Starter
    Default Member Bonnie West's Avatar
    Join Date
    Jun 2012
    Location
    InIDE
    Posts
    4,060

    Re: [VB6] Lock ListView Columns

    Quote Originally Posted by luca90 View Post
    but have prb when exit from form with unload me...
    i have see other form are freezed, and ivba ide work not correctlly..
    perphs i need to destroyed a object opened from lock listview routine?
    Can you post the code in your Form's Unload event? (You're not using the End statement, are you?)



    EDIT

    Quote Originally Posted by blackworx View Post
    Just a quick comment... Like luca90 I'm using your code in XL VBA. I've noticed that if any of the locked columns have a width of zero, then the "resize" pointer still shows when hovering between the two (non zero width) columns either side of the zero width column. Not really a problem, as the locking functionality persists.
    This issue turns out to be quite easy to fix (thanks to Krool for suggesting this solution). Just add this constant declaration:

    Code:
    Private Const HHT_ONDIVOPEN As Long = &H8
    And then modify this line:

    Code:
    If HDHTI.Flags And HHT_ONDIVIDER Then
    To:

    Code:
    If HDHTI.Flags And (HHT_ONDIVIDER Or HHT_ONDIVOPEN) Then
    Last edited by Bonnie West; Jan 2nd, 2017 at 11:00 AM.
    On Local Error Resume Next: If Not Empty Is Nothing Then Do While Null: ReDim i(True To False) As Currency: Loop: Else Debug.Assert CCur(CLng(CInt(CBool(False Imp True Xor False Eqv True)))): Stop: On Local Error GoTo 0
    Declare Sub CrashVB Lib "msvbvm60" (Optional DontPassMe As Any)

  18. #18
    Member
    Join Date
    Apr 2016
    Posts
    61

    Re: [VB6] Lock ListView Columns

    Is there a less statement that will locked the listview column headers?

  19. #19
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    5,651

    Re: [VB6] Lock ListView Columns

    Do you mean a less complex way of doing it than what Bonnie has posted? Not really... you could do it with a few less lines but you still have to subclass and intercept HDN_BEGINTRACK

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width