Jump to content

problem with optional parameters in a function

Recommended Posts

Func _DisableInput($n1, $n2, $n3, $n4)
    GUICtrlSetState($n1, $GUI_DISABLE)
    GUICtrlSetState($n2, $GUI_DISABLE)
    GUICtrlSetState($n3, $GUI_DISABLE)
    GUICtrlSetState($n4, $GUI_DISABLE)
EndFunc   ;==>_DisableInput

_DisableInput($aInput[0][0], $aInput[1][0], $aInput[2][0], $aInput[3][0])

The above works if I want to disable all 4 inputs. But if I only want to disable 3 and thus remove the $aInput[3][0] from the function call I get an error saying incorrect number of parameters.

Basically I want to be able to disable X number of inputs up to a maximum of 4.

Edited by kor
Link to post
Share on other sites

To make a parameter optional you have to set a default value in the parameter list. In this case I used the keyword Default but it could be any other value like 0 or "".

Func _DisableInput($n1, $n2 = Default, $n3 = Default, $n4 = Default)
    GUICtrlSetState($n1, $GUI_DISABLE)
    If $n2 <> Default Then GUICtrlSetState($n2, $GUI_DISABLE)
    If $n3 <> Default Then GUICtrlSetState($n3, $GUI_DISABLE)
    If $n4 <> Default Then GUICtrlSetState($n4, $GUI_DISABLE)
EndFunc   ;==>_DisableInput
Edited by water

My UDFs and Tutorials:


Active Directory (NEW 2021-06-05 - Version - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version - Download - General Help & Support - Example Scripts
OutlookEX (NEW 2021-06-14 - Version - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version - Download
Outlook Tools (2019-07-22 - Version - Download - General Help & Support - Wiki
PowerPoint (NEW 2021-08-31 - Version - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2019-12-03 - Version - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

ADO - Wiki
WebDriver - Wiki


Link to post
Share on other sites

Like this, now only the first parameter is mandatory as why would you use this without using at least one parameter.

Func _DisableInput($n1, $n2 = -1, $n3 = -1, $n4 = -1)
    Local $aArray[5] = [4, $n1, $n2, $n3, $n4]
    For $i = 1 To $aArray[0]
        If $aArray[$i] = -1 Then
        GUICtrlSetState($aArray[$i], $GUI_DISABLE)
EndFunc   ;==>_DisableInput

Plus I love using loops, because you can adapt this to 15 parameters without adding more lines.

Edited by guinness

UDF List:

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_ArrayFilter/_ArrayReduce_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 22/04/2018

Link to post
Share on other sites
  • Moderators


Use arrays. Put all the ControlIDs into one array and then pass another array as a parameter to the function. tehn you can have as many as you want: :oops:

#include <guiconstantsex.au3>

Global $aInput[4], $aState[4]

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

For $i = 0 To 3
    $aInput[$i] = GUICtrlCreateInput("", 10, 10 + (30 * $i), 200, 20)

$cButton_1 = GUICtrlCreateButton("Disable #3", 10, 200, 100, 30)
$cButton_2 = GUICtrlCreateButton("Disable #1 && #2", 10, 250, 100, 30)
$cButton_3 = GUICtrlCreateButton("Disable #2 && #4", 10, 300, 100, 30)
$cButton_4 = GUICtrlCreateButton("Enable all", 10, 350, 100, 30)


While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
        Case $cButton_1
            Global $aState [4] = [1, 1, 0, 1]
        Case $cButton_2
            Global $aState [4] = [0, 0, 1, 1]
        Case $cButton_3
            Global $aState [4] = [1, 0, 1, 0]
        Case $cButton_4
            Global $aState [4] = [1, 1, 1, 1]

Func _Set_Input_State()

    For $i = 0 To UBound($aState) - 1
        If $aState[$i] = 1 Then
            GUICtrlSetState($aInput[$i], $GUI_ENABLE)
            GUICtrlSetState($aInput[$i], $GUI_DISABLE)



Edited by Melba23
Wrong button, too soon!

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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:


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


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
  • Recently Browsing   0 members

    No registered users viewing this page.

  • Create New...