Jump to content

Recommended Posts

Posted (edited)

Just wanted to share my functions for managing a GUI with lots of controls and many screens.

I personally feel that it is very efficient and keeps line count to a minimum.

And if it's not, I'll learn a better solution!

Note: There are some conditional veriables such as $changes and $cont that are modified by the main loop in this particular script; they are meant as an example, please don't try too hard to understand why they're used.

*Usage*

This allows you to re-state a handful of buttons in one line when doing slight GUI changes:

ToggleCTRL($GUI_DISABLE, $dALO, $dAAF, $dRLA) (A few buttons on a certain screen)

ToggleCTRL($GUI_DISABLE, $dSAV, $dCAN) (Save/Cancel buttons)

AND

A single line solution to switching a GUI "screen":

GuiMod("MAIN", $GUI_HIDE)

GuiMod("DETAILS", $GUI_SHOW)

GuiMod("PLANS", $GUI_HIDE)

Another way I've used this to my advantage is as follows:

Say a GUI has 3 or 4 main screens, and 3 or 4 "form" screens.

When switching to a form screen, save the main screen that it was on to a variable; then once the user is done with the form, either submitting or cancelling, you can call the variable to revert back to the main screen it was on.

Func _GuiMod($i, $tog, $act = True)
   Switch $i
   Case "MAIN"
      If $tog = $GUI_SHOW And $changes Then
         $changes = False
         ControlClick($GUI, "", $LOGfg)
      Else
      EndIf
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 535, 445)
      _ToggleCTRL($tog, $TAB, $LOGfl, $LOGfc, $LOGfi, $LOGfg, $LOGta, $LOGvd)
      _ToggleCTRL($tog, $LOGtp, $LOGse)
   Case "DETAILS"
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 535, 475)
      Local $cont = True
      If $tog = $GUI_SHOW And $act Then
         If Not UpdateDetails() Then
            $cont = False
            _GuiMod("MAIN", $GUI_SHOW)
         EndIf
      EndIf
      If $cont Then
         _ToggleCTRL($tog, $dUID, $dICU, $dACC, $dCOF, $dNAM, $dCO2, $dIPW, $dEMA, $dEDI, $dLOG, $dMOD)
         _ToggleCTRL($tog, $dUPD, $dINP, $dAPL, $dALO, $dAAF, $dPLL, $dLOL, $dAFL, $dSAV, $dCAN, $dRPL)
         _ToggleCTRL($tog, $dRLA, $dACT, $dAC2, $dRET, $dDEL, $dADD, $dMQC)
      EndIf
   Case "ADD-INPUT"
      _ToggleCTRL($tog, $nCOMi, $nCO2i, $nNAMi, $nNAMb, $nEMAi, $nPREc, $nACCc, $nICUi, $nADDi, $nNOTi)
      _ToggleCTRL($tog, $nAPLb, $nALOb, $nAAFb, $nPLAl, $nLOCl, $nAFFl, $nRPLb, $nRLAb, $nADDb, $nCLEb)
   Case "PLANS"
      If $tog <> $GUI_SHOW Or Not $changing Then
         If $act Then _ToggleCTRL($tog, $dMOD, $dLOG, $dUPD, $dDEL)
         _ToggleCTRL($tog, $dINP, $dAPL, $dALO, $dAAF, $dPLL, $dLOL, $dAFL, $dSAV, $dCAN, $dRPL, $dRLA)
      EndIf
   Case "CHANGE"
      _ToggleCTRL($tog, $dITL, $dITC, $dAML, $dAMC, $dSCB, $dCCB, $dEAB)
      If $tog = $GUI_SHOW Then
         GUICtrlSetState($dRET, $GUI_DISABLE)
      Else
         GUICtrlSetState($dRET, $GUI_ENABLE)
      EndIf
   Case "NEW"
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 465, 475)
      _ToggleCTRL($tog, $TAB, $nCOMl, $nCOMi, $nCO2l, $nCO2i, $nNAMl, $nNAMi, $nEMAl, $nEMAi, $nPREl, $nPREc)
      _ToggleCTRL($tog, $nACCl, $nACCc, $nPAEb, $nICUl, $nICUi, $nADDi, $nAPLb, $nALOb, $nAAFb, $nPLAl, $nLOCl)
      _ToggleCTRL($tog, $nAFFl, $nRPLb, $nNOTl, $nNOTi, $nADDb, $nCLEb, $nLAST)
   Case "ACCESS"
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 465, 330)
      _ToggleCTRL($tog, $ACCd1, $ACCd2, $TYPla, $TYPli, $TYPad, $TYPde, $TYPmu, $TYPmd, $ACCla, $ACCli)
      _ToggleCTRL($tog, $ACCde, $ACCmu, $ACCmd, $ACCdl, $ACCdi, $ACCal, $ACCai, $ACCaa, $taRET)
   Case "TPi"
      _ToggleCTRL($tog, $tpOPEb, $tpCREr, $tpLINr, $tpCO2i, $tpEMAi, $tpEMAl, $tpCREb, $tpCOPb, $tpCOPi, $tpCTOi, $tpCGOb)
      _ToggleCTRL($tog, $tpUFLi, $tpUFLb, $tpICUi, $tpPLAi, $tpLINb, $tpDLIb, $tpUFMi, $tpSCAl, $tpCFAr, $tpCBAr)
   Case "TPC"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPL", $GUI_HIDE)
         _GuiMod("TPH", $GUI_HIDE)
         _GuiMod("TPD", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpCO2l, $tpCO2i, $tpCOPl, $tpCOPi, $tpCREb, $tpCOPb, $tpSEPl, $tpCTOl, $tpCTOi, $tpCGOb, $tpLAST)
      _ToggleCTRL($tog, $tpIDNl, $tpIDNi, $tpIDNn, $tpSCAl, $tpEMAi, $tpEMAl, $tpCFAr, $tpCBAr)
   Case "TPL"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPC", $GUI_HIDE)
         _GuiMod("TPH", $GUI_HIDE)
         _GuiMod("TPD", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpUFLl, $tpUFLi, $tpUFLb, $tpICUl, $tpICUi, $tpPLAl, $tpPLAi, $tpLINb, $tpDLIb, $tpUFMl, $tpUFMi, $tpIMAb, $tpIMRb)
   Case "TPH"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPC", $GUI_HIDE)
         _GuiMod("TPL", $GUI_HIDE)
         _GuiMod("TPD", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpUFLl, $tpUFLi, $tpUFLb, $tpUFMl, $tpUFMi, $tpCOCl, $tpCOCi, $tpCOGb)
   Case "TPD"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPC", $GUI_HIDE)
         _GuiMod("TPL", $GUI_HIDE)
         _GuiMod("TPH", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpDIDl, $tpDIDi, $tpDIDb)
   EndSwitch
EndFunc

Func _ToggleCTRL($tog, $c0 = 0, $c1 = 0, $c2 = 0, $c3 = 0, $c4 = 0, $c5 = 0, $c6 = 0, $c7 = 0, $c8 = 0, $c9 = 0, $c10 = 0, $c11 = 0, $c12 = 0)
   For $i = 0 To @NumParams
      GUICtrlSetState(Eval("c" & $i), $tog)
   Next
EndFunc

 

Edited by Aphotic
Posted

I see only functions.
Can you post also example to show us how to use it ?

 

Signature beginning:
Please remember: "AutoIt"..... *  Wondering who uses AutoIt and what it can be used for ? * Forum Rules *
ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Codefor other useful stuff click the following button:

  Reveal hidden contents

Signature last update: 2023-04-24

Posted

Small question/suggestion for your _ToggleCTRL function.  Why not just provide an array to the function?  That way you don't limit yourself to x (12) manually defined parameters and you eliminate the extra overhead from the Eval function.

Posted (edited)
  On 11/11/2016 at 9:30 PM, mLipok said:

I see only functions.
Can you post also example to show us how to use it ?

 

Expand  

Edited the first post.

  On 11/14/2016 at 7:54 PM, spudw2k said:

Small question/suggestion for your _ToggleCTRL function.  Why not just provide an array to the function?  That way you don't limit yourself to x (12) manually defined parameters and you eliminate the extra overhead from the Eval function.

Expand  

Sometimes, I call the ToggleCTRL function with different groups of control variables, maybe even just two to three.

I like your idea, although I wish I could call it with something like:

ToggleCTRL($GUI_DISABLE, [$nALOb, $nAAFb])

I don't like the idea of having to declare a local variable every time before calling it, unless I'm missing something...

Local $tcArr = [$nALOb, $nAAFb]

ToggleCTRL($GUI_DISABLE, $tcArr)

The idea is to reduce the line count in the main script logic; please advise?

 

Thank you to you both!

 

 

Edited by Aphotic
Posted (edited)

Well, you're right...you'd have to define the arrays, but not every time (unless it truly needs to be dynamic).  You could declare arrays for each collection of controls at the beginning of your script.  

edit:
Another idea I thought of to simply the array creation....you could use StringSplit to create an array from a character delimited string with your control IDs.  Really cutting out the Eval overhead is what I was suggesting.  

Edited by spudw2k
Posted
  On 11/15/2016 at 10:22 PM, spudw2k said:

Well, you're right...you'd have to define the arrays, but not every time (unless it truly needs to be dynamic).  You could declare arrays for each collection of controls at the beginning of your script.  

edit:
Another idea I thought of to simply the array creation....you could use StringSplit to create an array from a character delimited string with your control IDs.  Really cutting out the Eval overhead is what I was suggesting.  

Expand  

Yeah, I'd rather not define an array that's going to be used once.

The delimited string idea is perfect, thank you very much; I'll edit the first post with that!

Posted

I'm not sure if I prefer this method. It may reduce the need for Eval and turn it into a one line solution, although I've never had issues with performance when using Eval and the method I'm currently using is a tad easier to read.

Func _GuiMod($i, $tog, $act = True)
   Switch $i
   Case "MAIN"
      If $tog = $GUI_SHOW And $changes Then
         $changes = False
         ControlClick($GUI, "", $LOGfg)
      Else
      EndIf
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 535, 445)
      _ToggleCTRL($tog, $TAB &"|"& $LOGfl &"|"& $LOGfc &"|"& $LOGfi &"|"& $LOGfg &"|"& $LOGta &"|"& $LOGvd &"|"& $LOGtp &"|"& $LOGse)
   Case "DETAILS"
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 535, 475)
      Local $cont = True
      If $tog = $GUI_SHOW And $act Then
         If Not UpdateDetails() Then
            $cont = False
            _GuiMod("MAIN", $GUI_SHOW)
         EndIf
      EndIf
      If $cont Then
         _ToggleCTRL($tog, $dUID &"|"& $dICU &"|"& $dACC &"|"& $dCOF &"|"& $dNAM &"|"& $dCO2 &"|"& $dIPW &"|"& $dEMA &"|"& $dEDI &"|"& $dLOG &"|"& $dMOD)
         _ToggleCTRL($tog, $dUPD &"|"& $dINP &"|"& $dAPL &"|"& $dALO &"|"& $dAAF &"|"& $dPLL &"|"& $dLOL &"|"& $dAFL &"|"& $dSAV &"|"& $dCAN &"|"& $dRPL)
         _ToggleCTRL($tog, $dRLA &"|"& $dACT &"|"& $dAC2 &"|"& $dRET &"|"& $dDEL &"|"& $dADD &"|"& $dMQC)
      EndIf
   Case "ADD-INPUT"
      _ToggleCTRL($tog, $nCOMi &"|"& $nCO2i &"|"& $nNAMi &"|"& $nNAMb &"|"& $nEMAi &"|"& $nPREc &"|"& $nACCc &"|"& $nICUi &"|"& $nADDi &"|"& $nNOTi)
      _ToggleCTRL($tog, $nAPLb &"|"& $nALOb &"|"& $nAAFb &"|"& $nPLAl &"|"& $nLOCl &"|"& $nAFFl &"|"& $nRPLb &"|"& $nRLAb &"|"& $nADDb &"|"& $nCLEb)
   Case "PLANS"
      If $tog <> $GUI_SHOW Or Not $changing Then
         If $act Then _ToggleCTRL($tog, $dMOD &"|"& $dLOG &"|"& $dUPD &"|"& $dDEL)
         _ToggleCTRL($tog, $dINP &"|"& $dAPL &"|"& $dALO &"|"& $dAAF &"|"& $dPLL &"|"& $dLOL &"|"& $dAFL &"|"& $dSAV &"|"& $dCAN &"|"& $dRPL &"|"& $dRLA)
      EndIf
   Case "CHANGE"
      _ToggleCTRL($tog, $dITL &"|"& $dITC &"|"& $dAML &"|"& $dAMC &"|"& $dSCB &"|"& $dCCB &"|"& $dEAB)
      If $tog = $GUI_SHOW Then
         GUICtrlSetState($dRET, $GUI_DISABLE)
      Else
         GUICtrlSetState($dRET, $GUI_ENABLE)
      EndIf
   Case "NEW"
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 465, 475)
      _ToggleCTRL($tog, $TAB &"|"& $nCOMl &"|"& $nCOMi &"|"& $nCO2l &"|"& $nCO2i &"|"& $nNAMl &"|"& $nNAMi &"|"& $nEMAl &"|"& $nEMAi &"|"& $nPREl &"|"& $nPREc &"|"& $nACCl &"|"& $nACCc &"|"& $nPAEb &"|"& $nICUl &"|"& $nICUi &"|"& $nADDi &"|"& $nAPLb &"|"& $nALOb &"|"& $nAAFb &"|"& $nPLAl &"|"& $nLOCl &"|"& $nAFFl &"|"& $nRPLb &"|"& $nNOTl &"|"& $nNOTi &"|"& $nADDb &"|"& $nCLEb &"|"& $nLAST)
   Case "ACCESS"
      If $tog = $GUI_SHOW Then WinMove($GUI, "", Default, Default, 465, 330)
      _ToggleCTRL($tog, $ACCd1 &"|"& $ACCd2 &"|"& $TYPla &"|"& $TYPli &"|"& $TYPad &"|"& $TYPde &"|"& $TYPmu &"|"& $TYPmd &"|"& $ACCla &"|"& $ACCli &"|"& $ACCde &"|"& $ACCmu &"|"& $ACCmd &"|"& $ACCdl &"|"& $ACCdi &"|"& $ACCal &"|"& $ACCai &"|"& $ACCaa &"|"& $taRET)
   Case "TPi"
      _ToggleCTRL($tog, $tpOPEb &"|"& $tpCREr &"|"& $tpLINr &"|"& $tpCO2i &"|"& $tpEMAi &"|"& $tpEMAl &"|"& $tpCREb &"|"& $tpCOPb &"|"& $tpCOPi &"|"& $tpCTOi &"|"& $tpCGOb &"|"& $tpUFLi &"|"& $tpUFLb &"|"& $tpICUi &"|"& $tpPLAi &"|"& $tpLINb &"|"& $tpDLIb &"|"& $tpUFMi &"|"& $tpSCAl &"|"& $tpCFAr &"|"& $tpCBAr)
   Case "TPC"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPL", $GUI_HIDE)
         _GuiMod("TPH", $GUI_HIDE)
         _GuiMod("TPD", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpCO2l &"|"& $tpCO2i &"|"& $tpCOPl &"|"& $tpCOPi &"|"& $tpCREb &"|"& $tpCOPb &"|"& $tpSEPl &"|"& $tpCTOl &"|"& $tpCTOi &"|"& $tpCGOb &"|"& $tpLAST &"|"& $tpIDNl &"|"& $tpIDNi &"|"& $tpIDNn &"|"& $tpSCAl &"|"& $tpEMAi &"|"& $tpEMAl &"|"& $tpCFAr &"|"& $tpCBAr)
   Case "TPL"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPC", $GUI_HIDE)
         _GuiMod("TPH", $GUI_HIDE)
         _GuiMod("TPD", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpUFLl &"|"& $tpUFLi &"|"& $tpUFLb &"|"& $tpICUl &"|"& $tpICUi &"|"& $tpPLAl &"|"& $tpPLAi &"|"& $tpLINb &"|"& $tpDLIb &"|"& $tpUFMl &"|"& $tpUFMi &"|"& $tpIMAb &"|"& $tpIMRb)
   Case "TPH"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPC", $GUI_HIDE)
         _GuiMod("TPL", $GUI_HIDE)
         _GuiMod("TPD", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpUFLl &"|"& $tpUFLi &"|"& $tpUFLb &"|"& $tpUFMl &"|"& $tpUFMi &"|"& $tpCOCl &"|"& $tpCOCi &"|"& $tpCOGb)
   Case "TPD"
      If $tog = $GUI_SHOW Then
         _GuiMod("TPC", $GUI_HIDE)
         _GuiMod("TPL", $GUI_HIDE)
         _GuiMod("TPH", $GUI_HIDE)
      EndIf
      _ToggleCTRL($tog, $tpDIDl &"|"& $tpDIDi &"|"& $tpDIDb)
   EndSwitch
EndFunc

Func _ToggleCTRL($tog, $ctrl)
   Local $arr = StringSplit($ctrl, "|")
   For $i = 1 To Ubount($arr)-1
      GUICtrlSetState($arr[$i], $tog)
   Next
EndFunc

 

  • 1 month later...
Posted (edited)

Today I realized a good way to further reduce the needed code to process controls like this.

 

Local $mNamLa = GUICtrlCreateLabel("Name:", 8, 9)
Local $mNamIn = GUICtrlCreateInput("", 50, 5, 220, 20)
Local $mTypLa = GUICtrlCreateLabel("Type:", 12, 39)
Local $mTypAD = GUICtrlCreateRadio("Active Directory", 50, 35, 100)
Local $mTypIn = GUICtrlCreateRadio("Input", 160, 35, 50)
Local $mTypSe = GUICtrlCreateRadio("Select", 220, 35, 50)
Local $mADaLa = GUICtrlCreateLabel("Attribute Name:", 10, 71, 79)
Local $mADaIn = GUICtrlCreateInput("", 90, 67, 180)
Local $mSelLa = GUICtrlCreateLabel("Values:", 30, 71, 44)
Local $mSelCo = GUICtrlCreateCombo("", 75, 67, 120, 25, $CBS_DROPDOWNLIST)
Local $mSelAd = GUICtrlCreateButton("", 205, 64, 26, 26, $BS_ICON)
GUICtrlSetImage(-1, @ScriptDir & "\icons\add.ico")
Local $mSelRe = GUICtrlCreateButton("", 240, 64, 26, 26, $BS_ICON)
GUICtrlSetImage(-1, @ScriptDir & "\icons\delete.ico")
Local $mActSa = GUICtrlCreateButton("Save", 30, 105, 100, 25)
Local $mActCa = GUICtrlCreateButton("Cancel", 150, 105, 100, 25)

;Using the function below, you can use the following to toggle gui screens.
_ToggleCTRL($GUI_HIDE, "mNamLa", "mActCa")
;Further, it leaves the option to toggle a list of particular ones that are not in order if you need to
_ToggleCTRL($GUI_HIDE, $mTypLa, $mTypAD, $mTypIn, $mTypSe, $mSelLa, $mSelCo)

Func _ToggleCTRL($tog, $c0 = 0, $c1 = 0, $c2 = 0, $c3 = 0, $c4 = 0, $c5 = 0, $c6 = 0, $c7 = 0, $c8 = 0, $c9 = 0, $c10 = 0, $c11 = 0, $c12 = 0)
   If IsString($c0) Then
      For $i = Eval($c0) To Eval($c1)
         GUICtrlSetState($i, $tog)
      Next
   Else
      For $i = 0 To @NumParams
         GUICtrlSetState(Eval("c" & $i), $tog)
      Next
   EndIf
EndFunc

 

Edited by Aphotic

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
×
×
  • Create New...