(!) RestrictControlRegExp-UDF - automatisch zu setzende Zeichen

  • Hallo Com,

    ich arbeite momentan mit der UDF von 'peethebee': RestrictControlRegExp-UDF - Fehleingaben direkt verhindern, Eingabentypen festlegen und habe auch schon mit ihm gesprochen gehabt.

    Mein Anliegen: Ich habe einen einzeiligen Input Ctrl indem eine Uhrzeit eingegeben werden soll. D.h. die UDF soll nach 2 Zeichen automatisch ein Zeichen einfügen.
    Die Variante einfach 2 InputCtrls zu nehmen ist mir bekannt aber mir wäre wenn möglich die kleine Anpassung der UDF 1000x lieber.

    Wahrscheinlich muss in der

    [autoit]

    Func _RegEx_RestrictControl_check($hWnd, $msg, $wParam, $lParam)

    [/autoit]

    etwas hinzugefügt werden.

    main.au3
    [autoit]

    #include <EditConstants.au3>
    #include <GUIConstantsEx.au3>
    #include "RestrictControlRegExp.au3"

    [/autoit] [autoit][/autoit] [autoit]

    _RegEx_RestrictControl_setup (1) ; prepare for up to 20 Controls to restrict

    [/autoit] [autoit][/autoit] [autoit]

    Global $hGUI = GUICreate("", 120, 50)
    Global $hINPUT = GUICtrlCreateInput("", 10, 5, 100, 20, $ES_WANTRETURN)
    _RegEx_RestrictControl_add ($hINPUT, "^[012]{1}[0-9]{1}\:[0-6]{1}[0-9]{1}$", "13:37")

    [/autoit] [autoit][/autoit] [autoit]

    GUISetState(@SW_SHOW, $hGUI)
    GUISetOnEvent($GUI_EVENT_CLOSE, "_exit", $hGUI)

    [/autoit] [autoit][/autoit] [autoit]

    ; Wechsle in den OnEvent Modus
    Opt("GUIOnEventMode", 1)

    [/autoit] [autoit][/autoit] [autoit]

    ; Schleife
    While 1
    Sleep(100) ; beschäftigen, damit die CPU-Auslastung nicht zu hoch geht
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func _exit()
    Exit
    EndFunc ;==>_exit

    [/autoit]

    Mache ich aus:

    UDF 'RestrictControlRegExp.au3'
    [autoit]

    ; ===============================================================================
    ;
    ; UDF Name: RestrictControlRegExp.au3
    ; Description: This UDF can restrict the text typed into input controls live
    ; while typing based on a given Regular Expression.
    ; Requirement: AutoIt 3.2.0.1 or higher
    ; Author: peethebee ([email='peethebee@gmx.de'][/email], http://www.autoit.de)
    ; Notice: based on a script by gafrost in this thread:
    ; http://www.autoitscript.com/forum/index.ph…=guiregistermsg
    ;
    ; ===============================================================================

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    #include-once

    [/autoit] [autoit][/autoit] [autoit]

    #include <EditConstants.au3>
    #include <GUIConstants.au3>
    #include <WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    ;Global Const $WM_COMMAND = 0x0111
    ;Global Const $EN_CHANGE = 0x300

    [/autoit] [autoit][/autoit] [autoit]

    Global $array__RegEx_RestrictControl[1][4]
    Global $array__RegEx_RestrictControl_id_count = 0

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Function Name: _RegEx_RestrictControl_setup
    ; Description: Sets up the features of this UDF.
    ; Parameter(s): $_re_rc_max_controls (opotional, default 10)
    ; defines how many controls gan be controlled at maximum
    ; Requirement(s): This UDF included
    ; Return Value(s): None.
    ; Author(s): peethebee
    ;
    ;===============================================================================
    Func _RegEx_RestrictControl_setup($_re_rc_max_controls = 10)
    GUIRegisterMsg($WM_COMMAND, "_RegEx_RestrictControl_check")
    ReDim $array__RegEx_RestrictControl[$_re_rc_max_controls][4]
    $array__RegEx_RestrictControl_id_count = 0
    EndFunc ;==>_RegEx_RestrictControl_setup

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Function Name: _RegEx_RestrictControl_add
    ; Description: Sets up the controlling for a control.
    ; Parameter(s): $_re_rc_ctrlid
    ; ID of the control to be monitored
    ; $_re_rc_regex_pattern
    ; RegExp which has to be fullfilled or fullfillable
    ; $_re_rc_valid_string (optional, default "")
    ; Valid string for testing if RegExp can still be fullfilled.
    ; Requirement(s): This UDF included
    ; Return Value(s): None.
    ; Author(s): peethebee
    ;
    ;===============================================================================
    Func _RegEx_RestrictControl_add($_re_rc_ctrlid, $_re_rc_regex_pattern, $_re_rc_valid_string = "")
    $array__RegEx_RestrictControl[$array__RegEx_RestrictControl_id_count][0] = $_re_rc_ctrlid
    $array__RegEx_RestrictControl[$array__RegEx_RestrictControl_id_count][1] = $_re_rc_regex_pattern
    $array__RegEx_RestrictControl[$array__RegEx_RestrictControl_id_count][2] = $_re_rc_valid_string
    $array__RegEx_RestrictControl[$array__RegEx_RestrictControl_id_count][3] = ""
    $array__RegEx_RestrictControl_id_count += 1
    EndFunc ;==>_RegEx_RestrictControl_add

    [/autoit] [autoit][/autoit] [autoit][/autoit] [autoit]

    ;===============================================================================
    ;
    ; Function Name: _RegEx_RestrictControl_check
    ; Description: Internal processing function - not to be called from outside!
    ; Parameter(s): [...]
    ; Requirement(s): This UDF included
    ; Return Value(s): None.
    ; Author(s): peethebee (using gafrost's work as a basis)
    ;
    ;===============================================================================
    Func _RegEx_RestrictControl_check($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam

    [/autoit] [autoit][/autoit] [autoit]

    ;~ MsgBox(0, "", UBound($array__RegEx_RestrictControl, 1))
    For $_re_rc_i = 0 To UBound($array__RegEx_RestrictControl, 1) - 1
    If $nID = $array__RegEx_RestrictControl[$_re_rc_i][0] Then
    If $nNotifyCode = $EN_CHANGE Then
    ; Check RegEx and set text
    For $_re_rc_j = 0 To StringLen($array__RegEx_RestrictControl[$_re_rc_i][2])
    $_re_rc_regex_res = 1
    ;~ MsgBox(0, "regexp string", GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0]) & StringTrimLeft($array__RegEx_RestrictControl[$_re_rc_i][2], $_re_rc_j))
    If StringRegExp (GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0]) & StringTrimLeft($array__RegEx_RestrictControl[$_re_rc_i][2], $_re_rc_j), $array__RegEx_RestrictControl[$_re_rc_i][1]) Then ExitLoop
    $_re_rc_regex_res = 0
    Next
    If $_re_rc_regex_res = 1 Then
    ; save string as "good" value for later resettability
    $array__RegEx_RestrictControl[$_re_rc_i][3] = GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0])
    Else
    ; reset Text to last known good value
    GUICtrlSetData($array__RegEx_RestrictControl[$_re_rc_i][0], $array__RegEx_RestrictControl[$_re_rc_i][3])
    EndIf
    EndIf
    EndIf
    Next
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
    EndFunc ;==>_RegEx_RestrictControl_check

    [/autoit] [autoit][/autoit] [autoit]

    ; helping function by gafrost
    Func _HiWord($x)
    Return BitShift($x, 16)
    EndFunc ;==>_HiWord

    [/autoit] [autoit][/autoit] [autoit]

    ; helping function by gafrost
    Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
    EndFunc ;==>_LoWord

    [/autoit]

    folgendes:

    bearbeitete Funktion
    [autoit]

    Func _RegEx_RestrictControl_check($hWnd, $msg, $wParam, $lParam)
    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam

    [/autoit] [autoit][/autoit] [autoit]

    ;~ MsgBox(0, "", UBound($array__RegEx_RestrictControl, 1))
    For $_re_rc_i = 0 To UBound($array__RegEx_RestrictControl, 1) - 1
    If $nID = $array__RegEx_RestrictControl[$_re_rc_i][0] Then
    If $nNotifyCode = $EN_CHANGE Then
    ; Check RegEx and set text
    For $_re_rc_j = 0 To StringLen($array__RegEx_RestrictControl[$_re_rc_i][2])
    $_re_rc_regex_res = 1
    ;~ MsgBox(0, "regexp string", GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0]) & StringTrimLeft($array__RegEx_RestrictControl[$_re_rc_i][2], $_re_rc_j))
    If StringRegExp (GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0]) & StringTrimLeft($array__RegEx_RestrictControl[$_re_rc_i][2], $_re_rc_j), $array__RegEx_RestrictControl[$_re_rc_i][1]) Then ExitLoop
    $_re_rc_regex_res = 0
    Next
    If $_re_rc_regex_res = 1 Then
    ; save string as "good" value for later resettability
    $array__RegEx_RestrictControl[$_re_rc_i][3] = StringMid(GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0]), 1, 2) & ":" & StringMid(GUICtrlRead($array__RegEx_RestrictControl[$_re_rc_i][0]), 4, 2)
    Else
    ; reset Text to last known good value
    GUICtrlSetData($array__RegEx_RestrictControl[$_re_rc_i][0], $array__RegEx_RestrictControl[$_re_rc_i][3])
    EndIf
    EndIf
    EndIf
    Next
    ; Proceed the default Autoit3 internal message commands.
    ; You also can complete let the line out.
    ; !!! But only 'Return' (without any value) will not proceed
    ; the default Autoit3-message in the future !!!
    Return $GUI_RUNDEFMSG
    EndFunc ;==>_RegEx_RestrictControl_check

    [/autoit]

    So wird aus der Eingabe '1234' -> 12:4, aus '12345' -> 12:45 ... Der Doppelpunkt benötigt immernoch eine Eingabe, aber es ist egal welche.

    Könnt ihr mir vll. helfen dies wie oben erwähnt zu verbessern?
    'peethebee' meinte noch:

    Zitat

    Hab den Code gerade nicht da, aber auch keine Zeit im Moment. Der Reset-Part ist vermutlich die falsche Stelle für die Änderung. Mach das außerhalb der Fallunterscheidung (danach denke ich).[...]


    Grüße und schonmal Danke im voraus

    Grüße Yaerox

    Grüne Hölle

    Einmal editiert, zuletzt von Yaerox (17. Dezember 2012 um 09:27) aus folgendem Grund: vergessen den Thread auf gelöst zu stellen

    • Offizieller Beitrag

    Hier alter Code, den ich in meinen Schnipseln gefunden habe. (Habe ich nicht geschrieben, aber ggf. hilft er dir ja.

    Spoiler anzeigen
    [autoit]

    #include<Array.au3>
    #include<EditConstants.au3>
    #include<GUIConstantsEx.au3>
    #include<WindowsConstants.au3>

    [/autoit] [autoit][/autoit] [autoit]

    ;Global $changed = False

    [/autoit] [autoit][/autoit] [autoit]

    GUICreate("My GUI Zeiteingabe",200,100) ; will create a dialog box that when displayed is centered

    [/autoit] [autoit][/autoit] [autoit]

    $edit = GUICtrlCreateInput(" :", 10, 30, 52, 20,$ES_NUMBER) ; Eingabebox
    GUICtrlSetFont(-1,10,600,0,"Courier New") ; nichtproportionale Schrift gewählt, für die Optik des gleichbleibenden Abstandes vor dem ":" mit Leerzeichen
    GUICtrlSetLimit(-1,5)
    $edit1 = GUICtrlCreateInput(" :", 75, 30, 52, 20,$ES_NUMBER) ; Eingabebox
    GUICtrlSetFont(-1,10,600,0,"Courier New") ; nichtproportionale Schrift gewählt, für die Optik des gleichbleibenden Abstandes vor dem ":" mit Leerzeichen
    GUICtrlSetLimit(-1,5)
    $edit2 = GUICtrlCreateInput(" :", 140, 30, 52, 20,$ES_NUMBER) ; Eingabebox
    GUICtrlSetFont(-1,10,600,0,"Courier New") ; nichtproportionale Schrift gewählt, für die Optik des gleichbleibenden Abstandes vor dem ":" mit Leerzeichen
    GUICtrlSetLimit(-1,5)
    $btn = GUICtrlCreateButton("Ohne Funktion", 50, 70, 100, 20)
    GUISetState()
    GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND")
    Send("{LEFT 3}")
    While 1
    $msg = GUIGetMsg()
    Select
    Case $msg = $GUI_EVENT_CLOSE
    ExitLoop
    EndSelect
    WEnd

    [/autoit] [autoit][/autoit] [autoit]

    Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)
    Local $backspc = 0
    Local $nNotifyCode = _HiWord($wParam)
    Local $nID = _LoWord($wParam)
    Local $hCtrl = $lParam
    Switch $nID ; aktuelle Inputbox
    Case $edit Or $edit1 Or $edit2
    Switch $nNotifyCode
    Case $EN_CHANGE
    $temp = StringSplit(GUICtrlRead($nID),":")
    If @error Then
    _ArrayAdd($temp,"")
    EndIf
    $temp[1] = StringReplace($temp[1]," ","")
    If $temp[0] = 1 Then
    $temp[1] = StringLeft($temp[1],1)&" "
    GUICtrlSetData($nID,$temp[1]&":")
    Send("{LEFT 2}")
    EndIf
    Switch StringLen($temp[2])
    Case 2
    Send("{TAB}")
    Case 1
    If NOT StringIsDigit($temp[1]) Then
    GuiCtrlSetData($nID," :")
    Send ("{LEFT 3}")
    EndIf
    If $temp[2] > 5 Then
    $temp[2]=""
    GuiCtrlSetData($nID,$temp[1]&":"&$temp[2])
    EndIf
    Case 0
    Switch StringLen($temp[1])
    Case 2
    Switch StringLeft($temp[1],1)
    Case 2
    If $temp[1] > 23 Then
    $temp[1] = "2 "
    GuiCtrlSetData($nID,$temp[1]&":")
    send ("{LEFT 2}")
    Else
    GuiCtrlSetData($nID,$temp[1]&":")
    EndIf
    Case 1
    GuiCtrlSetData($nID,$temp[1]&":")
    Case 0
    GuiCtrlSetData($nID,$temp[1]&":")
    EndSwitch
    Case 1
    Switch $temp[1]
    Case 3 to 9
    $temp[1] = "0"&$temp[1]
    GuiCtrlSetData($nID,$temp[1]&":")
    Case 0 to 2
    GuiCtrlSetData($nID,$temp[1]&" :")
    send ("{LEFT 2}")
    EndSwitch
    Case 0
    GuiCtrlSetData($nID," :")
    send ("{LEFT 3}")
    EndSwitch
    EndSwitch
    Case $EN_SETFOCUS
    GuiCtrlSetData($nID," :")
    Send("{LEFT 3}")
    Case $EN_KILLFOCUS
    $temp = StringSplit(GUICtrlRead($nID),":")
    $temp[1] = StringReplace($temp[1]," ","")
    If $temp[0] = 1 Then _ArrayAdd($temp,"")
    If StringLen($temp[2]) = 2 Then
    If StringLen($temp[1]) = 2 Then
    Return
    Else
    GUICtrlSetData($nID," :")
    Return
    EndIf
    EndIf
    Switch StringisDigit($temp[1])
    Case True
    Switch StringLen($temp[1])
    Case 1
    If $temp[1] > 2 Then $temp[1] = "0"&$temp[1]
    If $temp[1] <= 2 Then $temp[1] = " "
    Case 2
    If $temp[2] > "" Then
    $temp[2] = $temp[2]&"0"
    GUICtrlSetData($nID,$temp[1]&":"&$temp[2])
    EndIf
    EndSwitch
    If $temp[2] = "" Or $temp[2] = 0 Then
    $temp[2] = "00"
    GUICtrlSetData($nID,$temp[1]&":"&$temp[2])
    EndIf
    Case False
    GUICtrlSetData($nID," :")
    EndSwitch
    EndSwitch
    EndSwitch
    EndFunc ;==>MY_WM_COMMAND

    [/autoit] [autoit][/autoit] [autoit]

    Func _HiWord($x)
    Return BitShift($x, 16)
    EndFunc ;==>_HiWord

    [/autoit] [autoit][/autoit] [autoit]

    Func _LoWord($x)
    Return BitAND($x, 0xFFFF)
    EndFunc ;==>_LoWord

    [/autoit]