Jump to content

InputBoxes


Recommended Posts

I hate designing GUIs. It takes too long and I can't seem to reuse pieces well. Also, I like to have my program run quietly and only ask for things as needed. What bothers me more is when I need to ask a question slightly more complicated than Yes/No or input some text. So I started working on a couple of variations on the InputBox.

If you have any additions to this, I'd love to steal include them in this UDF! (Contributions welcome!)

Spoiler

This is the beta version. I'm in a rush right now, so I'll hopefully add more comments and clean things up in the future.

NOTE: Parameters are NOT set in stone and WILL likely change order. (I know, it hurts, cause it breaks my scripts too)

#include-once

#include <GUIConstantsEx.au3>
#include <GUIListView.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputBox_ComboBox
; Description ...: Creates an input box that allows the user to select an option.
; Syntax ........: _InputBox_ComboBox($sTitle, $sPrompt, $vOptions[, $vSelected = Default[, $iLeft = -1[, $iTop = -1[, $iStyle = -1[, $iExStyle = -1[, $hParent = 0]]]]]])
; Parameters ....: $sTitle              - a string value.
;                  $sPrompt             - a string value.
;                  $vOptions            - an array of choices or a GUIDataSeperatorChar seperated string of options.
;                  $vSelected           - [optional] a variant value. Default is Default.
;                  $iLeft               - [optional] an integer value. Default is -1.
;                  $iTop                - [optional] an integer value. Default is -1.
;                  $iStyle              - [optional] an integer value. Default is -1.
;                  $iExStyle            - [optional] an integer value. Default is -1.
;                  $hParent             - [optional] a handle value. Default is 0.
; Return values .: Success - the text of the selected item
;                  Failure - False and sets @error:
;                  |1 - GUI was closed
; Author ........: Seadoggie01
; Modified ......: October 15, 2020
; Remarks .......: Won't return until an option is selected or the GUI is closed
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _InputBox_ComboBox($sTitle, $sPrompt, $vOptions, $vSelected = Default, $iLeft = Default, $iTop = Default, $iStyle = Default, $iExStyle = Default, $hParent = Default)

    Local Const $sDelimiter = Opt("GUIDataSeparatorChar")

    If $iLeft = Default Then $iLeft = -1
    If $iTop = Default Then $iTop = -1
    If $iStyle = Default Then $iStyle = -1
    If $iExStyle = Default Then $iExStyle = -1
    If $hParent = Default Then $hParent = 0

    Local $hGUI = GUICreate($sTitle, 250, 100, $iLeft, $iTop, $iStyle, $iExStyle, $hParent)
    GUICtrlCreateLabel($sPrompt, 5, 10, 240, 20)
    Local $hCombo = GUICtrlCreateCombo("", 5, 30, 240, 20)
    Local $hButton = GUICtrlCreateButton("OK", 100, 60, 50, 20)

    Local $sOptions
    If IsArray($vOptions) Then
        For $i=0 To UBound($vOptions) - 1
            $sOptions &= $vOptions[$i] & $sDelimiter
        Next
        ; Remove the last pipe
        $sOptions = StringReplace($sOptions, $sDelimiter, "", -1)
    Else
        $sOptions = $vOptions
    EndIf

    ; If there is a pre-selected item
    If $vSelected <> Default Then
        ; If the selected item is a number
        If IsNumber($vSelected) Then
            ; Split the options
            $vOptions = StringSplit($sOptions, $sDelimiter, 3)
            ; If there are enough items, then select the requested item
            If UBound($vOptions) > $vSelected Then GUICtrlSetData($hCombo, $sOptions, $vOptions[$vSelected])
        Else
            GUICtrlSetData($hCombo, $sOptions, $vSelected)
        EndIf
    Else
        GUICtrlSetData($hCombo, $sOptions)
    EndIf

    GUISetState(@SW_SHOW, $hGUI)

    Local $aAccelKeys[1][2] = [["{ENTER}", $hButton]]
    GUISetAccelerators($aAccelKeys)

    While True
        Switch GUIGetMsg()
            Case $hButton
                $vSelected = GUICtrlRead($hCombo)
                If $vSelected <> "" Then ExitLoop
            Case $GUI_EVENT_CLOSE
                $vSelected = ""
                ExitLoop
        EndSwitch
    WEnd

    GUISetState(@SW_HIDE)
    GUIDelete($hGUI)

    If $vSelected = "" Then
        Return SetError(1, 0, False)
    Else
        Return $vSelected
    EndIf

EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputBox_Date
; Description ...: Asks the user to select a date from a GUI
; Syntax ........: _InputBox_Date($sTitle, $sPrompt, $sDefaultDate[, $iLeft = -1[, $iTop = -1[, $iStyle = -1[, $iExStyle = -1[, $hParent = 0]]]]])
; Parameters ....: $sTitle              - a string value.
;                  $sPrompt             - a string value.
;                  $sDefaultDate        - a string value.
;                  $iLeft               - [optional] an integer value. Default is -1.
;                  $iTop                - [optional] an integer value. Default is -1.
;                  $iStyle              - [optional] an integer value. Default is -1.
;                  $iExStyle            - [optional] an integer value. Default is -1.
;                  $hParent             - [optional] a handle value. Default is 0.
; Return values .: Success - The selected date (returned from GUICtrlRead; regional formatting)
;                  Failure - False and sets @error:
;                  |1 - $sDefaultDate isn't in yyyy/mm/dd format
;                  |2 - GUI was closed
; Author ........: Seadoggie01
; Modified ......: January 7, 2020
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _InputBox_Date($sTitle, $sPrompt, $sDefaultDate, $iLeft = -1, $iTop = -1, $iStyle = -1, $iExStyle = -1, $hParent = 0)

    If Not StringRegExp($sDefaultDate, "\d{4}\/\d{2}\/\d{2}") Then Return SetError(1, 0, False)

    Local $hGUI = GUICreate($sTitle, 250, 100, $iLeft, $iTop, $iStyle, $iExStyle, $hParent)
    GUICtrlCreateLabel($sPrompt, 10, 10, 230, 20)
    Local $hDate = GUICtrlCreateDate($sDefaultDate, 10, 40, 230, 20)
    Local $hOkBttn = GUICtrlCreateButton("OK", 100, 70, 50, 20)

    GUISetState(@SW_SHOW)

    GUISwitch($hGUI)

    Local $bQuit = False

    While True

        Switch GUIGetMsg()
            Case $hOkBttn
                ExitLoop
            Case $GUI_EVENT_CLOSE
                $bQuit = True
                ExitLoop
        EndSwitch
    WEnd

    Local $vRet = GUICtrlRead($hDate)

    GUISetState(@SW_HIDE, $hGUI)
    GUIDelete($hGUI)

    If IsHWnd($hParent) Then GUISwitch($hParent)

    If $bQuit Then Return SetError(2, 0, False)

    Return $vRet

EndFunc

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputBox_ListView
; Description ...: Asks the user to select some items from a list, multiple selections are allowed
; Syntax ........: _InputBox_ListView($sTitle, $sPrompt, $sColumnName, $vOptions[, $iLeft = Default[, $iTop = Default[,
;                  $iStyle = Default[, $iExStyle = Default[, $hParent = Default]]]]])
; Parameters ....: $sTitle              - a string value.
;                  $sPrompt             - a string value.
;                  $sColumnName         - a string value.
;                  $vOptions            - an array or a GUIDataSeparatorChar delimited string.
;                  $iLeft               - [optional] an integer value. Default is Default.
;                  $iTop                - [optional] an integer value. Default is Default.
;                  $iStyle              - [optional] an integer value. Default is Default.
;                  $iExStyle            - [optional] an integer value. Default is Default.
;                  $hParent             - [optional] a handle value. Default is Default.
; Return values .: Success - a 0-based 1D array of selected items
;                  Failure - False and sets @error:
;                  |1 - GUI was closed
;                  |2 - No items were selected
; Author ........: Seadoggie01
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func _InputBox_ListView($sTitle, $sPrompt, $sColumnName, $vOptions, $iLeft = Default, $iTop = Default, $iStyle = Default, $iExStyle = Default, $hParent = Default)

    #forceref $sTitle, $sPrompt, $vOptions, $iLeft, $iTop, $iStyle, $iExStyle, $hParent

    If IsKeyword($sTitle) Then $sTitle = "Select Options"
    If IsKeyword($sPrompt) Then $sPrompt = "Select some or all options"
    If IsKeyword($iLeft) Then $iLeft = -1
    If IsKeyword($iTop) Then $iTop = -1
    If IsKeyword($iStyle) Then $iStyle = -1
    If IsKeyword($iExStyle) Then $iExStyle = -1
    If IsKeyword($hParent) Then $hParent = 0

    #Region ### START Koda GUI section ###
    Local $hGUI = GUICreate($sTitle, 405, 293, -1, -1)
    Local $idListBox = GUICtrlCreateListView("", 15, 35, 371, 214, $LVS_SHOWSELALWAYS)
    Local $idTitleLabel = GUICtrlCreateLabel("", 15, 10, 368, 20)
    GUICtrlSetFont(-1, 10, 400, 0, "MS Sans Serif")
    Local $idOkBttn = GUICtrlCreateButton("OK", 165, 260, 75, 25)

    #EndRegion ### END Koda GUI section ###

    GUICtrlSetData($idTitleLabel, $sPrompt)

    GUISwitch($hGUI)

    Local $aItems
    If IsArray($vOptions) Then
        $aItems = $vOptions
    Else
        $aItems = StringSplit($vOptions, Opt("GUIDataSeparatorChar"), 3)
    EndIf

    _GUICtrlListView_AddColumn($idListBox, $sColumnName, 350)

    For $i=0 To UBound($aItems) - 1
        _GUICtrlListView_AddItem($idListBox, $aItems[$i])
    Next

    _GUICtrlListView_SetItemSelected($idListBox, -1, False)

    GUISetState(@SW_SHOW)

    Local $bExit = False

    While True
        Switch GUIGetMsg()
            Case $idOkBttn
                ExitLoop
            Case $GUI_EVENT_CLOSE
                $bExit = True
                ExitLoop
        EndSwitch
    WEnd

    If Not $bExit Then
        Local $sSelected = _GUICtrlListView_GetSelectedIndices($idListBox, False)
        If $sSelected = "" Then Return SetError(2, 0, False)

        Local $aSelected = StringSplit($sSelected, "|", 3)

        For $i=0 To UBound($aSelected) - 1
            $aSelected[$i] = _GUICtrlListView_GetItem($idListBox, Number($aSelected[$i]))[3]
        Next
    EndIf

    GUISetState(@SW_HIDE)

    If $bExit Then Return SetError(1, 0, False)

    Return $aSelected

EndFunc

If @ScriptName = "InputBox.au3" Then __InputBox_Testing()

Func __InputBox_Testing()

    Local $vRet

    $vRet = _InputBox_ComboBox("Favorite Color", "What is your favorite color?", "Red|Yellow|Green|Purple|Nope", "Nope")
    If @error Then ConsoleWrite("! _InputBox_ComboBox - Error: " & @error & " Extended: " & @extended & @CRLF)
    ConsoleWrite("_InputBox_ComboBox Returned: " & $vRet & @CRLF)

    $vRet = _InputBox_Date("Birthday!", "When is your birthday?", "2021/01/01")
    If @error Then ConsoleWrite("! _InputBox_Date - Error: " & @error & " Extended: " & @extended & @CRLF)
    ConsoleWrite("_InputBox_Date Returned: " & $vRet & @CRLF)

    $vRet = _InputBox_ListView("Favorite Colors", "What are your favorite colors?", "Color List", "Red|Yellow|Green|Purple|None")
    If @error Then
        ConsoleWrite("! _InputBox_ListView - Error: " & @error & " Extended: " & @extended & @CRLF)
    Else
        _ArrayDisplay($vRet, "_InputBox_ListView")
    EndIf

EndFunc

 

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

nice idea, thanks for sharing.

p.s.
for those interested in the subject, here (https://www.autoitscript.com/forum/topic/183288-extended-input-box-multiple-inputsis another interesting udf by @Jefrey that allows you to build multiple inputbox on the fly (a sort of small "form" with multiple inputs)

 

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...