orbs

InputImpose UDF - Impose valid characters in an Input control

7 posts in this topic

#1 ·  Posted (edited)

Inspired by $ES_NUMBER, but not limited to numbers.
 
May be useful for other types of controls (Combo, Edit, etc.), but not tested.
 
What It Does
 
When a control is imposed for valid characters, it removes the offending characters, displays a balloon tip (similar to $ES_NUMBER), plays a beep, and waits for user activity (key press or mouse move) before the tooltip is cleared. Beep and tooltip parameters are controlled, and can even be completely disabled if the calling script prefers to handle the user notification in custom.
 
A core concept in the UDF is the conditions array. this array contains string and/or numeric conditions (characters allowed/disallowed, etc.) and notification behaviour for tooltip and beep. The conditions array is managed by _InputImpose_Create_InputImpose_Duplicate, and _InputImpose_Update. You can build a conditions array to be used for multiple input controls, or you can simply call _InputImpose with ad-hoc conditions, without creating an array. all techniques are demonstrated in the example script.
 
How To Use
 
after you create the Input control $gInput in the GUI $hGUI, create a conditions array. for example:
Global $aCondition = _InputImpose_Create($__INIM_ALLOW_ANYWHERE, '0..9.-+*/^()') ; math expression

you can specify any or all available conditions in the same array creation (see the UDF constants section). you can update the array later, duplicate it for other controls and modify the conditions, etc.

to apply, put this line in the main loop of your script:

_InputImpose($hGUI, $gInput, $aCondition)
Parameters
; Parameters ....: $hGUI       - Handle to the GUI in which the input control is located.
;                  $gInput     - Control ID of the Input control to impose.
;                  $aCondition - Conditions array.

if the conditions array defines no tooltip or beep, then the only indication the user has that an invalid character was removed is that... well... it was removed. This is useful if you want to handle the notification yourself. In this case, use the function return value to determine if a notification is due, for example:

If _InputImpose(...) Then MsgBox(...)
Example
 

The attached example shows a GUI with 6 Input controls, each with its own conditions array (content and build technique) for valid characters, custom tooltip and beep behaviour.

Enjoy!

the UDF (v0.9):

#include-Once

; #INDEX# =======================================================================================================================
; Title .........: InputImpose
; AutoIt Version : 3.3.14.2
; UDF Version ...: 0.9
; Status ........: Development
; Language ......: English
; Description ...: Impose valid characters in an Input control - inspired by $ES_NUMBER, but not limited to numbers.
;                  May be useful for other types of controls (Combo, Edit, etc.), but not tested.
;                  When a control is imposed for valid characters, it removes the offending characters, displays a balloon tip
;                  (similar to $ES_NUMBER), plays a beep, and waits for user activity (key press or mouse move) before the
;                  tooltip is cleared. Beep and tooltip parameters are controlled, and can even be completely disabled if the
;                  calling script prefers to handle the user notification in custom.
;                  NOTE: Default values for tooltip and beep:
;                    - tooltip text: "Press any key or move mouse to continue"
;                    - tooltip title: "Unacceptabe Character" (same as $ES_NUMBER for English systems)
;                    - beep frequency: 430
;                    - beep duration: 300
; Dll ...........: kernel32.dll, user32.dll
; Author(s) .....: orbs
; Resources .....:
; ===============================================================================================================================

; #CONSTANTS# ===================================================================================================================
Global Enum _
        $__INIM_ALLOW_ANYWHERE, $__INIM_ALLOW_FIRST, $__INIM_ALLOW_LAST, $__INIM_ALLOW_ONCE, _ ; allow conditions
        $__INIM_DISALLOW_ANYWHERE, $__INIM_DISALLOW_FIRST, $__INIM_DISALLOW_LAST, _            ; disallow conditions
        $__INIM_ENFORCE_NUMLOWERLIMIT, $__INIM_ENFORCE_NUMUPPERLIMIT, _                        ; enforce numerical coditions
        $__INIM_TOOLTIP_TEXT, $__INIM_TOOLTIP_TITLE, $__INIM_TOOLTIP_TIMEOUT, _                ; tooltip behaviour
        $__INIM_BEEP_FREQUENCY, $__INIM_BEEP_DURATION, _                                       ; beep behaviour
        $__INIM_CONDITIONCOUNT ; conditons array size
; ===============================================================================================================================

; #CURRENT# =====================================================================================================================
;_InputImpose
;_InputImpose_Create
;_InputImpose_Duplicate
;_InputImpose_Update
; ===============================================================================================================================

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputImpose
; Description ...: Impose valid characters in an Input control.
; Syntax ........: _InputImpose($hGUI, $gInput, $aCondition[, $iElement0 = -1, $xValue0 = -1[ ... [, $iElement11 = -1, $xValue11 = -1]]])
; Parameters ....: $hGUI                   - Handle to the GUI in which the input control is located.
;                  $gInput                 - Control ID of the Input control to impose.
;                  $aCondition             - Conditions array as returned by _InputImpose_Create() or _InputImpose_Duplicate().
;                  $iElement,$xValue pairs - (up to 12 pairs) Conditions that will be updated into the given conditions array.
; Return values .: Returns the given Control ID ($gInput) if impose was needed, returns 0 if impose was not needed.
; Author ........: orbs
; Modified ......:
; Remarks .......: If $aCondition is somehow invalid, it is independently created as a local variable.
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _InputImpose($hGUI, $gInput, $aCondition, $iElement0 = -1, $xValue0 = -1, $iElement1 = -1, $xValue1 = -1, $iElement2 = -1, $xValue2 = -1, $iElement3 = -1, $xValue3 = -1, $iElement4 = -1, $xValue4 = -1, $iElement5 = -1, $xValue5 = -1, $iElement6 = -1, $xValue6 = -1, $iElement7 = -1, $xValue7 = -1, $iElement8 = -1, $xValue8 = -1, $iElement9 = -1, $xValue9 = -1, $iElement10 = -1, $xValue10 = -1, $iElement11 = -1, $xValue11 = -1)
    ; validate conditions array
    If Not IsArray($aCondition) Or UBound($aCondition, 0) > 1 Or UBound($aCondition) <> $__INIM_CONDITIONCOUNT Then $aCondition = _InputImpose_Create()
    _InputImpose_Update($aCondition, $iElement0, $xValue0, $iElement1, $xValue1, $iElement2, $xValue2)
    _InputImpose_Update($aCondition, $iElement3, $xValue3, $iElement4, $xValue4, $iElement5, $xValue5)
    _InputImpose_Update($aCondition, $iElement6, $xValue6, $iElement7, $xValue7, $iElement8, $xValue8)
    _InputImpose_Update($aCondition, $iElement9, $xValue9, $iElement10, $xValue10, $iElement11, $xValue11)
    ; if $hGUI not in focus then nothing to do => return OK
    If Not WinActive($hGUI) Then Return 0
    ; if $gInput not in focus then nothing to do => return OK
    If Not __InputImpose_GUICtrlHasFocus($hGUI, $gInput) Then Return 0
    ; if no text to check then nothing to do => return OK
    Local $sInput = GUICtrlRead($gInput)
    If $sInput = '' Then Return 0
    ; main
    Local $sChar
    Local $sInputNew = ''
    Local $sOccurredOnce = ''
    Local $bError = False
    For $i = 1 To StringLen($sInput)
        $sChar = StringMid($sInput, $i, 1)
        If ($aCondition[$__INIM_ALLOW_ANYWHERE] <> '' And Not StringInStr($aCondition[$__INIM_ALLOW_ANYWHERE] & $aCondition[$__INIM_ALLOW_FIRST] & $aCondition[$__INIM_ALLOW_LAST] & $aCondition[$__INIM_ALLOW_ONCE], $sChar)) Or _
                ($aCondition[$__INIM_ALLOW_FIRST] <> '' And $i > 1 And StringInStr($aCondition[$__INIM_ALLOW_FIRST], $sChar)) Or _
                ($aCondition[$__INIM_ALLOW_LAST] <> '' And $i < StringLen($sInput) And StringInStr($aCondition[$__INIM_ALLOW_LAST], $sChar)) Or _
                (StringInStr($sOccurredOnce, $sChar)) Or _
                ($aCondition[$__INIM_DISALLOW_ANYWHERE] <> '' And StringInStr($aCondition[$__INIM_DISALLOW_ANYWHERE], $sChar)) Or _
                ($aCondition[$__INIM_DISALLOW_FIRST] <> '' And $i = 1 And StringInStr($aCondition[$__INIM_DISALLOW_FIRST], $sChar)) Or _
                ($aCondition[$__INIM_DISALLOW_LAST] <> '' And $i = StringLen($sInput) And StringInStr($aCondition[$__INIM_DISALLOW_LAST], $sChar)) Then
            $bError = True
        Else
            $sInputNew &= $sChar
            If StringInStr($aCondition[$__INIM_ALLOW_ONCE], $sChar) Then $sOccurredOnce &= $sChar
        EndIf
    Next
    If ($aCondition[$__INIM_ENFORCE_NUMLOWERLIMIT] <> Null And Number($sInput) < $aCondition[$__INIM_ENFORCE_NUMLOWERLIMIT]) Or _
            ($aCondition[$__INIM_ENFORCE_NUMUPPERLIMIT] <> Null And Number($sInput) > $aCondition[$__INIM_ENFORCE_NUMUPPERLIMIT]) Then
        $bError = True
        $sInputNew = ''
    EndIf
    Local $aPos
    If $bError Then
        $aPos = __InputImpose_CaretPos()
        GUICtrlSetData($gInput, $sInputNew)
        ToolTip($aCondition[$__INIM_TOOLTIP_TEXT], $aPos[0], $aPos[1] + 15, $aCondition[$__INIM_TOOLTIP_TITLE], 3, 1)
        Beep($aCondition[$__INIM_BEEP_FREQUENCY], $aCondition[$__INIM_BEEP_DURATION])
        __InputImpose_WaitForUser($aCondition[$__INIM_TOOLTIP_TIMEOUT])
        ToolTip('')
        Return $gInput
    EndIf
    Return 0
EndFunc   ;==>_InputImpose

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputImpose_Create
; Description ...: Creates a conditions array. Values are set to default - see Remarks.
; Syntax ........: _InputImpose_Create([$iElement0 = -1, $xValue0 = -1[ ... [, $iElement11 = -1, $xValue11 = -1]]])
; Parameters ....: $iElement,$xValue pairs (up to 14 pairs) - [optional] Conditions that will be updated into the array.
; Return values .: Returns a zero-based, 14-elements array.
; Author ........: orbs
; Modified ......:
; Remarks .......: All strings values are blank by default.
;                  ToolTip values are set as follows:
;                    text = "Press any key or move mouse to continue"
;                    title = "Unacceptabe Character"
;                    timeout = 0 (no timeout)
;                  Beep values are set as follows:
;                    frequency = 430 [Hz]
;                    duration = 300 [ms]
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _InputImpose_Create($iElement0 = -1, $xValue0 = -1, $iElement1 = -1, $xValue1 = -1, $iElement2 = -1, $xValue2 = -1, $iElement3 = -1, $xValue3 = -1, $iElement4 = -1, $xValue4 = -1, $iElement5 = -1, $xValue5 = -1, $iElement6 = -1, $xValue6 = -1, $iElement7 = -1, $xValue7 = -1, $iElement8 = -1, $xValue8 = -1, $iElement9 = -1, $xValue9 = -1, $iElement10 = -1, $xValue10 = -1, $iElement11 = -1, $xValue11 = -1, $iElement12 = -1, $xValue12 = -1, $iElement13 = -1, $xValue13 = -1)
    Local $aCondition[$__INIM_CONDITIONCOUNT] = ['', '', '', '', '', '', '', Null, Null, 'Press any key or move mouse to continue', 'Unacceptabe Character', 0, 430, 300]
    _InputImpose_Update($aCondition, $iElement0, $xValue0, $iElement1, $xValue1, $iElement2, $xValue2)
    _InputImpose_Update($aCondition, $iElement3, $xValue3, $iElement4, $xValue4, $iElement5, $xValue5)
    _InputImpose_Update($aCondition, $iElement6, $xValue6, $iElement7, $xValue7, $iElement8, $xValue8)
    _InputImpose_Update($aCondition, $iElement9, $xValue9, $iElement10, $xValue10, $iElement11, $xValue11)
    _InputImpose_Update($aCondition, $iElement12, $xValue12, $iElement13, $xValue13)
    Return $aCondition
EndFunc   ;==>_InputImpose_Create

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputImpose_Duplicate
; Description ...: Duplicates a conditions array.
; Syntax ........: _InputImpose_Duplicate($aCondition[, $iElement0 = -1, $xValue0 = -1[ ... [, $iElement13 = -1, $xValue13 = -1]]])
; Parameters ....: $aCondition             - Conditions array to be duplicated.
;                  $iElement,$xValue pairs - (up to 14 pairs) Conditions that will be updated into the duplicated array.
; Return values .: Success - Returns a conditions array identical to the given conditions array, updated as ordered.
;                  Failure - Returns the conditions array and sets @error to 1. A partial update may have been performed.
; Author ........: orbs
; Modified ......:
; Remarks .......: There is no validation of array size or contents!
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _InputImpose_Duplicate($aCondition, $iElement0 = -1, $xValue0 = -1, $iElement1 = -1, $xValue1 = -1, $iElement2 = -1, $xValue2 = -1, $iElement3 = -1, $xValue3 = -1, $iElement4 = -1, $xValue4 = -1, $iElement5 = -1, $xValue5 = -1, $iElement6 = -1, $xValue6 = -1, $iElement7 = -1, $xValue7 = -1, $iElement8 = -1, $xValue8 = -1, $iElement9 = -1, $xValue9 = -1, $iElement10 = -1, $xValue10 = -1, $iElement11 = -1, $xValue11 = -1, $iElement12 = -1, $xValue12 = -1, $iElement13 = -1, $xValue13 = -1)
    If Not _InputImpose_Update($aCondition, $iElement0, $xValue0, $iElement1, $xValue1, $iElement2, $xValue2) Then Return SetError(1, 0, $aCondition)
    If Not _InputImpose_Update($aCondition, $iElement3, $xValue3, $iElement4, $xValue4, $iElement5, $xValue5) Then Return SetError(1, 0, $aCondition)
    If Not _InputImpose_Update($aCondition, $iElement6, $xValue6, $iElement7, $xValue7, $iElement8, $xValue8) Then Return SetError(1, 0, $aCondition)
    If Not _InputImpose_Update($aCondition, $iElement9, $xValue9, $iElement10, $xValue10, $iElement11, $xValue11) Then Return SetError(1, 0, $aCondition)
    If Not _InputImpose_Update($aCondition, $iElement12, $xValue12, $iElement13, $xValue13) Then Return SetError(1, 0, $aCondition)
    Return $aCondition
EndFunc   ;==>_InputImpose_Duplicate

; #FUNCTION# ====================================================================================================================
; Name ..........: _InputImpose_Update
; Description ...: Updates an element of a given conditions array.
; Syntax ........: _InputImpose_Update(Byref $aCondition[, $iElement1, $xValue1[, $iElement2 = -1, $xValue2 = -1[, $iElement3 = -1, $xValue3 = -1)]]])
; Parameters ....: $aCondition             - [in/out] Conditions array.
;                  $iElement,$xValue pairs - (up to 3 pairs) Values that will be updated for their respective elements.
; Return values .: Success - Returns 1
;                  Failure - Returns 0 and sets @error to 1. In this case, a partial update may have been performed.
; Author ........: orbs
; Modified ......:
; Remarks .......: There is no validation of array size or contents!
;                  $iElement1, $iElement2, and $iElement3 are referenced by the global constants declared in this UDF. You can
;                  update up to 3 elements in a single call to this function. For readability, it is recommended you use a single
;                  call for several $__INIM_ALLOW_* conditions, another distinct call for $__INIM_DISALLOW_*, another call for
;                  $__INIM_TOOLTIP_*, and another call for $__INIM_BEEP_* conditions.
;                  REGARDING THE CONDITION STRINGS:
;                  Only single characters are imposed. A string cannot be imposed.
;                  Characters can be grouped by specifying double-dot between limits. For example:
;                    - Condition string "0..9" implies all digits.
;                    - Condition string "A..Za..z" implies all english letters, uppercase and lowercase.
;                  REGARDING THE BALLOON TOOLTIP AND BEEP:
;                  If you specify empty strings for the tooltip and zero values for the beep, then the only indication the user
;                  has that an invalid character was removed is that... well... it was removed. This is useful if you want to
;                  handle the notification yourself. In this case, use the _InputImpose function return value to determine if a
;                  notification is due, for example: If _InputImpose(...) Then MsgBox(...)
; Related .......:
; Link ..........:
; Example .......: Yes
; ===============================================================================================================================
Func _InputImpose_Update(ByRef $aCondition, $iElement1 = -1, $xValue1 = -1, $iElement2 = -1, $xValue2 = -1, $iElement3 = -1, $xValue3 = -1)
    ; update array
    If $iElement1 <> -1 Then $aCondition[$iElement1] = $xValue1
    If $iElement2 <> -1 Then $aCondition[$iElement2] = $xValue2
    If $iElement3 <> -1 Then $aCondition[$iElement3] = $xValue3
    ; parse full valid characters string for all string elements
    Local $aStringElement
    For $iCondition = 0 To 6
        If StringRight($aCondition[$iCondition], 2) = '..' Or StringInStr($aCondition[$iCondition], '...') Then Return SetError(1, 0, 0)
        $aStringElement = StringSplit($aCondition[$iCondition], '..', 1)
        If $aStringElement[0] > 1 Then
            $aCondition[$iCondition] = ''
            For $iStringElement = 1 To $aStringElement[0] - 1
                $aCondition[$iCondition] &= $aStringElement[$iStringElement]
                For $iChar = Asc(StringRight($aStringElement[$iStringElement], 1)) + 1 To Asc(StringLeft($aStringElement[$iStringElement + 1], 1)) - 1
                    $aCondition[$iCondition] &= Chr($iChar)
                Next
            Next
            $aCondition[$iCondition] &= $aStringElement[$aStringElement[0]]
        EndIf
    Next
    Return 1
EndFunc   ;==>_InputImpose_Update

; #INTERNAL_USE_ONLY# ===========================================================================================================
;__InputImpose_GUICtrlHasFocus
;__InputImpose_CaretPos
;__InputImpose_WaitForUser
;__InputImpose_GetIdleTime
; ===============================================================================================================================

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __InputImpose_GUICtrlHasFocus
; Description ...: Checks if a specific control in a specific GUI has focus.
; Syntax ........: __InputImpose_GUICtrlHasFocus($hGUI, $gControl)
; Parameters ....: $hGUI     - Handle to the GUI in which the control is located.
;                  $gControl - Control ID of the control to check.
; Return values .: Returns 1 if the control has focus.
;                  Returns 0 if the control does not have focus.
; Author ........:
; Modified ......:
; Remarks .......: Adopted from the link below. Several versions of this functions exist in the forum.
; Related .......:
; Link ..........: http://www.autoitscript.com/forum/topic/162416-edit-control-post-processing-to-checkformat-the-input/
; Example .......: No
; ===============================================================================================================================
Func __InputImpose_GUICtrlHasFocus($hGUI, $gControl)
    Local $hControl = ControlGetHandle($hGUI, "", ControlGetFocus($hGUI))
    If $hControl <> ControlGetHandle($hGUI, "", $gControl) Then Return 0 ; lose focus
    If $hControl = ControlGetHandle($hGUI, "", $gControl) Then Return 1 ; has focus
EndFunc   ;==>__InputImpose_GUICtrlHasFocus

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __InputImpose_CaretPos
; Description ...: Returns the coordinates of the caret in the foreground window.
; Syntax ........: __InputImpose_CaretPos()
; Parameters ....: None
; Return values .: Success - Returns a 2-element array containing the following information:
;                    $array[0] = X coordinate
;                    $array[1] = Y coordinate
;                  Failure - Returns an empty 2-element array and sets @error to 1
; Author ........:
; Modified ......:
; Remarks .......: Adopted from the AutoIt help example for WinGetCaretPos()
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __InputImpose_CaretPos()
    Local $aReturnIfError[2]
    Opt("CaretCoordMode", 0) ;relative mode
    Local $c = WinGetCaretPos() ;relative caret coords
    Local $w = WinGetPos("") ;window's coords
    Local $f = ControlGetFocus("", "") ;text region "handle"
    Local $e = ControlGetPos("", "", $f) ;text region coords
    Local $t[2]
    If IsArray($c) And IsArray($w) And IsArray($e) Then
        $t[0] = $c[0] + $w[0] + $e[0]
        $t[1] = $c[1] + $w[1] + $e[1]
        Return $t ;absolute screen coords of caret cursor
    Else
        Return SetError(1, 0, $aReturnIfError)
    EndIf
EndFunc   ;==>__InputImpose_CaretPos

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __InputImpose_WaitForUser
; Description ...: Waits until user activity (key press or mouse move) occurs.
; Syntax ........: __InputImpose_WaitForUser([$nTimeout = 0[,$nInterval = 100]])
; Parameters ....: $nTimeout  - [optional] Timeout [sec] for return even if no user activity occurred. Default is 0 (no timeout).
;                  $nInterval - [optional] Internal loop sleep parameter [ms]. Default is 100.
; Return values .: None
; Author ........: orbs
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __InputImpose_WaitForUser($nTimeout = 0, $nInterval = 100)
    Local $nIdleTimeStart, $nIdleTimeCurrent
    Local $hTimer = TimerInit()
    While True
        Sleep($nInterval)
        If $nTimeout > 0 And TimerDiff($hTimer) > $nTimeout * 1000 Then Return
        $nIdleTimeCurrent = __InputImpose_GetIdleTime()
        If $nIdleTimeCurrent < $nIdleTimeStart Then Return
        $nIdleTimeStart = $nIdleTimeCurrent
    WEnd
EndFunc   ;==>__InputImpose_WaitForUser

; #INTERNAL_USE_ONLY# ===========================================================================================================
; Name ..........: __InputImpose_GetIdleTime
; Description ...: Returns the number of ticks since last user activity (key press or mouse move).
; Syntax ........: __InputImpose_GetIdleTime()
; Parameters ....: None
; Return values .: Success - Returns time since last activity, in ticks (approx milliseconds).
;                    Also sets @extended to 1 if rollover of ticks counter has occured.
;                  Failure - Returns 0 and sets @error and @extended as set by internal DLL calls.
; Author ........:
; Modified ......:
; Remarks .......: This function is exact copy of _Timer_GetIdleTime() from the Timers UDF. It was copied here in order to avoid
;                  needlessly including another UDF.
; Related .......:
; Link ..........:
; Example .......: No
; ===============================================================================================================================
Func __InputImpose_GetIdleTime()
    ; Get ticks at last activity
    Local $tStruct = DllStructCreate("uint;dword");
    DllStructSetData($tStruct, 1, DllStructGetSize($tStruct));
    Local $aResult = DllCall("user32.dll", "bool", "GetLastInputInfo", "struct*", $tStruct)
    If @error Or $aResult[0] = 0 Then Return SetError(@error, @extended, 0)

    ; Get current ticks since last restart
    Local $avTicks = DllCall("Kernel32.dll", "dword", "GetTickCount")
    If @error Or Not $aResult[0] Then Return SetError(@error, @extended, 0)

    ; Return time since last activity, in ticks (approx milliseconds)
    Local $iDiff = $avTicks[0] - DllStructGetData($tStruct, 2)
    If $iDiff < 0 Then Return SetExtended(1, $avTicks[0]) ; Rollover of ticks counter has occured
    ; Normal return
    Return $iDiff
EndFunc   ;==>__InputImpose_GetIdleTime

the example script:

#AutoIt3Wrapper_Au3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <EditConstants.au3>
#include "InputImpose.au3"

#cs
    example input #1: using $ES_NUMBER for user experience reference
    example input #2 and #3: create conditions array, update it (by human-logical groups), impose it in main loop
    example input #4: simultaneously duplicate and update a conditions array, impose it in main loop
    example input #5: simultaneously create and update a conditions array, simultaneously apply a temporary update and impose it in main loop
    example input #6: single function call to temporary create, update and impose the conditions in main loop
#ce

Global $hGUI = GUICreate('InputImpose Example', 310, 185)
GUISetBkColor(0xFEDCBA)
Global $y = 5
GUICtrlCreateLabel('$ES_NUMBER:', 10, $y + 3)
Global $gInput0 = GUICtrlCreateInput('', 100, $y, 200, 20, $ES_NUMBER)
$y += 30
GUICtrlCreateLabel('Positive,Decimal:', 10, $y + 3)
Global $gInput1 = GUICtrlCreateInput('', 100, $y, 200, 20)
; only digits and decimal point, custom tooltip text, no tooltip title, timeout 3 sec., higher pitch beep
Global $aCondition1 = _InputImpose_Create()
_InputImpose_Update($aCondition1, $__INIM_ALLOW_ANYWHERE, '0..9', $__INIM_ALLOW_ONCE, '.')
_InputImpose_Update($aCondition1, $__INIM_TOOLTIP_TEXT, 'Only a Positive number (Whole or Decimal) is allowed', $__INIM_TOOLTIP_TIMEOUT, 3)
_InputImpose_Update($aCondition1, $__INIM_BEEP_FREQUENCY, 700)
$y += 30
GUICtrlCreateLabel('Negative,Whole:', 10, $y + 3)
Global $gInput2 = GUICtrlCreateInput('', 100, $y, 200, 20)
; only digits and leading minus, custom tooltip text & title
Global $aCondition2 = _InputImpose_Create()
_InputImpose_Update($aCondition2, $__INIM_ALLOW_ANYWHERE, '0..9', $__INIM_ALLOW_FIRST, '-')
_InputImpose_Update($aCondition2, $__INIM_ENFORCE_NUMLOWERLIMIT, -100, $__INIM_ENFORCE_NUMUPPERLIMIT, 100)
_InputImpose_Update($aCondition2, $__INIM_TOOLTIP_TEXT, 'Only a Whole number (Positive or Negative) is allowed, in range -100 to 100 (included).', $__INIM_TOOLTIP_TITLE, 'Invalid Entry')
$y += 30
GUICtrlCreateLabel('Negative,Decimal:', 10, $y + 3)
Global $gInput3 = GUICtrlCreateInput('', 100, $y, 200, 20)
; same as before, but with a decimal point allowed, and naturally a matching tooltip text to match
Global $aCondition3 = _InputImpose_Duplicate($aCondition2, $__INIM_ALLOW_ONCE, '.', $__INIM_TOOLTIP_TEXT, 'Only a number is allowed.')
$y += 30
GUICtrlCreateLabel('Math Expression:', 10, $y + 3)
Global $gInput4 = GUICtrlCreateInput('', 100, $y, 120, 20)
; any math expression
Global $aCondition4 = _InputImpose_Create($__INIM_ALLOW_ANYWHERE, '0..9.-+*/^()')
Global $gInput4_Calc = GUICtrlCreateButton('=', 225, $y, 20, 20)
GUICtrlSetCursor(-1, 0)
GUICtrlSetTip(-1, 'Evaluate Expression')
GUICtrlSetState(-1, $GUI_DEFBUTTON)
Global $gOutput4 = GUICtrlCreateInput('', 250, $y, 50, 20, BitOR($ES_AUTOHSCROLL, $ES_READONLY, $ES_RIGHT))
Global $sResult
GUICtrlSetBkColor(-1, 0xD4D4D4)
$y += 30
GUICtrlCreateLabel('File Name:', 10, $y + 3)
Global $gInput5 = GUICtrlCreateInput('', 100, $y, 200, 20)
GUISetState()
Global $msg = 0
While $msg <> $GUI_EVENT_CLOSE
    _InputImpose($hGUI, $gInput1, $aCondition1)
    _InputImpose($hGUI, $gInput2, $aCondition2)
    _InputImpose($hGUI, $gInput3, $aCondition3)
    _InputImpose($hGUI, $gInput4, $aCondition4, $__INIM_TOOLTIP_TEXT, '') ; no tooltip (blank text implies blamk title), just beep
    _InputImpose($hGUI, $gInput5, -1, $__INIM_DISALLOW_ANYWHERE, '\/:*?"<>|', $__INIM_TOOLTIP_TEXT, 'The following characters are not allowed: ' & @CR & '\ / : * ? " < > |', $__INIM_TOOLTIP_TITLE, 'Invalid Entry')
    $msg = GUIGetMsg()
    If $msg = $gInput4_Calc Then
        $sResult = Execute(GUICtrlRead($gInput4))
        If @error Then
            GUICtrlSetData($gOutput4, '')
            MsgBox(16, 'Evaluation Error', 'The expression could not be evaluated. You can NOT rely on InputImpose to validate a math expression!', 0, $hGUI)
        Else
            GUICtrlSetData($gOutput4, $sResult)
        EndIf
    EndIf
WEnd

 

Edited by orbs
3 people like this

Share this post


Link to post
Share on other sites



updated the UDF (in first post) from v0.1 to v0.3 - added tooltip and beep parameters. also updated the example.

Share this post


Link to post
Share on other sites

updated the UDF (in first post) from v0.3 to v0.7 - introduced the conditions array. also updated the example script.

Attention: No backward compatibility to the deprecated release (v0.3)!

While this UDF is in status "Development", i make no effort to maintain backward compatibility.

Share this post


Link to post
Share on other sites

updated the UDF (in first post) from v0.7 to v0.9 - introduced basic numerical conditions (upper/lower numeric limits). also updated the example script.

Share this post


Link to post
Share on other sites

Thank you for this UDF.  I'm surprised there haven't been comments.  I fills an important gap by providing guidance to a user at the right moment(s).  Good work!

 

Share this post


Link to post
Share on other sites

Impressive and very nice. Thank you


Skysnake

Why is the snake in the sky?

Share this post


Link to post
Share on other sites

Gee whiz!! I wonder how I missed this UDF! Perfect for database app I am busy with...

Thanks for sharing.

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