Jump to content

Right-click on a ListBox - I need the value below cursor selected


 Share

Recommended Posts

Hi All,

I have a listbox that has a context menu associated with it.  When I right-click on the listbox the context menu pops up as expected.  But I would like for the list item under the cursor to be selected, so that my context menu can take action on that item (such as "remove item").  I have culled my script down to the pertinent parts I think.

Can someone give me a pointer in the right direction on how to select / activate a list item under the cursor?  Please overlook my #region definitions.  It drives most people mad but it does help me.

#region Includes and Constants
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#endregion Includes and Constants

#region GUI Scaling
Local $hWidth = 615
Local $hHeight = 438
Local $hTop = (@DesktopHeight - 40 - $hHeight) / 2
Local $hLeft = (@DesktopWidth - $hWidth) / 2
#endregion

#region GUI section
Local $iTitle = "User Configuration Applet"
Local $hGUI = GUICreate($iTitle, $hWidth, $hHeight, $hLeft, $hTop,BitOR($GUI_SS_Default_GUI, $WS_SIZEBOX, $WS_MAXIMIZEBOX))
Local $tQuestion = GUICtrlCreateLabel("When I right-click in the listbox I would like the user below it to be selected but I'm not sure how.",5,5)

Local $hUsers = GUICtrlCreateList("", 16, 245, 227, 175)
_GUICtrlListBox_AddString($hUsers,"Joe Mama")
_GUICtrlListBox_AddString($hUsers,"John Public")
_GUICtrlListBox_AddString($hUsers,"Jane Doe")
_GUICtrlListBox_EndUpdate($hUsers)

Local $ContextMenuUsers = GUICtrlCreateContextMenu($hUsers)
Local $ContextRemove = GUICtrlCreateMenuItem("Remove User",$ContextMenuUsers)

GUISetState(@SW_SHOW)
#endregion

#region Monitor the GUI
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $ContextRemove
            MsgBox(0,"User to remove","Need to have the user selected...")
    EndSwitch
WEnd
#endregion
Link to comment
Share on other sites

This dirty hack works

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_SECONDARYDOWN
            $a = GUIGetCursorInfo()
            If $a[4] = $hUsers Then
                $i = _GUICtrlListBox_ItemFromPoint($hUsers, $a[0] - 16, $a[1] - 245)
                If $i <> -1 Then _GUICtrlListBox_SetCurSel($hUsers, $i)
            EndIf
        Case $ContextRemove
            MsgBox(0,"User to remove","Need to have the user selected...")
    EndSwitch
WEnd
Note that in ItemFromPoint API you have to use X,Y coordinates relative to ListBox, so you have to subtract Left/Top position of ListBox Edited by Zedna
Link to comment
Share on other sites

This dirty hack works

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $GUI_EVENT_SECONDARYDOWN
            $a = GUIGetCursorInfo()
            If $a[4] = $hUsers Then
                $i = _GUICtrlListBox_ItemFromPoint($hUsers, $a[0] - 16, $a[1] - 245)
                If $i <> -1 Then _GUICtrlListBox_SetCurSel($hUsers, $i)
            EndIf
        Case $ContextRemove
            MsgBox(0,"User to remove","Need to have the user selected...")
    EndSwitch
WEnd
Note that in ItemFromPoint API you have to use X,Y coordinates relative to ListBox, so you have to subtract Left/Top position of ListBox

 

 

That's brilliant, thanks!  I'll try that today and see where it lands me.  I appreciate your reply.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...