Results 1 to 13 of 13

Thread: VB - ListView; LabelEdit for SubItems

  1. #1

    Thread Starter
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    VB - ListView; LabelEdit for SubItems

    The ListView control available from the Windows Common Controls does provide the ability to edit the labels of items. But, when set to Report mode, it only allows editing of the text in the first column. The SubItems in the other columns cannot be directly edited.

    This module allows editing of the items in all columns. It uses a textbox to let the user edit listitems.

    Please download the attachment for an example of its usage.

    • Unlike the previous version, the code is now placed inside a class. That makes it easier to use and doesn't clutter up existing code. Also multiple instances can run at once, so LabelEditing can be applied to several ListViews at the same time.
    • The code now works with ListViews with horizontal scrollbars.
    • You no longer have to place a textbox on your form for the editing; the class takes care of that by itself.
    • This code works with the Windows Common Controls 6 ListView (not with the Common Controls 5 component). With a bit of tweaking it can also be applied to the vbAccelerator ListView (in that case the subclassing code can be removed).
    • The ScaleMode of the form has to be 1-Twip for this code to work.


    The LabelEdit class:
    VB Code:
    1. Option Explicit
    2.  
    3. '===============================================================
    4. 'ListView LabelEdit
    5. '© 2004 by Michiel Meulendijk
    6. '
    7. 'This code enables label editing for SubItems in ListViews.
    8. 'By default, when using the LabelEdit property, only the first
    9. 'ListItem of a ListView can be edited. With this code all
    10. 'ListSubItems can be edited as well.
    11. '
    12. 'This code is contained within a class, so multiple instances
    13. 'can run at the same time (e.g. more ListViews on one form can
    14. 'all support label editing).
    15. '
    16. 'This file is provided "as is" with no expressed or implied
    17. 'warranty. The author accepts no liability for any damage caused
    18. 'to your system because of using this code.
    19. '===============================================================
    20.  
    21. Private Declare Function GetScrollInfo Lib "user32.dll" ( _
    22.                 ByVal hwnd As Long, ByVal n As Long, _
    23.                 lpScrollInfo As SCROLLINFO) As Long
    24.  
    25. Private Type SCROLLINFO
    26.     cbSize As Long
    27.     fMask As Long
    28.     nMin As Long
    29.     nMax As Long
    30.     nPage As Long
    31.     nPos As Long
    32.     nTrackPos As Long
    33. End Type
    34.  
    35. Private Const SB_HORZ = 0
    36. Private Const SB_VERT = 1
    37. Private Const SIF_POS = &H4
    38.  
    39. Dim WithEvents txtEdit As TextBox
    40. Dim WithEvents ltvListView As ListView
    41. Dim objItem As Object
    42.  
    43. Public Sub Init(ByRef ctlForm As Form, ByRef ctlListView As ListView)
    44. 'Initiates object. Adds textbox control.
    45. Set ltvListView = ctlListView
    46. Set txtEdit = ctlForm.Controls.Add("VB.TextBox", "txtLabelEdit_" & ctlListView.Name)
    47. Set txtEdit.Container = ctlListView.Container
    48. Set txtEdit.Font = ctlListView.Font
    49. txtEdit.Appearance = 0
    50. txtEdit.ForeColor = vbHighlight
    51. 'Subclass listview
    52. SubClassWnd ltvListView.hwnd, Me
    53. End Sub
    54.  
    55. Private Function GetHorizontalScroll() As Long
    56. 'Returns the position of the horizontal scroll bar
    57. Dim scrInfo As SCROLLINFO
    58. scrInfo.cbSize = LenB(scrInfo)
    59. scrInfo.fMask = SIF_POS
    60. GetScrollInfo ltvListView.hwnd, SB_HORZ, scrInfo
    61. GetHorizontalScroll = scrInfo.nPos
    62. End Function
    63.  
    64. Private Sub EditText(ByVal x As Integer, ByVal y As Integer)
    65. 'Handles label editing
    66. On Error GoTo endSub
    67. Dim i As Integer, objCol As ColumnHeader, lngScroll As Long
    68.  
    69. lngScroll = GetHorizontalScroll * Screen.TwipsPerPixelX
    70. x = x + lngScroll
    71.  
    72. For i = 1 To ltvListView.ColumnHeaders.Count
    73.     If x < ltvListView.ColumnHeaders.Item(1).Width Or ltvListView.ColumnHeaders.Count = 1 Then
    74.         Set objCol = ltvListView.ColumnHeaders.Item(1)
    75.         Set objItem = ltvListView.SelectedItem
    76.         Exit For
    77.     ElseIf x < ltvListView.ColumnHeaders.Item(i).Left Then
    78.         Set objCol = ltvListView.ColumnHeaders.Item(i - 1)
    79.         Set objItem = ltvListView.SelectedItem.ListSubItems.Item(i - 2)
    80.         Exit For
    81.     ElseIf i = ltvListView.ColumnHeaders.Count Then
    82.         Set objCol = ltvListView.ColumnHeaders(i)
    83.         Set objItem = ltvListView.SelectedItem.ListSubItems.Item(i - 1)
    84.         Exit For
    85.     End If
    86. Next i
    87.  
    88. txtEdit.BorderStyle = 0
    89. txtEdit.Left = ltvListView.Left + objCol.Left - lngScroll
    90. txtEdit.Top = ltvListView.Top + ltvListView.SelectedItem.Top
    91. txtEdit.Width = objCol.Width
    92. txtEdit.Height = ltvListView.SelectedItem.Height
    93. txtEdit.BorderStyle = 1
    94.  
    95. txtEdit.Text = objItem.Text
    96. txtEdit.SelStart = 0
    97. txtEdit.SelLength = Len(txtEdit)
    98. txtEdit.Visible = True
    99. txtEdit.SetFocus
    100.  
    101. endSub:
    102. End Sub
    103.  
    104. Public Sub SetText()
    105. On Error Resume Next
    106. objItem.Text = txtEdit.Text
    107. txtEdit.Visible = False
    108. End Sub
    109.  
    110. Private Sub Class_Terminate()
    111. UnSubClassWnd ltvListView.hwnd
    112. Set txtEdit = Nothing
    113. Set ltvListView = Nothing
    114. End Sub
    115.  
    116. Private Sub ltvListView_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    117. EditText x, y
    118. End Sub
    119.  
    120. Private Sub txtEdit_KeyDown(KeyCode As Integer, Shift As Integer)
    121. If KeyCode = vbKeyReturn Then SetText
    122. End Sub
    123.  
    124. Private Sub txtEdit_LostFocus()
    125. SetText
    126. End Sub
    The SubClassing Module:
    VB Code:
    1. Option Explicit
    2.  
    3. '===============================================================
    4. 'ListView LabelEdit
    5. '© 2004 by Michiel Meulendijk
    6. '
    7. 'This module handles the subclassing and belongs to the
    8. 'LabelEdit class module.
    9. '===============================================================
    10.  
    11. Private Declare Function GetWindowLong Lib "user32" _
    12.                 Alias "GetWindowLongA" (ByVal hwnd As Long, _
    13.                 ByVal nIndex As Long) As Long
    14. Private Declare Function SetWindowLong& Lib "user32" _
    15.                 Alias "SetWindowLongA" (ByVal hwnd As Long, _
    16.                 ByVal nIndex As Long, ByVal dwNewLong As Long)
    17. Private Declare Function CallWindowProc Lib "user32" Alias _
    18.                 "CallWindowProcA" (ByVal lpPrevWndFunc As Long, _
    19.                 ByVal hwnd As Long, ByVal msg As Long, _
    20.                 ByVal wParam As Long, ByVal lParam As Long) As Long
    21.  
    22. Private Const GWL_WNDPROC = (-4)
    23. Private Const WM_VSCROLL = &H115
    24. Private Const WM_HSCROLL = &H114
    25.  
    26. Dim WndProcOld As Long
    27. Dim colWnd As Collection
    28. Dim colClass As Collection
    29.  
    30. 'SubClass Code
    31. Public Function WindProc(ByVal hwnd As Long, ByVal wMsg As Long, _
    32.        ByVal wParam As Long, ByVal lParam As Long) As Long
    33. If wMsg = WM_VSCROLL Or wMsg = WM_HSCROLL Then colClass.Item("H" & hwnd).SetText
    34. WindProc = CallWindowProc(WndProcOld&, hwnd&, wMsg&, wParam&, lParam&)
    35. End Function
    36.  
    37. Public Sub InitSubClass()
    38. Set colClass = New Collection
    39. End Sub
    40.  
    41. Public Sub CloseSubClass()
    42. Set colClass = Nothing
    43. End Sub
    44.  
    45. Public Sub SubClassWnd(hwnd As Long, Class As Object)
    46. colClass.Add Class, "H" & hwnd
    47. WndProcOld& = SetWindowLong(hwnd, GWL_WNDPROC, AddressOf WindProc)
    48. End Sub
    49.  
    50. Public Sub UnSubClassWnd(hwnd As Long)
    51. SetWindowLong hwnd, GWL_WNDPROC, WndProcOld&
    52. WndProcOld& = 0
    53. End Sub
    Attached Files Attached Files
    Last edited by TheVader; Dec 29th, 2004 at 02:49 PM.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  2. #2
    Big D Danial's Avatar
    Join Date
    Jul 2000
    Location
    ASP.Net Forum
    Posts
    2,877
    Brilliant code

    Just one problem, if your listview has horizontal scroll bar then it gets all wired, if you could fix that, it would be perfect.

    Thanks.
    [VBF RSS Feed]

    There is a great war coming. Are you sure you are on the right side? Atleast I have chosen a side.

    If I have been helpful, Please Rate my Post. Thanks.

    This post was powered by :

  3. #3
    Member
    Join Date
    Mar 2004
    Location
    Florida
    Posts
    44

    Post Re: VB - ListView; LabelEdit for SubItems

    This is what I've been trying to do...
    The code above posted by vader works nicely when theres 4 or more columns.
    I was trying to figure out why when theres only 2 columns its only editing column 1. Even if I use the demo, set the LV for only 2 columns and click column2 samething.
    I'm stumped... I tried changing somethings around, can't figure it out.
    Anyone got any idea's ?

    Any help/input much appreciated..
    $hep

  4. #4

    Thread Starter
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    Re: VB - ListView; LabelEdit for SubItems

    I've tried editing with two columns and it works fine for me... With only one column it gives an error, though.

    This code could surely use an update. A bit more reliability wouldn't hurt, and the horizontal scrollbar problem pointed out by Danial should be fixed too.

    Anyway, I attached an example with two columns, which works fine for me. Does that work?
    Attached Files Attached Files
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  5. #5

    Thread Starter
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    Resolved Re: VB - ListView; LabelEdit for SubItems

    I've completely revised the code today; the scrollbar bug has been fixed and the code is now much easier to implement. Check out the first post in the thread.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  6. #6
    Member
    Join Date
    Mar 2004
    Location
    Florida
    Posts
    44

    Re: VB - ListView; LabelEdit for SubItems

    I started to think I had messed it up when I seen your reply post, cause you said it worked fine for you.. I've posted a picture just so you don't think I'm crazy.
    Basically all I did to your demo just to check, was delete 2 columns..
    But this pic is from the demo you said works for you. Samething.(Looks like your editing in col 2 but edits col 1)
    But the revised code(class) seems to correct it. Being as you've made it into a class its much better and it works very nicely.. Thanks for your help and sharing...

    Oh also why the subclassing ?
    When I commented out the Call SubClassWnd(ltv.hwnd) to step through, it seemed to funtion the same.
    To control painting?

    Thanks again,
    $hep
    Attached Images Attached Images  
    Last edited by $hep; Dec 29th, 2004 at 11:12 PM.

  7. #7

    Thread Starter
    Fanatic Member TheVader's Avatar
    Join Date
    Oct 2002
    Location
    Rotterdam, the Netherlands
    Posts
    871

    Re: VB - ListView; LabelEdit for SubItems

    You're welcome.
    Quote Originally Posted by $hep
    Oh also why the subclassing ?
    When I commented out the Call SubClassWnd(ltv.hwnd) to step through, it seemed to funtion the same.
    To control painting?
    To capture the Scroll messages. If you're editing an item and scroll the listview, the textbox doesn't lose focus, and so it stays visible.
    Author for Visual Basic Web Magazine

    My articles on the Web Browser Control:
    Using the Web Browser Control & Using the DHTML Document Object Model

    The examples referenced in the articles can be found here:

  8. #8
    Member
    Join Date
    Mar 2004
    Location
    Florida
    Posts
    44

    Re: VB - ListView; LabelEdit for SubItems

    Oh... That makes since...

    This helps me alot.
    Thanks
    $hep

  9. #9
    Member
    Join Date
    Mar 2004
    Location
    Florida
    Posts
    44

    Re: VB - ListView; LabelEdit for SubItems

    Hi again,
    Another question.. How would I go about capturing the tab key when in edit .
    Like say you were editing in column 1 and you hit [tab].
    I've tried different events of the txtEdit and the Listview.

    KeyUp - Doesn't catch tab.
    KeyDown - In a normal txtbox if I set tabstop=False this catches [tab] can't get it to work in the class.
    KeyPress - Doesn't catch tab.
    LostFocus - Reacts to tab but can't get keycode.

    I'm tring to add like a tab order to the columns.
    If your editing in col 1 hit tab and begin editing in col 2.

    Thanks for any help\input
    $hep

    [RESOLVED]-The only way I could resolve this was to set every control's tabstop=false. But it works, its just dirty.
    Last edited by $hep; Jan 16th, 2005 at 07:11 PM.

  10. #10
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB - ListView; LabelEdit for SubItems

    I've got this problem, (attached is the screenshot), why is it not fitted exactly? Here is the minor modifications I did so the editing will be triggered after double-click...

    VB Code:
    1. Dim xPos As Single, yPos As Single
    2.  
    3. Private Sub ltvListView_DblClick()
    4.     EditText xPos, yPos
    5. End Sub
    6.  
    7. Private Sub ltvListView_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    8.     xPos = x
    9.     yPos = y
    10. End Sub
    Attached Images Attached Images  
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  11. #11
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: VB - ListView; LabelEdit for SubItems

    Quote Originally Posted by dee-u
    I've got this problem, (attached is the screenshot), why is it not fitted exactly? Here is the minor modifications I did so the editing will be triggered after double-click...

    VB Code:
    1. Dim xPos As Single, yPos As Single
    2.  
    3. Private Sub ltvListView_DblClick()
    4.     EditText xPos, yPos
    5. End Sub
    6.  
    7. Private Sub ltvListView_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
    8.     xPos = x
    9.     yPos = y
    10. End Sub
    You need to make the style of the listview the same as the original, Appearence and BorderStyle set to 0. Or calculate those and reposition the textbox by adding those to your calculations.


    Has someone helped you? Then you can Rate their helpful post.

  12. #12
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,127

    Re: VB - ListView; LabelEdit for SubItems

    I've given up on this, the subclassing is crashing my IDE and I have found a similar technique but without resorting to subclassing...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  13. #13
    Hyperactive Member
    Join Date
    Mar 2006
    Posts
    282

    Re: VB - ListView; LabelEdit for SubItems

    hello,

    If I edit 1st column, how can I directly change subitem colum 3 and 4 from database ( for example )

    If I add number 50 to frist coulumn I want to change subitem 3 and 4 in that column with data from mysql database.

    I just need some example how can be subitem changed when 1st column is edited.

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