Hobbyist Posted December 4, 2014 Share Posted December 4, 2014 Here is my code (after reading about menus) and I am trying to learn how to control a selection with the most efficient code. It seems cumbersome to have almost identical coding when there is a small difference of a variable in each of the two.The variable in each case would be used in a function elsewhere. Am I trying to do the impossible or can anyone give me some guidance on the best way to do this. Thanks in advance. Hobbyist expandcollapse popup;<<<<<<<<<<<<<<<<<<<<<<<< #include <Array.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <GuiMenu.au3> ;<<<<<<<<<<<< #Region ### START Koda GUI section ### Form=C:\Users\Steven\Autoit Trys\Vendors Trials\My combo Form Test.kxf $main = GUICreate(" Dash Board", 680, 515, 150, 100) ;height was 480 Local $idViewmenu = GUICtrlCreateMenu("&View") Local $idViewFiles = GUICtrlCreateMenuItem("View Names", $idViewmenu) GUICtrlSetState($idViewFiles, $GUI_unCHECKED) Local $idViewLogFiles = GUICtrlCreateMenu("View/Edit Log Details", $idViewmenu) GUICtrlSetState($idViewLogFiles, $GUI_unCHECKED) local $viewVCA = GUICtrlCreateMenuItem("View: V/C/A", $idViewLogFiles) GUICtrlSetState($viewVCA, $GUI_unCHECKED) local $viewCVA = GUICtrlCreateMenuItem("View: C/V/A", $idViewLogFiles) GUICtrlSetState($viewCVA, $GUI_unCHECKED) GUISetState(@SW_SHOW) ;~ ;#EndRegion ### END Koda GUI section ### While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE Exit Case $viewVCA GUICtrlSetState($viewCVA, $GUI_unCHECKED) If GUICtrlSetState($ViewVCA, $GUI_CHECKED) Then ;do a bunch of code, 20 lines for instance ; ; ;a variable = "x" for instance in another part of code, a function for instance ; ; ; MsgBox($MB_SYSTEMMODAL, "Check", "The VCA view was clicked.") ;just for example purposes GUICtrlSetState($viewVCA, $GUI_unCHECKED) EndIf Case $viewCVA GUICtrlSetState($viewVCA, $GUI_unCHECKED) If GUICtrlSetState($ViewCVA, $GUI_CHECKED) Then ;do a bunch of code, 20 lines for instance ; ; ;a variable = "y" for instance in another part of code, a function for instance ; ; ; ;same bunch of code as above MsgBox($MB_SYSTEMMODAL, "Check", "The CVA view was clicked.");just for example purposes GUICtrlSetState($viewCVA, $GUI_unCHECKED) EndIf EndSwitch WEnd Link to comment Share on other sites More sharing options...
mikell Posted December 4, 2014 Share Posted December 4, 2014 Not sure I understood the question, maybe this ? expandcollapse popup;<<<<<<<<<<<<<<<<<<<<<<<< #include <Array.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <GuiMenu.au3> ;<<<<<<<<<<<< $main = GUICreate(" Dash Board", 680, 515, 150, 100) ;height was 480 Local $idViewmenu = GUICtrlCreateMenu("&View") Local $idViewFiles = GUICtrlCreateMenuItem("View Names", $idViewmenu) GUICtrlSetState($idViewFiles, $GUI_unCHECKED) Local $idViewLogFiles = GUICtrlCreateMenu("View/Edit Log Details", $idViewmenu) GUICtrlSetState($idViewLogFiles, $GUI_unCHECKED) local $viewVCA = GUICtrlCreateMenuItem("View: V/C/A", $idViewLogFiles) GUICtrlSetState($viewVCA, $GUI_unCHECKED) local $viewCVA = GUICtrlCreateMenuItem("View: C/V/A", $idViewLogFiles) GUICtrlSetState($viewCVA, $GUI_unCHECKED) GUISetState(@SW_SHOW) While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE Exit Case $viewVCA GUICtrlSetState($viewCVA, $GUI_unCHECKED) If GUICtrlSetState($ViewVCA, $GUI_CHECKED) Then $var = "The VCA view was clicked." _func($var) GUICtrlSetState($viewVCA, $GUI_unCHECKED) EndIf Case $viewCVA GUICtrlSetState($viewVCA, $GUI_unCHECKED) If GUICtrlSetState($ViewCVA, $GUI_CHECKED) Then $var = "The CVA view was clicked." _func($var) GUICtrlSetState($viewCVA, $GUI_unCHECKED) EndIf EndSwitch WEnd Func _func($param) MsgBox($MB_SYSTEMMODAL, "Check", $param) ;just for example purposes EndFunc Link to comment Share on other sites More sharing options...
Hobbyist Posted December 4, 2014 Author Share Posted December 4, 2014 Thanks! I wasn't sure how to word it. I can do what you have replied with. The code in both "cases" is the same with the noted exceptions (like VCA versus CVA and the reference variable). So it seems to me there should be a way to "combine the two cases into one" and cut out the repetitious coding especially if there are a lot of lines in the Cases. I know from the help section the following is possible: Case "value" to "value" but then thought perhaps the following would work: Case "value" or "value" and then do qualifying statement s(if, then etc) and only have one set of coding versus the repetition. I tried but it failed and most likely because I don't know if the OR works in it or how to set it up. Bottom line - trying to shorten the number of lines of code and still get results. Hope this clarifies it and thanks for such a quick response. I new at this and not always sure how to word the questions. Hobbyist. Link to comment Share on other sites More sharing options...
mikell Posted December 4, 2014 Share Posted December 4, 2014 The problem will be to define for each Case a different value for the variable, which is not posssible using 'Case "value" to "value" ' One way could be a 2D array to store the IDs of the menuitems and their associated variable, and then loop through this array : expandcollapse popup;<<<<<<<<<<<<<<<<<<<<<<<< #include <Array.au3> #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <GuiMenu.au3> ;<<<<<<<<<<<< $main = GUICreate(" Dash Board", 680, 515, 150, 100) ;height was 480 Local $idViewmenu = GUICtrlCreateMenu("&View") Local $idViewFiles = GUICtrlCreateMenuItem("View Names", $idViewmenu) GUICtrlSetState($idViewFiles, $GUI_unCHECKED) Local $idViewLogFiles = GUICtrlCreateMenu("View/Edit Log Details", $idViewmenu) GUICtrlSetState($idViewLogFiles, $GUI_unCHECKED) Global $items[2][2] $items[0][0] = GUICtrlCreateMenuItem("View: V/C/A", $idViewLogFiles) GUICtrlSetState(-1, $GUI_unCHECKED) $items[1][0] = GUICtrlCreateMenuItem("View: C/V/A", $idViewLogFiles) GUICtrlSetState(-1, $GUI_unCHECKED) GUISetState(@SW_SHOW) $items[0][1] = "x" $items[1][1] = "y" While 1 $iMsg = GUIGetMsg() For $i = 0 to UBound($items)-1 Switch $iMsg Case $GUI_EVENT_CLOSE Exit Case $items[$i][0] If GUICtrlSetState($items[$i][0], $GUI_CHECKED) Then $var = $items[$i][1] _func($var) GUICtrlSetState($items[$i][0], $GUI_unCHECKED) EndIf EndSwitch Next WEnd Func _func($param) MsgBox($MB_SYSTEMMODAL, "Check", "$var = " & $param) ;just for example purposes EndFunc Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 4, 2014 Moderators Share Posted December 4, 2014 I know from the help section the following is possible: Case "value" to "value" but then thought perhaps the following would work: Case "value" or "value" and then do qualifying statement s(if, then etc) and only have one set of coding versus the repetition. I tried but it failed and most likely because I don't know if the OR works in it or how to set it up. For "Or": Case $value1, $value2, $value3 Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Hobbyist Posted December 5, 2014 Author Share Posted December 5, 2014 First solution works. Thank you. Second solution - I am having issues with. Here is my sample code, for which I get the same result regardless of which selection I make during execution. what am I doing wrong? Thanks. While 1 $iMsg = GUIGetMsg() Switch $iMsg Case $GUI_EVENT_CLOSE Exit Case $viewVCA, $viewCVA If GUICtrlSetState($ViewVCA, $GUI_CHECKED) Then GUICtrlSetState($viewCVA, $GUI_unCHECKED) $sortswitch = 1 EndIf If GUICtrlSetState($ViewCVA, $GUI_CHECKED) Then GUICtrlSetState($viewVCA, $GUI_unCHECKED) $sortswitch = 2 EndIf MsgBox($MB_SYSTEMMODAL, "Check", " clicked. "&$sortswitch) EndSwitch WEnd Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 5, 2014 Moderators Share Posted December 5, 2014 You're using "GUICtrlSetState" to verify if something is check? Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Hobbyist Posted December 5, 2014 Author Share Posted December 5, 2014 @ SmOke_N How would I verify if it is checked? GetState returns the state of a control. Now I am a bit confused, since i have seen other codes using SetState in that manner. Thanks again. Link to comment Share on other sites More sharing options...
Moderators SmOke_N Posted December 5, 2014 Moderators Share Posted December 5, 2014 (edited) To be honest, I use the udf versions. _GUICtrlMenu_GetItemChecked(). I modified the example script with an insert of code to give an idea on how maybe to approach it, but it also has an example on how to read standard controls checks too... using GUICtrlRead, so you get a bonus . expandcollapse popup#include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <StaticConstants.au3> #include <GuiMenu.au3> Example() Func Example() Local $hGUI = GUICreate("My GUI menu", 300, 200) Local $sStatus = "Ready" Local $idFileMenu = GUICtrlCreateMenu("&File") Local $idFileItem = GUICtrlCreateMenuItem("Open", $idFileMenu) GUICtrlSetState(-1, $GUI_DEFBUTTON) Local $idHelpMenu = GUICtrlCreateMenu("?") GUICtrlCreateMenuItem("Save", $idFileMenu) GUICtrlSetState(-1, $GUI_DISABLE) Local $idInfoItem = GUICtrlCreateMenuItem("Info", $idHelpMenu) Local $idExit = GUICtrlCreateMenuItem("Exit", $idFileMenu) Local $idRecentFilesMenu = GUICtrlCreateMenu("Recent Files", $idFileMenu, 1) GUICtrlCreateMenuItem("", $idFileMenu, 2) ; Create a separator line Local $idViewMenu = GUICtrlCreateMenu("View", -1, 1) ; Is created before "?" menu Local $idViewStatusItem = GUICtrlCreateMenuItem("Statusbar", $idViewMenu) GUICtrlSetState(-1, $GUI_CHECKED) GUICtrlCreateButton("OK", 50, 130, 70, 20) GUICtrlSetState(-1, $GUI_FOCUS) Local $idCancel = GUICtrlCreateButton("Cancel", 180, 130, 70, 20) Local $idStatusLabel = GUICtrlCreateLabel($sStatus, 0, 165, 300, 16, BitOR($SS_SIMPLE, $SS_SUNKEN)) ;#################################### code to look at ############################################################### Local $hMenu = _GUICtrlMenu_GetMenu($hGUI) Local $hViewMenu = _GUICtrlMenu_GetItemSubMenu($hMenu, 1) ; View menu ConsoleWrite("Is View Sub Item 0 checked? " & _GUICtrlMenu_GetItemChecked($hViewMenu, 0) & @CRLF) ;#################################### code to look at ############################################################### GUISetState(@SW_SHOW) Local $sFilePath ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $idCancel, $idExit Exit Case $idInfoItem MsgBox($MB_SYSTEMMODAL, "Info", "Only a test...") Case $idFileItem $sFilePath = FileOpenDialog("Choose a file...", @TempDir, "All (*.*)") If @error Then ContinueLoop EndIf GUICtrlCreateMenuItem($sFilePath, $idRecentFilesMenu) Case $idViewStatusItem If BitAND(GUICtrlRead($idViewStatusItem), $GUI_CHECKED) = $GUI_CHECKED Then GUICtrlSetState($idViewStatusItem, $GUI_UNCHECKED) GUICtrlSetState($idStatusLabel, $GUI_HIDE) Else GUICtrlSetState($idViewStatusItem, $GUI_CHECKED) GUICtrlSetState($idStatusLabel, $GUI_SHOW) EndIf EndSwitch WEnd EndFunc ;==>Example Edited December 5, 2014 by SmOke_N Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer. Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now