TripScott Posted June 22, 2011 Share Posted June 22, 2011 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 More sharing options...
Moderators Melba23 Posted June 22, 2011 Moderators Share Posted June 22, 2011 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: expandcollapse popup#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 ;==>ECMverifyYour 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. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
TripScott Posted June 22, 2011 Author Share Posted June 22, 2011 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! " 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: expandcollapse popup#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. M23 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted June 22, 2011 Moderators Share Posted June 22, 2011 TripScott,Here is a more comprehensive validation function for you: expandcollapse popup#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 EndFuncIt 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. 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 characterPlease ask if you have any questions. M23 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 columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now