Results 1 to 5 of 5

Thread: [RESOLVED] Listview question

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Resolved [RESOLVED] Listview question

    I want to use the listview double-click event to edit sub-item information in the listview. How can I determine which sub-item was double-clicked?
    Last edited by Hack; Aug 2nd, 2006 at 06:11 AM. Reason: Added [RESOLVED] to thread title and green "resolved" checkmark

  2. #2
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Listview question

    I use a seperate popupmenu when you right-click on a ListView item.
    VB Code:
    1. Private Sub lvwPrograms_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
    2.  
    3. If Button = 2 Then
    4.     PopupMenu mnuEditTop
    5. End If
    6.  
    7. End Sub
    In the mnuEditTop menu I have Delete Item, Edit Item and New Item. The Edit Item I have an Edit Form.

    If you want to use the DblClick event then you can do the same. Just send the ListView items to a Edit Form.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Listview question

    You will need to subclass LVM_SUBITEMHITTEST (and some other) message:
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const LVM_FIRST = &H1000
    4. Private Const LVHT_NOWHERE As Long = &H1
    5. Private Const LVHT_ONITEMICON As Long = &H2
    6. Private Const LVHT_ONITEMLABEL As Long = &H4
    7. Private Const LVHT_ONITEMSTATEICON As Long = &H8
    8. Private Const LVHT_ONITEM As Long = (LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_ONITEMSTATEICON)
    9. Private Const LVM_SUBITEMHITTEST As Long = (LVM_FIRST + 57)
    10.  
    11. Private Type POINTAPI
    12.    x As Long
    13.    y As Long
    14. End Type
    15.  
    16. Private Type HITTESTINFO
    17.    pt As POINTAPI
    18.    flags As Long
    19.    iItem As Long
    20.    iSubItem  As Long
    21. End Type
    22.  
    23. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
    24.   (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    25.  
    26. Dim hti As HITTESTINFO
    27.  
    28. Private Sub ListView1_DblClick()
    29. Dim iRes As Long
    30. Dim iItemIndex As Long
    31. Dim iSubitemIndex As Long
    32.  
    33. On Error Resume Next
    34.  
    35.     iRes = SendMessage(ListView1.hwnd, LVM_SUBITEMHITTEST, 0, hti)
    36.    
    37.     iItemIndex = hti.iItem + 1
    38.     iSubitemIndex = hti.iSubItem
    39.    
    40.     ListView1.ListItems(iItemIndex).Selected = True
    41.    
    42.     MsgBox ListView1.ListItems(iItemIndex).SubItems(iSubitemIndex)
    43.  
    44. End Sub
    45.  
    46. Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    47.      With hti
    48.         .pt.x = (x / Screen.TwipsPerPixelX)
    49.         .pt.y = (y / Screen.TwipsPerPixelY)
    50.         .flags = LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_NOWHERE
    51.     End With
    52. End Sub
    edit: code was slightly simplified

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: Listview question

    I think I can use the MouseDown event that KeithUK mentioned to determine which column was last clicked. Then handle the DoubleClick event to place a text box over the appropriate column and list item. I'll post some code when I get it to work.

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2006
    Posts
    39

    Re: Listview question

    This works

    [Highlight=VB]

    Option Explicit
    Private mlngColumnNumberClicked As Long

    Private Sub Form_Load()

    SetupListView
    Text1.Visible = False

    End Sub

    Private Sub SetupListView()
    Dim itmX As ListItem

    With ListView1
    .ColumnHeaders.Clear
    .ColumnHeaders.Add , , "Equipment Type", .Width / 5
    .ColumnHeaders.Add , , "Asset Number", .Width / 5
    .ColumnHeaders.Add , , "Manufacturer", .Width / 5
    .ColumnHeaders.Add , , "Model Number", .Width / 5
    .ColumnHeaders.Add , , "Next Cal Date", .Width / 5

    .ListItems.Clear

    .BorderStyle = ccFixedSingle
    .BackColor = vbWhite
    .Checkboxes = False
    .Enabled = True
    .FlatScrollBar = False
    .FullRowSelect = True
    .GridLines = True
    .HideSelection = False
    .LabelEdit = lvwManual
    .LabelWrap = False
    .MultiSelect = False
    .Sorted = False
    .View = lvwReport

    Set itmX = .ListItems.Add(, , "Voltmeter")
    itmX.SubItems(1) = "121212"
    itmX.SubItems(2) = "HP"
    itmX.SubItems(3) = "34401A"
    itmX.SubItems(4) = "1/1/2007"
    Set itmX = Nothing
    End With

    End Sub

    Private Sub ListView1_DblClick()

    If mlngColumnNumberClicked > 1 Then
    With ListView1
    Text1.Left = .Left + .ColumnHeaders(mlngColumnNumberClicked).Left
    Text1.Width = .ColumnHeaders(mlngColumnNumberClicked).Width
    Text1.Top = .Top + .ListItems(.SelectedItem.Index).Top
    Text1.Height = .ListItems(.SelectedItem.Index).Height
    Text1.Visible = True
    Text1.Text = .ListItems(.SelectedItem.Index).SubItems(mlngColumnNumberClicked - 1)
    Text1.SelStart = 0
    Text1.SelLength = Len(Text1.Text)
    Text1.SetFocus
    End With
    End If

    End Sub

    Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
    Dim lngColumnHeaderIndex As Long

    Text1.Visible = False

    With ListView1
    For lngColumnHeaderIndex = 1 To .ColumnHeaders.Count
    If x >= .ColumnHeaders(lngColumnHeaderIndex).Left And x <= .ColumnHeaders(lngColumnHeaderIndex).Left + .ColumnHeaders(lngColumnHeaderIndex).Width Then
    mlngColumnNumberClicked = lngColumnHeaderIndex
    Exit For
    End If
    Next lngColumnHeaderIndex
    End With

    End Sub
    [Highlight=VB]

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