get the number of row clicked

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

get the number of row clicked

Post by sal21 »

Code: Select all

Private Sub ListView1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)

    If Button = vbLeftButton Then
        With Me.ListView1
            .DragIcon = LoadPicture("C:\Lavori_Vb6\HOTEL\IMG\" & "DRAGMOVE.CUR")
            .Drag vbBeginDrag
        End With
    End If

End Sub
how to get the number of row index clicked on the listview?

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: get the number of row clicked

Post by HansV »

See VBA Drag Drop From TreeView to ListView & ListView to TreeView (ActiveX Controls) again. You have to add the following code to a standard module:

Code: Select all

Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Const LOGPIXELSX = 88
Const LOGPIXELSY = 90

Public Function TwipsPerPixelX() As Integer
    Dim MyDesktopWindowHandle As Long, MyDesktopWindowDeviceContext As Long
    Dim MyWidthOfScreen As Long, MyUsedToReleaseDeviceContext As Long
   'Get the handle of the desktop window
    MyDesktopWindowHandle = GetDesktopWindow()
    'Get the desktop window's device context
    MyDesktopWindowDeviceContext = GetDC(MyDesktopWindowHandle)
    'Get the width of the screen
    MyWidthOfScreen = GetDeviceCaps(MyDesktopWindowDeviceContext, LOGPIXELSX)
    'Release the device context
    MyUsedToReleaseDeviceContext = ReleaseDC(MyDesktopWindowHandle, MyDesktopWindowDeviceContext)

    TwipsPerPixelX = 1440 / MyWidthOfScreen '1 inch is always 1440 twips
End Function

Public Function TwipsPerPixelY() As Integer
    Dim MyDesktopWindowHandle As Long, MyDesktopWindowDeviceContext As Long
    Dim MyHeightOfScreen As Long, MyUsedToReleaseDeviceContext As Long

    'Get the handle of the desktop window
    MyDesktopWindowHandle = GetDesktopWindow()
    'Get the desktop window's device context
    MyDesktopWindowDeviceContext = GetDC(MyDesktopWindowHandle)
    'Get the width of the screen
    MyHeightOfScreen = GetDeviceCaps(MyDesktopWindowDeviceContext, LOGPIXELSY)
    'Release the device context
    MyUsedToReleaseDeviceContext = ReleaseDC(MyDesktopWindowHandle, MyDesktopWindowDeviceContext)

    TwipsPerPixelY = 1440 / MyHeightOfScreen '1 inch is always 1440 twips
End Function
Then use

Code: Select all

Private Sub ListView1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    Dim itm As ListItem
    If Button = vbLeftButton Then
        With Me.ListView1
            ' Get the item the user clicked in
            Set itm = .HitTest(x * TwipsPerPixelX, y * TwipsPerPixelY)
            ' Its row number is itm.Index
            .DragIcon = LoadPicture("C:\Lavori_Vb6\HOTEL\IMG\" & "DRAGMOVE.CUR")
            .Drag vbBeginDrag
        End With
    End If
End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: get the number of row clicked

Post by sal21 »

HansV wrote:
25 Jul 2021, 13:07
See VBA Drag Drop From TreeView to ListView & ListView to TreeView (ActiveX Controls) again. You have to add the following code to a standard module:

Code: Select all

Public Declare Function GetDesktopWindow Lib "user32" () As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Public Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long

Const LOGPIXELSX = 88
Const LOGPIXELSY = 90

Public Function TwipsPerPixelX() As Integer
    Dim MyDesktopWindowHandle As Long, MyDesktopWindowDeviceContext As Long
    Dim MyWidthOfScreen As Long, MyUsedToReleaseDeviceContext As Long
   'Get the handle of the desktop window
    MyDesktopWindowHandle = GetDesktopWindow()
    'Get the desktop window's device context
    MyDesktopWindowDeviceContext = GetDC(MyDesktopWindowHandle)
    'Get the width of the screen
    MyWidthOfScreen = GetDeviceCaps(MyDesktopWindowDeviceContext, LOGPIXELSX)
    'Release the device context
    MyUsedToReleaseDeviceContext = ReleaseDC(MyDesktopWindowHandle, MyDesktopWindowDeviceContext)

    TwipsPerPixelX = 1440 / MyWidthOfScreen '1 inch is always 1440 twips
End Function

Public Function TwipsPerPixelY() As Integer
    Dim MyDesktopWindowHandle As Long, MyDesktopWindowDeviceContext As Long
    Dim MyHeightOfScreen As Long, MyUsedToReleaseDeviceContext As Long

    'Get the handle of the desktop window
    MyDesktopWindowHandle = GetDesktopWindow()
    'Get the desktop window's device context
    MyDesktopWindowDeviceContext = GetDC(MyDesktopWindowHandle)
    'Get the width of the screen
    MyHeightOfScreen = GetDeviceCaps(MyDesktopWindowDeviceContext, LOGPIXELSY)
    'Release the device context
    MyUsedToReleaseDeviceContext = ReleaseDC(MyDesktopWindowHandle, MyDesktopWindowDeviceContext)

    TwipsPerPixelY = 1440 / MyHeightOfScreen '1 inch is always 1440 twips
End Function
Then use

Code: Select all

Private Sub ListView1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal x As stdole.OLE_XPOS_PIXELS, ByVal y As stdole.OLE_YPOS_PIXELS)
    Dim itm As ListItem
    If Button = vbLeftButton Then
        With Me.ListView1
            ' Get the item the user clicked in
            Set itm = .HitTest(x * TwipsPerPixelX, y * TwipsPerPixelY)
            ' Its row number is itm.Index
            .DragIcon = LoadPicture("C:\Lavori_Vb6\HOTEL\IMG\" & "DRAGMOVE.CUR")
            .Drag vbBeginDrag
        End With
    End If
End Sub
BUT I'M on vb6

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: get the number of row clicked

Post by HansV »

Have you tried it?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: get the number of row clicked

Post by sal21 »

HansV wrote:
25 Jul 2021, 14:28
Have you tried it?
YES...
SEE image

but with, work!

Code: Select all

 With Me.ListView1
            Set ITMX = .HitTest(x, y)
            If Not ITMX Is Nothing Then
                RM = ITMX.Index
                .DragIcon = LoadPicture("C:\Lavori_Vb6\HOTEL\IMG\" & "DRAGMOVE.CUR")
                .Drag vbBeginDrag
            End If
        End With
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78229
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: get the number of row clicked

Post by HansV »

With what? Try replacing vbLeftButton with 1.
Best wishes,
Hans

User avatar
SpeakEasy
4StarLounger
Posts: 535
Joined: 27 Jun 2021, 10:46

Re: get the number of row clicked

Post by SpeakEasy »

Bit late I know, but why are you trying to use an MS Forms declaration for the ListView MouseDown event rather than the VB6 declaration? The former will NOT work in VB6 - and is the cause of the compile-time error you are seeing.