Thursday, June 14, 2012

Class ListBox - Untuk Memilih Item Pada Saat Klik Kanan

Mengenai cara memilih item pada ListBox dengan klik kanan menggunakan VB6 - Seperti yang telah kita ketahui, sebuah ListBox hanya dapat dipilih itemnya melalui klik kiri, nah bagaimana jika kita ingin memilih item ListBox tersebut melalui klik kanan? Di bawah ini adalah class VB6 yang saya buat untuk klik kanan pada ListBox, penggunaan class di sini bertujuan hanya untuk menyederhanakan kerumitan/kekomplekan dalam pemrograman saja, jadi Anda dapat membuat kodenya pada module atau langsung pada formnya dengan sedikit modifikasi tentunya.
Option Explicit

Private WithEvents lst As ListBox

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Const LB_ITEMFROMPOINT As Long = &H1A9

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Long) As Long
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
Private Declare Function ScreenToClient Lib "user32" (ByVal hWnd As Long, lpPoint As POINTAPI) As Long

Public Function LoWord(dwValue As Long) As Integer
CopyMemory LoWord, dwValue, 2
End Function

Public Function MAKELONG(wLow As Long, wHigh As Long) As Long
MAKELONG = LoWord(wLow) Or (&H10000 * LoWord(wHigh))
End Function

Public Function MAKELPARAM(wLow As Long, wHigh As Long) As Long
MAKELPARAM = MAKELONG(wLow, wHigh)
End Function

Public Property Let ListBox(New_List As ListBox)
Set lst = New_List
End Property

Private Sub lst_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = vbRightButton Then
Dim lParam As Long
Dim curritem As Long
Dim r As Long
Dim pt As POINTAPI
Call GetCursorPos(pt)
Call ScreenToClient(lst.hWnd, pt)
lParam = MAKELPARAM(pt.X, pt.Y)
r = SendMessage(lst.hWnd, LB_ITEMFROMPOINT, 0&, ByVal lParam)
If r > -1 Then
curritem = LoWord(r)
lst.Selected(curritem) = True
End If
End If
End Sub
Demikian Class ListBox untuk memilih item melalui klik kanan pada VB6, semoga bermanfaat.