dmlarsen30

Dynamic Menu bug

7 posts in this topic

I know that I'm doing something basic, and doing it wrong. I just can't seem to figure it out...I am trying to create a dynamic menu that changes when the user switches "modes."  This is my code:

Func _SetActions($var)

   $Menu_handle = _GUICtrlMenu_GetMenu($GUI_MF_BIS)
   _GUICtrlMenu_DeleteMenu($Menu_handle, 3)

   $New_Acts = _GUICtrlMenu_CreateMenu()

   Switch $var
   Case 0
      _GUICtrlMenu_InsertMenuItem($New_Acts, 0, "Add", $Menu_Act_Add_Tray)
      _GUICtrlMenu_InsertMenuItem($New_Acts, 1, "Delete", $Menu_Act_Delete_Tray)
      _GUICtrlMenu_InsertMenuItem($New_Acts, 2, "Edit", $Menu_Act_Edit_Tray)
      _GUICtrlMenu_InsertMenuItem($New_Acts, 3, "Move", $Menu_Act_Move_Tray)
   Case 1
      _GUICtrlMenu_InsertMenuItem($New_Acts, 0, "Add", $Menu_Act_Add_Strain)
      _GUICtrlMenu_InsertMenuItem($New_Acts, 1, "Delete", $Menu_Act_Delete_Strain)
      _GUICtrlMenu_InsertMenuItem($New_Acts, 2, "Edit", $Menu_Act_Edit_Strain)
    EndSwitch

   _GUICtrlMenu_InsertMenuItem($Menu_handle, 3, "Actions", 0, $New_Acts)
   _GUICtrlMenu_DrawMenuBar($GUI_MF_BIS)

EndFunc

I call this function with an argument of either 0 or 1. I delete the existing menu and build a new one. This part of the code works just fine, the menu looks the way it's supposed to. However, in the main loop of my program:

While 1
    $nMsg = GUIGetMsg()
      Switch $nMsg
      Case $GUI_EVENT_CLOSE, $Menu_File_Exit
            Exit
         Case $Menu_Cat_Trays
            _SetActions(0)
         Case $Menu_Cat_Strains
            _SetActions(1)
         Case $Menu_Act_Add_Strain
            MsgBox(0,"","Got Menu_Act_Add_Strain")
      EndSwitch
WEnd

The "Case $Menu_Act_Add_Strain" fires on every iteration of the loop. 

What am I doing wrong here?  I'm sure it's something simple, but I'm stuck.

If there's a better way to do this, I'm all ears.

Thanks in advance.

 

Share this post


Link to post
Share on other sites



may be the error is between lines 6-20 of original script.

Share this post


Link to post
Share on other sites
17 minutes ago, AutoBert said:

may be the error is between lines 6-20 of original script.

Please elaborate?

Share this post


Link to post
Share on other sites

Post your script or a runable reprocuder where error occurs!

Share this post


Link to post
Share on other sites

dmlarsen30,

Why are you using the UDF functions to do this? I would code the GUI something like this:

#include <GUIConstantsEx.au3>
#include <MsgBoxConstants.au3>

Global $aMenuItems, $iModeSet = 1

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

$mMenu = GUICtrlCreateMenu("Menu")
_SetActions(0)

$cMode_0 = GUICtrlCreateButton("Mode 0", 10, 10, 80, 30)
$cMode_1 = GUICtrlCreateButton("Mode 1", 10, 50, 80, 30)

GUISetState()

While 1

    $iMsg = GUIGetMsg()
    Switch $iMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $cMode_0
            _SetActions(0)
        Case $cMode_1
            _SetActions(1)
        Case Else
            For $i = 0 To UBound($aMenuItems) - 1
                If $iMsg = $aMenuItems[$i][0] Then
                    MsgBox($MB_SYSTEMMODAL, "Clicked", $aMenuItems[$i][1])
                    ExitLoop
                EndIf
            Next
    EndSwitch

WEnd

Func _SetActions($iMode = 0)

    Switch $iMode
        Case 0
            If $iModeSet = 1 Then
                For $i = 0 To UBound($aMenuItems) - 1
                    GUICtrlDelete($aMenuItems[$i][0])
                Next
                Global $aMenuItems[4][2]
                $aMenuItems[0][0] = GUICtrlCreateMenuItem("Add", $mMenu)
                $aMenuItems[0][1] = "Add"
                $aMenuItems[1][0] = GUICtrlCreateMenuItem("Delete", $mMenu)
                $aMenuItems[1][1] = "Delete"
                $aMenuItems[2][0] = GUICtrlCreateMenuItem("Edit", $mMenu)
                $aMenuItems[2][1] = "Edit"
                $aMenuItems[3][0] = GUICtrlCreateMenuItem("Move", $mMenu)
                $aMenuItems[3][1] = "Move"
                $iModeSet = 0
            EndIf

        Case 1
            If $iModeSet = 0 Then
                For $i = 0 To UBound($aMenuItems) - 1
                    GUICtrlDelete($aMenuItems[$i][0])
                Next
                Global $aMenuItems[3][2]
                $aMenuItems[0][0] = GUICtrlCreateMenuItem("Add", $mMenu)
                $aMenuItems[0][1] = "Add"
                $aMenuItems[1][0] = GUICtrlCreateMenuItem("Delete", $mMenu)
                $aMenuItems[1][1] = "Delete"
                $aMenuItems[2][0] = GUICtrlCreateMenuItem("Edit", $mMenu)
                $aMenuItems[2][1] = "Edit"
                $iModeSet = 1
            EndIf
    EndSwitch

EndFunc   ;==>_SetActions

Much cleaner and using the native commands.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Thank you Melba23, I'll take a closer look at your code and see if I can modify it to work for me. I tried using the native commands, but for whatever reason couldn't get it to work properly. I guess I thought it would be easier to delete the entire menu with  _GUICtrlMenu_DeleteMenu() and then rebuild it, rather than to delete each item one at a time. Thank you for your help.

 

Share this post


Link to post
Share on other sites
20 minutes ago, AutoBert said:

Post your script or a runable reprocuder where error occurs!

I did a little more testing of my code and it turns out you were correct AutoBert, the error is from outside my function.  My code works now that $Menu_Act_Add_Tray (and the other variables I'm using inside my function) are initialized to non-zero values.

Thanks for your help.

Share this post


Link to post
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