Jump to content

GUICtrlCreateMenu dynamic with a For...Next loop


guinness
 Share

Recommended Posts

2 hours ago, Nine said:

I think I understand what you mean (not exactly sure).  But anyway here a stronger approach I would use :

#include <GUIConstantsEx.au3>
#include <Array.au3>

_Main()

Func _Main()

  Local $array[8] = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]

  $hGUI = GUICreate("", 500, 300)

  $listview = GUICtrlCreateListView("categories", 20, 20, 460, 180)
  $mMenu = GUICtrlCreateContextMenu($listview)

  Local $aMenuId[UBound($array)]
  For $i = 0 To UBound($array) - 1
    $aMenuId[$i] = GUICtrlCreateMenuItem($array[$i], $mMenu)
  Next

  $idButton = GUICtrlCreateButton("OK", 200, 220, 100, 25)

  Local $idTemp = GUICtrlCreateMenuItem("end", $mMenu)
  _ArrayAdd($array, "end")
  _ArrayAdd($aMenuId, $idTemp)

  GUISetState(@SW_SHOW)
  While True
    $iMsg = GUIGetMsg()
    Switch $iMsg
      Case $GUI_EVENT_NONE ; not truly required but it will prevent triggering Case Else for nothing
      Case $GUI_EVENT_CLOSE
        Exit
      Case $idButton
        ConsoleWrite ("Button was pressed" & @CRLF)
      Case Else
        For $i = 0 To UBound($aMenuId) - 1
          If $iMsg = $aMenuId[$i] Then MsgBox(0, "Menu Selection", $array[$i])
        Next
    EndSwitch
  WEnd
EndFunc   ;==>_Main

 

amazing :)

thanks so much my friend :)

now I tried to delete an item but it still shown in the menu

_ArrayDelete($aMenuId,1)
_ArrayDelete($array,1)

 

Link to comment
Share on other sites

Button deletes 4th menu item altogether : 

#include <GUIConstantsEx.au3>
#include <Array.au3>

_Main()

Func _Main()

  Local $array[8] = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]

  $hGUI = GUICreate("", 500, 300)

  $listview = GUICtrlCreateListView("categories", 20, 20, 460, 180)
  $mMenu = GUICtrlCreateContextMenu($listview)

  Local $aMenuId[UBound($array)]
  For $i = 0 To UBound($array) - 1
    $aMenuId[$i] = GUICtrlCreateMenuItem($array[$i], $mMenu)
  Next

  $idButton = GUICtrlCreateButton("OK", 200, 220, 100, 25)

  Local $idTemp = GUICtrlCreateMenuItem("end", $mMenu)
  _ArrayAdd($array, "end")
  _ArrayAdd($aMenuId, $idTemp)

  GUISetState(@SW_SHOW)
  While True
    $iMsg = GUIGetMsg()
    Switch $iMsg
      Case $GUI_EVENT_NONE ; not truly required but it will prevent triggering Case Else for nothing
      Case $GUI_EVENT_CLOSE
        Exit
      Case $idButton
        ConsoleWrite ("Button was pressed" & @CRLF) ; delete 4th menu item
        GUICtrlDelete($aMenuId[4])
        _ArrayDelete($aMenuId, 4)
        _ArrayDelete($array, 4)
      Case Else
        For $i = 0 To UBound($aMenuId) - 1
          If $iMsg = $aMenuId[$i] Then MsgBox(0, "Menu Selection", $array[$i])
        Next
    EndSwitch
  WEnd
EndFunc   ;==>_Main

 

Link to comment
Share on other sites

On 11/23/2020 at 10:27 PM, Nine said:

Button deletes 4th menu item altogether : 

#include <GUIConstantsEx.au3>
#include <Array.au3>

_Main()

Func _Main()

  Local $array[8] = ["Sat", "Sun", "Mon", "Tue", "Wed", "Thu", "Fri"]

  $hGUI = GUICreate("", 500, 300)

  $listview = GUICtrlCreateListView("categories", 20, 20, 460, 180)
  $mMenu = GUICtrlCreateContextMenu($listview)

  Local $aMenuId[UBound($array)]
  For $i = 0 To UBound($array) - 1
    $aMenuId[$i] = GUICtrlCreateMenuItem($array[$i], $mMenu)
  Next

  $idButton = GUICtrlCreateButton("OK", 200, 220, 100, 25)

  Local $idTemp = GUICtrlCreateMenuItem("end", $mMenu)
  _ArrayAdd($array, "end")
  _ArrayAdd($aMenuId, $idTemp)

  GUISetState(@SW_SHOW)
  While True
    $iMsg = GUIGetMsg()
    Switch $iMsg
      Case $GUI_EVENT_NONE ; not truly required but it will prevent triggering Case Else for nothing
      Case $GUI_EVENT_CLOSE
        Exit
      Case $idButton
        ConsoleWrite ("Button was pressed" & @CRLF) ; delete 4th menu item
        GUICtrlDelete($aMenuId[4])
        _ArrayDelete($aMenuId, 4)
        _ArrayDelete($array, 4)
      Case Else
        For $i = 0 To UBound($aMenuId) - 1
          If $iMsg = $aMenuId[$i] Then MsgBox(0, "Menu Selection", $array[$i])
        Next
    EndSwitch
  WEnd
EndFunc   ;==>_Main

 

sorry for delay but I thought my self that I've replied :)

thanks so much my friend you are amazing :)

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