Jump to content
Sign in to follow this  
storme

GUICtrlSetStateMult

Recommended Posts

storme

G'day all

I'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
EndFunc

Thank you

John Morrison

aka

Storm-E

Edited by storme

Share this post


Link to post
Share on other sites
Melba23

storme,

That is a nice way to do it. :mellow:

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

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 :party: ).

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
Mat

Nice to see you again john - Long time no see. :P

This works... Up to 11 controls at a time.

#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 :mellow: )

Share this post


Link to post
Share on other sites
storme

Nice to see you again john - Long time no see. :party:

This works... Up to 11 controls at a time.

<SNIP>

Mat (You'll probably remember me as MDiesel :mellow: )

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. :party: Not I know why. :party:

I've been around the forums poking my hose in from time to time. Mostly reading and collecting ideas :P I have the "new content" page open on my computer all the time. Yep another Autoit Tragic. :party:

Keep up the great work and see you around!

Share this post


Link to post
Share on other sites
storme

storme,

That is a nice way to do it. :mellow:

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

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 :party: ).

M23

Groups 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!

Share this post


Link to post
Share on other sites
Mat

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 :mellow:

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. :party: Not I know why. :party:

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 :party: That'll teach me for changing names :P

I've been around the forums poking my hose in from time to time. Mostly reading and collecting ideas :party: I have the "new content" page open on my computer all the time. Yep another Autoit Tragic. :ILA:

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) :mad: This is a very useful scrap, why didn't I think of it? ;)

#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

Share this post


Link to post
Share on other sites
storme

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 :P

Well you're safe in my list I never throw anything out. :nuke::

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) :P This is a very useful scrap, why didn't I think of it?

Share this post


Link to post
Share on other sites
storme

#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 :blink: 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.

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
Sign in to follow this  

×