storme Posted July 1, 2010 Share Posted July 1, 2010 (edited) G'day allI'm looking for an easy way to set the state of multiple controls without repeating GUICtrlSetState({CONTROL}, {state}) over and over.It gets real OLD if I've got a series of controls that need to be individually disabled depending some external input.I was thinking of an array of control IDs but that make the code hard to read $StartButton is a lot easyer to understand than $controlArray[3].So I came up with the small function below. (The GUI is just for testing)Could you have a look and tell me if there is an easier way of achieving the same ends.#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form= Local $Form1 = GUICreate("Form1", 633, 447, 192, 124) Local $Button1 = GUICtrlCreateButton("Button1", 64, 72, 81, 41, $WS_GROUP) Local $Input1 = GUICtrlCreateInput("Input1", 64, 144, 73, 21) Local $Label1 = GUICtrlCreateLabel("Label1", 64, 200, 36, 17) Local $Button2 = GUICtrlCreateButton("Button2", 184, 80, 97, 33, $WS_GROUP) Local $Input2 = GUICtrlCreateInput("Input2", 184, 144, 97, 21) Local $Label2 = GUICtrlCreateLabel("Label2", 184, 192, 36, 17) GUISetState(@SW_SHOW) MsgBox(0,"test","Before Disable ") GUICtrlSetStateMult("$Button1,$input1,$label1", $GUI_DISABLE) MsgBox(0,"test","Before Enable ") GUICtrlSetStateMult("$Button1,$input1,$label1", $GUI_ENABLE) MsgBox(0,"test","Before Disable $Button1,$label1 only") GUICtrlSetStateMult("$Button1,$label1", $GUI_DISABLE) MsgBox(0,"test","DONE") #EndRegion ### END Koda GUI section ### Exit Func GUICtrlSetStateMult($sControls, $sState) ; $txtArray - contains list of controls to set the state of seperated by , Local $asControls = StringSplit($sControls,",") For $iLoop = 1 To $asControls[0] GUICtrlSetState(Execute($asControls[$iLoop]), $sState) Next EndFuncThank youJohn MorrisonakaStorm-E Edited July 1, 2010 by storme Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted July 1, 2010 Moderators Share Posted July 1, 2010 storme,That is a nice way to do it. Personally I tend to group the controls together when creating them so I can use the ControlIDs directly a loop, but that sometimes requires a bit of rearrangement of the creation code after you have decided what needs to be grouped. By the way, using Local in the main body of the script is does not affect the scope of the variables - they are Global by default (try reading one of them in the function to check ).M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Mat Posted July 1, 2010 Share Posted July 1, 2010 Nice to see you again john - Long time no see. This works... Up to 11 controls at a time. expandcollapse popup#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) #Region ### START Koda GUI section ### Form= Global $Form1 = GUICreate("Form1", 633, 447, 192, 124) Global $Button1 = GUICtrlCreateButton("Button1", 64, 72, 81, 41, $WS_GROUP) Global $Input1 = GUICtrlCreateInput("Input1", 64, 144, 73, 21) Global $Label1 = GUICtrlCreateLabel("Label1", 64, 200, 36, 17) Global $Button2 = GUICtrlCreateButton("Button2", 184, 80, 97, 33, $WS_GROUP) Global $Input2 = GUICtrlCreateInput("Input2", 184, 144, 97, 21) Global $Label2 = GUICtrlCreateLabel("Label2", 184, 192, 36, 17) GUISetState(@SW_SHOW) MsgBox(0,"test","Before Disable ") GUICtrlSetStateMult($Button1, $input1, $label1, $GUI_DISABLE) MsgBox(0,"test","Before Enable ") GUICtrlSetStateMult($Button1, $input1, $label1, $GUI_ENABLE) MsgBox(0,"test","Before Disable Button1,label1 only") GUICtrlSetStateMult($Button1, $label1, $GUI_DISABLE) MsgBox(0,"test","DONE") #EndRegion ### END Koda GUI section ### Exit Func GUICtrlSetStateMult($iCtrl1, $iCtrl2, $iCtrl3 = 0, $iCtrl4 = 0, $iCtrl5 = 0, $iCtrl6 = 0, $iCtrl7 = 0, $iCtrl8 = 0, $iCtrl9 = 0, $iCtrl10 = 0, $iCtrl11 = 0, $iCtrl12 = 0) Local $iState = Eval("iCtrl" & @NumParams) ; Last param If IsArray($iCtrl1) Then For $i = 0 To UBound($iCtrl1) - 1 GUICtrlSetState($iCtrl1[$i], $iState) Next Else For $i = 1 To @NumParams - 1 GUICtrlSetState(Eval("iCtrl" & $i), $iState) Next EndIf EndFunc Mat (You'll probably remember me as MDiesel ) AutoIt Project Listing Link to comment Share on other sites More sharing options...
storme Posted July 1, 2010 Author Share Posted July 1, 2010 Nice to see you again john - Long time no see. This works... Up to 11 controls at a time.<SNIP>Mat (You'll probably remember me as MDiesel )I LIKE IT!!!I didn't know about the @NumParams macro at all. AutoIT is constantly surprising me.AND you've set it up for ARRAY input as well.Also it solves one of the wories I had which was name checking.WELL DONE & THANKS!Yes MAT (clear throat) MDiesel.... I did think MAT's style looked familiar. Not I know why. I've been around the forums poking my hose in from time to time. Mostly reading and collecting ideas I have the "new content" page open on my computer all the time. Yep another Autoit Tragic. Keep up the great work and see you around! Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E Link to comment Share on other sites More sharing options...
storme Posted July 1, 2010 Author Share Posted July 1, 2010 storme,That is a nice way to do it. Personally I tend to group the controls together when creating them so I can use the ControlIDs directly a loop, but that sometimes requires a bit of rearrangement of the creation code after you have decided what needs to be grouped. By the way, using Local in the main body of the script is does not affect the scope of the variables - they are Global by default (try reading one of them in the function to check ).M23Groups are great (and my preference) but the appication I'm thinking of will be for multiple versions of Windows (xp, vista, 7 , 32bit, 64bit) so what is and isn't disabled could be spread through the controls.The LOCALs in the main body were only there from Koda. BUT THANKS as I didn't know variables in the main were global. That could have caused me a lot of pain.Thanks for the help! Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E Link to comment Share on other sites More sharing options...
Mat Posted July 1, 2010 Share Posted July 1, 2010 I LIKE IT!!! I didn't know about the @NumParams macro at all. AutoIT is constantly surprising me.Should have read the helpfile... It's a thrilling read from beginning to end, you can print it off and read it at night, or there's the online version so you can read it anywhere AND you've set it up for ARRAY input as well. Also it solves one of the wories I had which was name checking. WELL DONE & THANKS!See the latest version below for better array support.... Yes MAT (clear throat) MDiesel.... I did think MAT's style looked familiar. Not I know why. I had to tell everyone my secret identity here after I got "weeded" out of TheSaint's friends list because he didn't know who I was That'll teach me for changing names I've been around the forums poking my hose in from time to time. Mostly reading and collecting ideas I have the "new content" page open on my computer all the time. Yep another Autoit Tragic. Join the club, although I'm on 20 a day at the moment. It's a bad habit I'm trying to stop, but it's addictive. Keep up the great work and see you around! New version, with a fancy header. You can now have lots of arrays, and it returns how many succeeded and failed (@error) This is a very useful scrap, why didn't I think of it? expandcollapse popup#include <GUIConstantsEx.au3> Global $Form1 = GUICreate("Form1", 633, 447, 192, 124) Global $Button1 = GUICtrlCreateButton("Button1", 64, 72, 81, 41) Global $Input1 = GUICtrlCreateInput("Input1", 64, 144, 73, 21) Global $Label1 = GUICtrlCreateLabel("Label1", 64, 200, 36, 17) Global $Button2 = GUICtrlCreateButton("Button2", 184, 80, 97, 33) Global $Input2 = GUICtrlCreateInput("Input2", 184, 144, 97, 21) Global $Label2 = GUICtrlCreateLabel("Label2", 184, 192, 36, 17) GUISetState(@SW_SHOW) MsgBox(0, "test", "Before Disable ") $test = GUICtrlSetStateMult($Button1, $Input1, $Label1, $GUI_DISABLE) MsgBox(0, @error & " :: " & $test, "Before Enable ") $test = GUICtrlSetStateMult($Button1, $Input1, $Label1, $GUI_ENABLE) MsgBox(0, @error & " :: " & $test, "Before Disable Button1,label1 only") $test = GUICtrlSetStateMult($Button1, $Label1, 525252, $GUI_DISABLE) MsgBox(0, @error & " :: " & $test, "DONE") ; #FUNCTION# ==================================================================================================================== ; Name...........: GUICtrlSetStateMult ; Description ...: Sets the state of one or more controls. ; Syntax.........: GUICtrlSetStateMult( control1 [, control2 [, controlN ]] , State ) ; Parameters ....: control - Up to 11 controls can be given as arguments, the function will set the state of all of ; them in the order they are given. At least 1 control must be specified. If a control ; argument is an array then each array element will be treated as a control Id. ; State - The state to set to. The last parameter will always be taken as the state, which could ; cause problems if you set this as a control Id instead. ; Return values .: The number of controls that the state was set for. The number of failures is returned in the @error flag. ; Author(s) .....: StormE, Matt Diesel (Mat) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func GUICtrlSetStateMult($iCtrl1, $iCtrl2, $iCtrl3 = 0, $iCtrl4 = 0, $iCtrl5 = 0, $iCtrl6 = 0, $iCtrl7 = 0, $iCtrl8 = 0, _ $iCtrl9 = 0, $iCtrl10 = 0, $iCtrl11 = 0, $iCtrl12 = 0) Local $iState = Eval("iCtrl" & @NumParams) ; Last param Local $iRet = 0, $iTotal = 0, $vTmp For $i = 1 To @NumParams - 1 $vTmp = Eval("iCtrl" & $i) If IsArray($vTmp) Then If UBound($vTmp, 0) <> 1 Then ContinueLoop For $n = 0 To UBound($vTmp) - 1 $iRet += GUICtrlSetState($vTmp[$n], $iState) $iTotal += 1 Next Else $iRet += GUICtrlSetState($vTmp, $iState) $iTotal += 1 EndIf Next Return SetError($iTotal - $iRet, 0, $iRet) EndFunc ;==>GUICtrlSetStateMult Mat AutoIt Project Listing Link to comment Share on other sites More sharing options...
storme Posted July 2, 2010 Author Share Posted July 2, 2010 Should have read the helpfile... It's a thrilling read from beginning to end, you can print it off and read it at night, or there's the online version so you can read it anywhere Now I know you've gone around the bend.... *smile* Then again I'm going shopping with the wife Pitty I don't have screen I can carry it with me. I had to tell everyone my secret identity here after I got "weeded" out of TheSaint's friends list because he didn't know who I was That'll teach me for changing names Well you're safe in my list I never throw anything out. Join the club, although I'm on 20 a day at the moment. It's a bad habit I'm trying to stop, but it's addictive. I just wish their wasn't so many SMART people on here. Everytime I "think" I can answer something someone steps in and gives an answer that's 3 times as good as anything I could think up. New version, with a fancy header. You can now have lots of arrays, and it returns how many succeeded and failed (@error) This is a very useful scrap, why didn't I think of it? Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E Link to comment Share on other sites More sharing options...
storme Posted July 2, 2010 Author Share Posted July 2, 2010 expandcollapse popup#include <GUIConstantsEx.au3> Global $Form1 = GUICreate("Form1", 633, 447, 192, 124) Global $Button1 = GUICtrlCreateButton("Button1", 64, 72, 81, 41) Global $Input1 = GUICtrlCreateInput("Input1", 64, 144, 73, 21) Global $Label1 = GUICtrlCreateLabel("Label1", 64, 200, 36, 17) Global $Button2 = GUICtrlCreateButton("Button2", 184, 80, 97, 33) Global $Input2 = GUICtrlCreateInput("Input2", 184, 144, 97, 21) Global $Label2 = GUICtrlCreateLabel("Label2", 184, 192, 36, 17) GUISetState(@SW_SHOW) MsgBox(0, "test", "Before Disable ") $test = GUICtrlSetStateMult($Button1, $Input1, $Label1, $GUI_DISABLE) MsgBox(0, @error & " :: " & $test, "Before Enable ") $test = GUICtrlSetStateMult($Button1, $Input1, $Label1, $GUI_ENABLE) MsgBox(0, @error & " :: " & $test, "Before Disable Button1,label1 only") $test = GUICtrlSetStateMult($Button1, $Label1, 525252, $GUI_DISABLE) MsgBox(0, @error & " :: " & $test, "DONE") ; #FUNCTION# ==================================================================================================================== ; Name...........: GUICtrlSetStateMult ; Description ...: Sets the state of one or more controls. ; Syntax.........: GUICtrlSetStateMult( control1 [, control2 [, controlN ]] , State ) ; Parameters ....: control - Up to 11 controls can be given as arguments, the function will set the state of all of ; them in the order they are given. At least 1 control must be specified. If a control ; argument is an array then each array element will be treated as a control Id. ; State - The state to set to. The last parameter will always be taken as the state, which could ; cause problems if you set this as a control Id instead. ; Return values .: The number of controls that the state was set for. The number of failures is returned in the @error flag. ; Author(s) .....: StormE, Matt Diesel (Mat) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: Yes ; =============================================================================================================================== Func GUICtrlSetStateMult($iCtrl1, $iCtrl2, $iCtrl3 = 0, $iCtrl4 = 0, $iCtrl5 = 0, $iCtrl6 = 0, $iCtrl7 = 0, $iCtrl8 = 0, _ $iCtrl9 = 0, $iCtrl10 = 0, $iCtrl11 = 0, $iCtrl12 = 0) Local $iState = Eval("iCtrl" & @NumParams) ; Last param Local $iRet = 0, $iTotal = 0, $vTmp For $i = 1 To @NumParams - 1 $vTmp = Eval("iCtrl" & $i) If IsArray($vTmp) Then If UBound($vTmp, 0) <> 1 Then ContinueLoop For $n = 0 To UBound($vTmp) - 1 $iRet += GUICtrlSetState($vTmp[$n], $iState) $iTotal += 1 Next Else $iRet += GUICtrlSetState($vTmp, $iState) $iTotal += 1 EndIf Next Return SetError($iTotal - $iRet, 0, $iRet) EndFunc ;==>GUICtrlSetStateMult Mat You should suggest these changes to our elustrius benifactors the AutoIT authors. It doesn't look like it would take much to add but adds useful functionailty to the routines. Yours is even backward compatible with the current syntax. Some of my small contributions to AutoIt Browse for Folder Dialog - Automation SysTreeView32 | FileHippo Download and/or retrieve program information | Get installedpath from uninstall key in registry | RoboCopy function John Morrison aka Storm-E 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