Jump to content

[HELP] - ContextMenu in Listview


Recommended Posts

Hi guys !.
 

I'm created context menu items:

Delete
Edit
Add
------------
ON
OFF

I want to do the following:

 

Untitled.thumb.png.f40c6411266f2ee7f47779ab1cdc0724.png


+ If right-click on a listview's item only --> detect a text in COL3 is 'ON' or 'OFF'  --> if it is ON then set the context item ON to disabled, if it is OFF then set the context item OFF to disabled. (Pic1 and Pic 2).
+ If selected multi-row (pic 3) --> 'ON' and 'Edit' set to disabled.
+ If selected multi-row (pic 4) --> 'Edit' set to disabled.

+If right-click without listview's item --> 'Add' context item only set to enabled (pic 5)

Sorry sorry. My English is bad.

Edited by MarkLee
Link to comment
Share on other sites

@MarkLee

Don't worry, you make yourself understandable :)

You should use a WM_NOTIFY handler, in order to get the selected item(s), from which you can then enable or disable your context menu items.

In order to have more help, please post your code :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

@FrancescoDiMuro

Thank you for replying.

This is my demo code:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
 
$iLVStyle = BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS)
$iLVExtStyle = BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)
 
Example()
 
Func Example()
    GUICreate("listview items", 220, 250, -1, -1, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF) ; will change background color
 
    Local $idListview = GUICtrlCreateListView("col1      |col2|col3  ", 10, 10, 200, 150, $iLVStyle, $iLVExtStyle) ;,$LVS_SORTDESCENDING)
    Local $idItem1 = GUICtrlCreateListViewItem("item1|col22|ON", $idListview)
    Local $idItem2 = GUICtrlCreateListViewItem("item2|col12|OFF", $idListview)
    Local $idItem3 = GUICtrlCreateListViewItem("item3|col32|ON", $idListview)
    $Menu = GUICtrlCreateContextMenu($idListview)
    $mDel = GUICtrlCreateMenuItem("Delete", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mEdit = GUICtrlCreateMenuItem("Edit", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mAdd = GUICtrlCreateMenuItem("Add", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlCreateMenuItem("", $Menu)
    $mON = GUICtrlCreateMenuItem("ON", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mOFF = GUICtrlCreateMenuItem("OFF", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState(@SW_SHOW)
 
    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
 
;~             Case $idButton
;~                 MsgBox($MB_SYSTEMMODAL, "listview item", GUICtrlRead(GUICtrlRead($idListview)), 2)
 
            Case $idListview
;~                 MsgBox($MB_SYSTEMMODAL, "listview", "clicked=" & GUICtrlGetState($idListview), 2)
 
        EndSwitch
    WEnd
EndFunc   ;==>Example

 

Edited by MarkLee
Link to comment
Share on other sites

@MarkLee  You can use Francesco WM_NOTIFY handler with the notification of  $NM_RCLICK. You can read about it here.

Inside the handler, use _GUICtrlListView_GetSelectedIndices to manage the current selection.  Rest is quite straightforward. 

Link to comment
Share on other sites

33 minutes ago, Nine said:

@MarkLee  You can use Francesco WM_NOTIFY handler with the notification of  $NM_RCLICK. You can read about it here.

Inside the handler, use _GUICtrlListView_GetSelectedIndices to manage the current selection.  Rest is quite straightforward. 

@NIce: I'm using WM_NOTIFY handler and _GUICtrlListView_GetSelectedIndices func but not working like pic4.

This my code: 

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>
 
Global $idListview, $idItem1, $mON, $mOFF, $mEdit, $mAdd, $mDel
$iLVStyle = BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS)
$iLVExtStyle = BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)
 
Example()
 
Func Example()
    GUICreate("listview items", 220, 250, -1, -1, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF) ; will change background color
 
    $idListview = GUICtrlCreateListView("col1      |col2|col3  ", 10, 10, 200, 150, $iLVStyle, $iLVExtStyle) ;,$LVS_SORTDESCENDING)
    $idItem1 = GUICtrlCreateListViewItem("item1|col22|ON", $idListview)
    $idItem2 = GUICtrlCreateListViewItem("item2|col12|OFF", $idListview)
    $idItem3 = GUICtrlCreateListViewItem("item3|col32|ON", $idListview)
    $idItem4 = GUICtrlCreateListViewItem("item4|col14|OFF", $idListview)
    $Menu = GUICtrlCreateContextMenu($idListview)
    $mDel = GUICtrlCreateMenuItem("Delete", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mEdit = GUICtrlCreateMenuItem("Edit", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mAdd = GUICtrlCreateMenuItem("Add", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlCreateMenuItem("", $Menu)
    $mON = GUICtrlCreateMenuItem("ON", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mOFF = GUICtrlCreateMenuItem("OFF", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState(@SW_SHOW)
 
    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")
 
    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
 
;~
 
        EndSwitch
    WEnd
EndFunc   ;==>Example
 
 
Func ListView_RClick()
    Local $sSelLine = _GUICtrlListView_GetSelectedIndices($idListview)
    If ($sSelLine) <> ""   Then
        GUICtrlSetState($mON, _GUICtrlListView_GetItemText($idListview, Number($sSelLine), 2) == "ON" ? $GUI_DISABLE : $GUI_ENABLE)
        GUICtrlSetState($mOFF, _GUICtrlListView_GetItemText($idListview, Number($sSelLine), 2) == "OFF" ? $GUI_DISABLE : $GUI_ENABLE)
        $cou = _GUICtrlListView_GetSelectedCount($idListview)
        GUICtrlSetState($mEdit, $cou >= 2 ? $GUI_DISABLE : $GUI_ENABLE)
 
    Else
        GUICtrlSetState($mDel,$sSelLine = "" ? $GUI_DISABLE: $GUI_ENABLE)
        GUICtrlSetState($mEdit,$sSelLine = "" ?  $GUI_DISABLE : $GUI_ENABLE)
        GUICtrlSetState($mON, $sSelLine = "" ? $GUI_DISABLE: $GUI_ENABLE)
        GUICtrlSetState($mOFF,$sSelLine = "" ? $GUI_DISABLE: $GUI_ENABLE)
    EndIf
;~  Return $aHit[0]
EndFunc
 
 
Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $idListview
    If Not IsHWnd($idListview) Then $hWndListView = GUICtrlGetHandle($idListview)
 
    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    ;$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
;~                  msgbox(0, "", "22")
                    ListView_RClick()
;~                     Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Link to comment
Share on other sites

12 minutes ago, MarkLee said:

Local $sSelLine = _GUICtrlListView_GetSelectedIndices($idListview)     If ($sSelLine) <> ""   Then         GUICtrlSetState($mON, _GUICtrlListView_GetItemText($idListview, Number($sSelLine), 2) == "ON" ? $GUI_DISABLE : $GUI_ENABLE)

$sSelLine is a string delimited by a pipe.

You are using it definitely wrong.

The == operator should be used only for strings (case-sensitive).

Correct your code debugging your texts :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

I'm trying to  fix my code but still not working.

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>
#include <WindowsConstants.au3>
#include <ListViewConstants.au3>
#include <GuiListView.au3>

Global $idListview, $idItem1, $mON, $mOFF, $mEdit, $mAdd, $mDel
$iLVStyle = BitOR($LVS_REPORT, $LVS_SHOWSELALWAYS)
$iLVExtStyle = BitOR($WS_EX_CLIENTEDGE, $LVS_EX_GRIDLINES, $LVS_EX_FULLROWSELECT)

Example()

Func Example()
    GUICreate("listview items", 220, 250, -1, -1, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF) ; will change background color

    $idListview = GUICtrlCreateListView("col1      |col2|col3  ", 10, 10, 200, 150, $iLVStyle, $iLVExtStyle) ;,$LVS_SORTDESCENDING)
    $idItem1 = GUICtrlCreateListViewItem("item1|col22|ON", $idListview)
    Local $idItem2 = GUICtrlCreateListViewItem("item2|col12|OFF", $idListview)
    Local $idItem3 = GUICtrlCreateListViewItem("item3|col32|ON", $idListview)
    Local $idItem4 = GUICtrlCreateListViewItem("item4|col14|OFF", $idListview)
    $Menu = GUICtrlCreateContextMenu($idListview)
    $mDel = GUICtrlCreateMenuItem("Delete", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mEdit = GUICtrlCreateMenuItem("Edit", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mAdd = GUICtrlCreateMenuItem("Add", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    GUICtrlCreateMenuItem("", $Menu)
    $mON = GUICtrlCreateMenuItem("ON", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    $mOFF = GUICtrlCreateMenuItem("OFF", $Menu)
;~  GUICtrlSetState(-1, $GUI_DISABLE)
    GUISetState(@SW_SHOW)

    GUIRegisterMsg($WM_NOTIFY, "WM_NOTIFY")

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop

;~

        EndSwitch
    WEnd
EndFunc   ;==>Example


Func ListView_RClick()
    Local $sSelLine = _GUICtrlListView_GetSelectedIndices($idListview, True)
    If $sSelLine[0] = 1  Then
        GUICtrlSetState($mON, _GUICtrlListView_GetItemText($idListview, $sSelLine[1], 2) = "ON" ? $GUI_DISABLE : $GUI_ENABLE)
        GUICtrlSetState($mOFF, _GUICtrlListView_GetItemText($idListview, $sSelLine[1], 2) = "OFF" ? $GUI_DISABLE : $GUI_ENABLE)
    ElseIf $sSelLine[0] > 1 Then
        GUICtrlSetState($mEdit, $sSelLine[0] > 1 ? $GUI_DISABLE : $GUI_ENABLE)

    Else
        GUICtrlSetState($mDel,$sSelLine[0] = 0  ? $GUI_DISABLE: $GUI_ENABLE)
        GUICtrlSetState($mEdit,$sSelLine[0] = 0  ?  $GUI_DISABLE : $GUI_ENABLE)
        GUICtrlSetState($mON, $sSelLine[0] = 0  ? $GUI_DISABLE: $GUI_ENABLE)
        GUICtrlSetState($mOFF,$sSelLine[0] = 0  ? $GUI_DISABLE: $GUI_ENABLE)
    EndIf
;~  Return $aHit[0]
EndFunc


Func WM_NOTIFY($hWnd, $iMsg, $iwParam, $ilParam)
    Local $hWndFrom, $iIDFrom, $iCode, $tNMHDR, $hWndListView, $tInfo
    $hWndListView = $idListview
    If Not IsHWnd($idListview) Then $hWndListView = GUICtrlGetHandle($idListview)

    $tNMHDR = DllStructCreate($tagNMHDR, $ilParam)
    $hWndFrom = HWnd(DllStructGetData($tNMHDR, "hWndFrom"))
    ;$iIDFrom = DllStructGetData($tNMHDR, "IDFrom")
    $iCode = DllStructGetData($tNMHDR, "Code")
    Switch $hWndFrom
        Case $hWndListView
            Switch $iCode
                Case $NM_RCLICK ; Sent by a list-view control when the user clicks an item with the left mouse button
;~                  msgbox(0, "", "22")
                    ListView_RClick()
;~                     Return 0
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NOTIFY

 

Edited by MarkLee
Link to comment
Share on other sites

Try this :

Func WM_NOTIFY($hWnd, $iMsg, $wParam, $lParam)
Local $tNMHDR, $hdlWindowFrom, $intMessageCode

  $tNMHDR = DllStructCreate($tagNMHDR, $lParam)

  $hdlWindowFrom = DllStructGetData($tNMHDR, "hWndFrom")
  $intMessageCode = DllStructGetData($tNMHDR, "Code")

  If $hdlWindowFrom = $hdlListView and $intMessageCode = $NM_RCLICK Then
    Local $aSel = _GUICtrlListView_GetSelectedIndices ($hdlListView, True)
    If $aSel[0] = 0 Then
      ToggleMenu (False,False,True,False,False)
    Else
      Local $sStatus = ReadStatus ($hdlListView,$aSel)
      If $sStatus = "ON" Then
        ToggleMenu (True,$aSel[0] > 1 ? False : True,True,False,True)
      ElseIf $sStatus = "OFF" Then
        ToggleMenu (True,$aSel[0] > 1 ? False : True,True,True,False)
      Else
        ToggleMenu (True,$aSel[0] > 1 ? False : True,True,True,True)
      EndIf
    EndIf
  EndIf

  Return $GUI_RUNDEFMSG

EndFunc

Func ToggleMenu ($bDel, $bEdit, $bAdd, $bON, $bOFF)

  If $bDel Then GUICtrlSetState($mDel, $GUI_ENABLE)
  If not $bDel Then GUICtrlSetState($mDel, $GUI_DISABLE)
  If $bEdit Then GUICtrlSetState($mEdit, $GUI_ENABLE)
  If not $bEdit Then GUICtrlSetState($mEdit, $GUI_DISABLE)
  If $bAdd Then GUICtrlSetState($mAdd, $GUI_ENABLE)
  If not $bAdd Then GUICtrlSetState($mAdd, $GUI_DISABLE)
  If $bON Then GUICtrlSetState($mON, $GUI_ENABLE)
  If not $bON Then GUICtrlSetState($mON, $GUI_DISABLE)
  If $bOFF Then GUICtrlSetState($mOFF, $GUI_ENABLE)
  If not $bOFF Then GUICtrlSetState($mOFF, $GUI_DISABLE)

EndFunc

Func ReadStatus ($hdlListView,$aSel)
Local $sText = "", $sTmp

  For $i = 1 to $aSel[0]
    $sTmp = _GUICtrlListView_GetItemText ($hdlListView,$aSel[$i],2)
    If $sText <> $sTmp Then $sText &= $sTmp
  Next
  Return $sText

EndFunc

 

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...