Jump to content

GUI Input verification loop


Recommended Posts

I have a simple GUI with only 5 input boxes. I need to be able to verify each input. If the data entered is verified to be incorrect, the script should return to the GUI, clear the previous entry and allow the user to enter the data again.

The script belows clears the inputbox but the msgbox keeps popping up not getting focus back to the GUI for another try. What am I missing here?

$Form1_1 = GUICreate("Cost Account Numbers", 477, 389, 192, 114)
    $tbECM = GUICtrlCreateInput("", 152, 20, 153, 28)
    GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
        GUICtrlSetOnEvent($tbECM, "tbECM")
; more GUICtrlCreateInput here ****
    $btnOK = GUICtrlCreateButton("OK", 149, 328, 75, 25)
        GUICtrlSetOnEvent($btnOK, "btnOKClick")
    $btnCancel = GUICtrlCreateButton("Cancel", 253, 328, 75, 25)
        GUICtrlSetOnEvent($btnCancel, "btnCancelClick")
GUISetState(@SW_SHOW)

Func tbECM()
    do
        $tbECM = GUICtrlRead(8, 1)
        ECMverify($tbECM)
            $tbECM = GUICtrlCreateInput("", 152, 20, 153, 28)
    until StringLen($tbECM)>=10
EndFunc
func ECMverify($tbECM)
        $tbECM = StringReplace($tbECM," ", "")
        if stringleft($tbECM,3) <> StringLower("ecm") then $tbECM = "ecm" & $tbECM
        $len=StringLen($tbECM)
            if $len<10 then
                MsgBox(0,"Error", "The ECM number is not correct. Please fix.")
                GUICtrlSetState(8,$GUI_FOCUS)
                GUICtrlSetData (8,"")
            EndIf
        return $tbECM
EndFunc

Thanks for any help.

Trip

Link to comment
Share on other sites

  • Moderators

TripScott,

You were using the same variable for both the input and its content - never a good idea. You were also hardcoding ControlIDs ($tbECM = GUICtrlRead(8, 1)) - presumably because you found the variable no longer worked as you had overwritten it! :huh2:

Take a look at this version and see if it works for you: ;)

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$Form1_1 = GUICreate("Cost Account Numbers", 477, 389, 192, 114)

$hECM = GUICtrlCreateInput("", 152, 20, 153, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetOnEvent($hECM, "tbECM")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func tbECM()

    $sECM = GUICtrlRead($hECM)
    Switch ECMverify($sECM)
        Case 1
            MsgBox(0, "Checked", "ECM Number valid")
            Exit
        Case 0
            MsgBox(0, "Error", "The ECM number is not correct. Please fix.")
            GUICtrlSetData($hECM, "")
    EndSwitch

EndFunc   ;==>tbECM

Func ECMverify($sECM)
    ; Strip all spaces
    $sECM = StringStripWS($sECM, 8)
    ; Add ecm if required
    If StringLeft($sECM, 3) <> StringLower("ecm") Then $sECM = "ecm" & $sECM
    ; Check length
    If StringLen($sECM) < 10 Then
        Return 0
    EndIf
    Return 1
EndFunc   ;==>ECMverify

Your ECMverify function is, frankly, less than optimal. What exactly do you want to check? I presume a valid entry is "ecm" followed by 7 digits or just the 7 digits - am I right? Let me know and I will try and develop a better test for you. :alien:

M23

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:

Spoiler

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 comment
Share on other sites

M23,

I just added 'GUICtrlSetState($hECM,$GUI_FOCUS)' in Case 0 & it works just like I want it to. Thank you! :-)

As for your question on the ECMVerify function. I only created it thinking it would work to create the verification loop in a seperate function. It doesn't have to be. I was just coming up with alternatives.

>>"You were using the same variable for both the input and its content - never a good idea." I didn't know that. Thank you.

>>"You were also hardcoding ControlIDs ($tbECM = GUICtrlRead(8, 1)) - presumably because you found the variable no longer worked as you had overwritten it! :huh2: " I was thinking this would clear out the previous entry.

>>"What exactly do you want to check? I presume a valid entry is "ecm" followed by 7 digits or just the 7 digits - am I right?" The data entered here should look like this: "ecm" + 7 digits + [.ALPHAS] (lower 'ecm' followed by 7 digits, optionally followed by '.' upper letter(s))

Thanks again for your help.

Trip

TripScott,

You were using the same variable for both the input and its content - never a good idea. You were also hardcoding ControlIDs ($tbECM = GUICtrlRead(8, 1)) - presumably because you found the variable no longer worked as you had overwritten it! ;)

Take a look at this version and see if it works for you: :alien:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$Form1_1 = GUICreate("Cost Account Numbers", 477, 389, 192, 114)

$hECM = GUICtrlCreateInput("", 152, 20, 153, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")
GUICtrlSetOnEvent($hECM, "tbECM")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func tbECM()

    $sECM = GUICtrlRead($hECM)
    Switch ECMverify($sECM)
        Case 1
            MsgBox(0, "Checked", "ECM Number valid")
            Exit
        Case 0
            MsgBox(0, "Error", "The ECM number is not correct. Please fix.")
            GUICtrlSetData($hECM, "")
    EndSwitch

EndFunc   ;==>tbECM

Func ECMverify($sECM)
    ; Strip all spaces
    $sECM = StringStripWS($sECM, 8)
    ; Add ecm if required
    If StringLeft($sECM, 3) <> StringLower("ecm") Then $sECM = "ecm" & $sECM
    ; Check length
    If StringLen($sECM) < 10 Then
        Return 0
    EndIf
    Return 1
EndFunc   ;==>ECMverify

Your ECMverify function is, frankly, less than optimal. What exactly do you want to check? I presume a valid entry is "ecm" followed by 7 digits or just the 7 digits - am I right? Let me know and I will try and develop a better test for you. :ph34r:

M23

Link to comment
Share on other sites

  • Moderators

TripScott,

Here is a more comprehensive validation function for you: :alien:

#include <GUIConstantsEx.au3>

Opt("GUIOnEventMode", 1)

$Form1_1 = GUICreate("Cost Account Numbers", 477, 389, 192, 114)
GUISetOnEvent($GUI_EVENT_CLOSE, "On_Exit")

$hECM = GUICtrlCreateInput("", 152, 20, 153, 28)
GUICtrlSetFont(-1, 12, 400, 0, "MS Sans Serif")

$hButton = GUICtrlCreateButton("Test", 150, 70, 153, 28)
GUICtrlSetOnEvent($hButton, "tbECM")

GUISetState(@SW_SHOW)

While 1
    Sleep(10)
WEnd

Func tbECM()

    $sECM = GUICtrlRead($hECM)

    $sECM = ECMverify($sECM)
    Switch $sECM
        Case 0
            MsgBox(0, "Error", "The ECM number is not correct. Please fix.")
            GUICtrlSetData($hECM, "")
        Case Else
            $sECM = "ecm" & $sECM
            MsgBox(0, "Checked", "ECM Number valid" & @CRLF & $sECM)
            Exit
    EndSwitch

EndFunc   ;==>tbECM

Func ECMverify($sECM)

    ; Strip all spaces
    $sECM = StringStripWS($sECM, 8)
    ; Strip any alpha characters at start
    While 1
        If Number($sECM) Then
            ExitLoop
        Else
            $sECM = StringTrimLeft($sECM, 1)
        EndIf
    WEnd

    ; Check remaining input
    If StringRegExp($sECM, "^(\d){7}(\z|\.\D+)", Then
        Return $sECM
    EndIf
    Return 0
EndFunc   ;==>ECMverify

Func On_Exit()
    Exit
EndFunc

It strips all letters at the beginning of the input content and then checks that there are exactly 7 digits, possibly followed by ".alphas". We then add the "ecm" at the beginning. Try it and see if you would like to use it. :huh2:

If you are interested, the SRE works liek this:

^           Start of string
(\d){7}     Exactly 7 digits
(\z|\.\D+)  Either the end of the string or a dot followed by at least one non-numeric character

Please ask if you have any questions. ;)

M23

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:

Spoiler

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 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...