|
-
Aug 1st, 2006, 02:43 PM
#1
Thread Starter
Member
[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
-
Aug 1st, 2006, 03:04 PM
#2
Re: Listview question
I use a seperate popupmenu when you right-click on a ListView item.
VB Code:
Private Sub lvwPrograms_MouseDown(Button As Integer, Shift As Integer, X As Single, y As Single)
If Button = 2 Then
PopupMenu mnuEditTop
End If
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.
-
Aug 1st, 2006, 03:20 PM
#3
Re: Listview question
You will need to subclass LVM_SUBITEMHITTEST (and some other) message:
VB Code:
Option Explicit
Private Const LVM_FIRST = &H1000
Private Const LVHT_NOWHERE As Long = &H1
Private Const LVHT_ONITEMICON As Long = &H2
Private Const LVHT_ONITEMLABEL As Long = &H4
Private Const LVHT_ONITEMSTATEICON As Long = &H8
Private Const LVHT_ONITEM As Long = (LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_ONITEMSTATEICON)
Private Const LVM_SUBITEMHITTEST As Long = (LVM_FIRST + 57)
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type HITTESTINFO
pt As POINTAPI
flags As Long
iItem As Long
iSubItem As Long
End Type
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" _
(ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Dim hti As HITTESTINFO
Private Sub ListView1_DblClick()
Dim iRes As Long
Dim iItemIndex As Long
Dim iSubitemIndex As Long
On Error Resume Next
iRes = SendMessage(ListView1.hwnd, LVM_SUBITEMHITTEST, 0, hti)
iItemIndex = hti.iItem + 1
iSubitemIndex = hti.iSubItem
ListView1.ListItems(iItemIndex).Selected = True
MsgBox ListView1.ListItems(iItemIndex).SubItems(iSubitemIndex)
End Sub
Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
With hti
.pt.x = (x / Screen.TwipsPerPixelX)
.pt.y = (y / Screen.TwipsPerPixelY)
.flags = LVHT_ONITEMICON Or LVHT_ONITEMLABEL Or LVHT_NOWHERE
End With
End Sub
edit: code was slightly simplified
Last edited by RhinoBull; Aug 1st, 2006 at 03:45 PM.
-
Aug 1st, 2006, 03:35 PM
#4
Thread Starter
Member
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.
-
Aug 1st, 2006, 03:36 PM
#5
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|