| Display a PopUp Menu when a ListBox Item is Right-Clicked This is an example of how to display a pop-up menu when a list box item is right clicked. Refer to the comments for instructions regarding how to implement it in your own projects. Option Explicit 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 Private Const LB_GETITEMRECT = &H198 Private Const LB_ERR = (-1) Private Type RECT Left As Long Top As Long Right As Long Bottom As Long End Type Public Function GetRClickedItem(MyList As Control, _ X As Single, Y As Single) As Long 'PURPOSE: Determine which item was right clicked in a list 'box, from the list_box's mouse down event. YOU MUST CALL THIS 'FROM THE MOUSEDOWN EVENT, PASSING THE X AND Y VALUES FROM THAT 'EVENT TO THIS FUNCTION 'MYLIST: ListBox Control 'X, Y: X and Y position from MyList_MouseDown 'RETURNS: ListIndex of selected item, or -1 if 'a) There is no selected item, or b) an error occurs. Dim clickX As Long, clickY As Long Dim lRet As Long, CurRect As RECT Dim l As Long 'Control must be a listbox If Not TypeOf MyList Is ListBox Then GetRClickedItem = LB_ERR Exit Function End If 'get x and y in pixels l = 0 clickX = X \ Screen.TwipsPerPixelX clickY = Y \ Screen.TwipsPerPixelY Do While True 'get current selection as rectangle lRet = SendMessage(MyList.hwnd, LB_GETITEMRECT, l, CurRect) 'If error, or no selection, bye-bye If lRet < 1 Then GetRClickedItem = LB_ERR Exit Function End If If (clickX >= CurRect.Left) And (clickX <= CurRect.Right) _ And (clickY >= CurRect.Top) And (clickY <= CurRect.Bottom) Then GetRClickedItem = l Exit Function End If l = l + 1 Loop End Function 'BELOW IS AN EXAMPLE OF HOW TO POPUP A MENU USING THE ABOVE 'FUNCTION Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single) Dim lItem As Long If Button = vbRightButton Then lItem = GetRClickedItem(List1, X, Y) 'USE YOUR OWN LISTBOX 'AS THE FIRST PARAMETER If lItem <> -1 Then List1.ListIndex = lItem PopupMenu mnuPopup 'USE YOUR OWN POPUP MENU HERE End If End If End Sub |
Display a PopUp Menu when a ListBox Item is Right- |
Express News India | Freelance ecommerce web development India