The subitem of VB Editor ListView

Source: Internet
Author: User
Tags exit integer client
File One, form1.frm

Add a ListView, two ImageList, a text box

The code is as follows:
Option Explicit
'
' Copyright 1997-1999 Brad Martinez, http://www.mvps.org
'
' demonstrates to ' in ' where do subitem editing in the VB ListView.

Private m_hwndlv as Long ' Listview1.hwnd
Private M_hwndtb as Long ' Textbox1.hwnd
Private M_iitem as Long ' listitem.index whose subitem is being edited
Private M_isubitem as Long ' zero based index of Listview1.listitems (M_iitem). Subitem being edited
'

Private Sub Form_Load ()
Dim I as Long
Dim Item as ListItem

' text1.appearance = Ccflat ' comctllib enum value
Text1.visible = False
M_HWNDTB = Text1.hwnd

' Initialize the Imagelists
With ImageList1
. ImageHeight = 32
. ImageWidth = 32
. Listimages.add Picture:=icon
End With

With ImageList2
. ImageHeight = 16
. ImageWidth = 16
. Listimages.add Picture:=icon
End With

' Initialize the ListView
With ListView1
' . LabelEdit = lvwmanual
. HideSelection = False
. Icons = ImageList1
. Smallicons = ImageList2
M_HWNDLV =. HWnd

For i = 1 to 4
. Columnheaders.add text:= "column" & I
Next

For i = 0 to &h3f
Set item =. Listitems.add (,, "Item" & I, 1, 1)
Item. SubItems (1) = i * 10
Item. SubItems (2) = i * 100
Item. SubItems (3) = i * 1000
Next
End With


End Sub

Private Sub form_resize ()
' Listview1.move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub Listview1_dblclick ()
Dim Lvhti as Lvhittestinfo
Dim RC as RECT
Dim Li as ListItem

' If a left button double-click ... (change to suit)
If (Getkeystate (Vbkeylbutton) and &h8000) Then

' If a ListView subitem is double clicked ...
Call GetCursorPos (lvhti.pt)
Call ScreenToClient (M_HWNDLV, lvhti.pt)
If (Listview_subitemhittest (M_HWNDLV, Lvhti) <> Lvi_noitem) Then
If Lvhti.isubitem Then

' Get the Subitem ' s label (and icon) rect.
If Listview_getsubitemrect (M_HWNDLV, Lvhti.iitem, Lvhti.isubitem, Lvir_label, RC) Then

' Either set the ListView as the TextBox parent window in order to
' Have the TextBox Move method to use ListView client coords, or just
' Map the ListView client coords to the TextBox ' paent Form
' Call SetParent (M_HWNDTB, M_HWNDLV)
Call MapWindowPoints (M_HWNDLV, HWnd, RC, 2)
Text1.move (RC. Left + 4) * Screen.twipsperpixelx, _
Rc. Top * screen.twipsperpixely, _
(RC. RIGHT-RC. left) * Screen.twipsperpixelx, _
(RC. BOTTOM-RC. Top) * screen.twipsperpixely

' Save the one-based index ' the ListItem and the zero-based index
' Of the subitem (if the ListView is sorted via the API, then Listitem.index
' would be different than Lvhti.iitem +1 ...)
M_iitem = Lvhti.iitem + 1
M_isubitem = Lvhti.isubitem

' Put the subitem ' s-text in the TextBox, save the subitem ' s text,
' and clear the subitem ' s text.
Text1 = Listview1.listitems (M_iitem). SubItems (M_isubitem)
Text1.tag = Text1
Listview1.listitems (M_iitem). SubItems (M_isubitem) = ""

' Make the TextBox ' topmost Form control, make the IT visible, select
' Its text, give it's focus, and subclass it.
Text1.zorder 0
Text1.visible = True
Text1.selstart = 0
Text1.sellength = Len (TEXT1)
Text1.setfocus
Call Subclass (M_HWNDTB, AddressOf WndProc)

End If ' Listview_getsubitemrect
End If ' Lvhti.isubitem
End If ' Listview_subitemhittest
End If ' Getkeystate (Vbkeylbutton)

End Sub

' Selects the ListItem whose subitem is being edited ...

Private Sub Text1_gotfocus ()
Listview1.listitems (M_iitem). Selected = True
End Sub

' If ' The TextBox is shown, size it's width so it ' s always a little
' longer than the length of its Text.

Private Sub Text1_change ()
If m_iitem Then text1.width = TextWidth (TEXT1) + 180
End Sub

' Update the subitem text on the ' Enter key and Cancel on the ' Escape key.

Private Sub text1_keypress (KeyAscii as Integer)

If (KeyAscii = Vbkeyreturn) Then
Call Hidetextbox (True)
KeyAscii = 0
ElseIf (KeyAscii = vbkeyescape) Then
Call Hidetextbox (False)
KeyAscii = 0
End If

End Sub

Friend Sub Hidetextbox (fapplychanges as Boolean)

If fapplychanges Then
Listview1.listitems (M_iitem). SubItems (M_isubitem) = Text1
Else
Listview1.listitems (M_iitem). SubItems (M_isubitem) = Text1.tag
End If

Call Unsubclass (M_HWNDTB)
Text1.visible = False
Text1 = ""
' Call SetParent (M_HWNDTB, HWnd)
' Listview1.setfocus
M_iitem = 0

End Sub


Document II: Module1.bas

Option Explicit
'
' Copyright 1997-1999 Brad Martinez, http://www.mvps.org
'
Public Type Pointapi ' PT
X as Long
Y as Long
End Type

Public Type RECT ' RCT
Left as Long
Top as Long
Right as Long
Bottom as Long
End Type

Declare Function getcursorpos Lib "user32" (Lppoint as Pointapi) as Long
Declare Function screentoclient Lib "user32" (ByVal hWnd as Long, lppoint as POINTAPI) as Long
Declare Function getkeystate Lib "user32" (ByVal Nvirtkey as keycodeconstants) as Integer

Declare Function setparent Lib "user32" (ByVal hwndchild as Long, ByVal hwndnewparent as long) as long
Declare Function mapwindowpoints Lib "user32" (ByVal hwndfrom as Long, ByVal hwndto as Long, lppt as any, ByVal cpoints as Long) as Long

Declare Function sendmessage Lib "user32" Alias "SendMessageA" _
(ByVal hWnd as Long, _
ByVal wmsg as Long, _
ByVal WParam as Long, _
LParam as any) as Long ' <---

' ========================================================================
' ListView defs

#Const Win32_ie = &h300

' User-defined
Public Const Lvi_noitem =-1

' Messages
Public Const Lvm_first = &h1000
#If (Win32_ie >= &h300) Then
Public Const Lvm_getsubitemrect = (Lvm_first + 56)
Public Const Lvm_subitemhittest = (Lvm_first + 57)
#End If

' Lvm_getsubitemrect RCT. Left
Public Const Lvir_icon = 1
Public Const Lvir_label = 2

Public Type Lvhittestinfo ' is Lv_hittestinfo
PT as Pointapi
Flags as Long
IItem as Long
#If (Win32_ie >= &h300) Then
Isubitem as Long ' is ' isn't in Win95. Valid only for Lvm_subitemhittest
#End If
End Type

' Lvhittestinfo flags
Public Const Lvht_onitemlabel = &h4
'

#If (Win32_ie >= &h300) Then

Public Function Listview_getsubitemrect (hWnd as Long, iitem as long, isubitem as Long, _
Code as Long, PRC as RECT) as Boolean
Prc. top = Isubitem
Prc. Left = code
Listview_getsubitemrect = SendMessage (HWnd, Lvm_getsubitemrect, ByVal IItem, PRC)
End Function

Public Function listview_subitemhittest (hWnd as Long, Plvhti as Lvhittestinfo) as Long
Listview_subitemhittest = SendMessage (hWnd, lvm_subitemhittest, 0, Plvhti)
End Function

#End If ' Win32_ie >= &h300


Document III: Msubclass.bas

Option Explicit
'
' Copyright 1997-1999 Brad Martinez, http://www.mvps.org
'
Private Const Wm_destroy = &h2
Private Const wm_killfocus = &h8

Private Declare Function getprop Lib "user32" Alias "Getpropa" (ByVal hWnd as Long, ByVal lpstring as String) as Long
Private Declare Function setprop Lib "user32" Alias "Setpropa" (ByVal hWnd as Long, ByVal lpstring as String, ByVal hdata As long) as long
Private Declare Function removeprop Lib "user32" Alias "Removepropa" (ByVal hWnd as Long, ByVal lpstring as String) as Lon G

Declare Function setwindowlong Lib "user32" Alias "Setwindowlonga" (ByVal hWnd as Long, ByVal nindex as Long, ByVal DWNEWL Ong as long) as long
Private Const Gwl_wndproc = (-4)

Private Declare Function callwindowproc Lib "user32" Alias "Callwindowproca" (ByVal Lpprevwndfunc as Long, ByVal hWnd as L Ong, ByVal umsg as Long, ByVal WParam as Long, ByVal LParam as long) as long

Private Const oldwndproc = "Oldwndproc"
'

Public Function Subclass (HWnd as Long, lpfnnew as long) as Boolean
Dim Lpfnold as Long
Dim fsuccess as Boolean

If (Getprop (hWnd, Oldwndproc) = 0) Then
Lpfnold = SetWindowLong (HWnd, GWL_WNDPROC, lpfnnew)
If Lpfnold Then
fsuccess = SetProp (HWnd, Oldwndproc, Lpfnold)
End If
End If

If fsuccess Then
Subclass = True
Else
If lpfnold Then Call Unsubclass (hWnd)
MsgBox "Unable to successfully subclass &h" & Hex (hWnd), vbcritical
End If

End Function

Public Function Unsubclass (hWnd as Long) as Boolean
Dim Lpfnold as Long

Lpfnold = Getprop (hWnd, Oldwndproc)
If Lpfnold Then
If Removeprop (hWnd, Oldwndproc) Then
Unsubclass = SetWindowLong (HWnd, GWL_WNDPROC, Lpfnold)
End If
End If

End Function

Public Function WndProc (ByVal hWnd as Long, ByVal umsg as Long, ByVal WParam as Long, ByVal LParam as long) as long

Select Case umsg

' ======================================================
' Hide the TextBox when it loses focus (its LostFocus event it is not fired
' When losing focus to a window outside the app.

Case Wm_killfocus
' Oldwndproc'll be gone after the unsubclass is called, Hidetextbox
' Calls Unsubclass.
Call CallWindowProc (Getprop (hwnd, OLDWNDPROC), hwnd, umsg, WParam, LParam)
Call Form1.hidetextbox (True)
Exit Function

' ======================================================
' Unsubclass the window when it's destroyed in case someone forgot ...

Case Wm_destroy
' Oldwndproc'll be gone after the Unsubclass is called!
Call CallWindowProc (Getprop (hwnd, OLDWNDPROC), hwnd, umsg, WParam, LParam)
Call Unsubclass (HWND)
Exit Function

End Select

WndProc = CallWindowProc (Getprop (hwnd, Oldwndproc), HWnd, umsg, WParam, LParam)

End Function




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.