Mazaxist Posted September 28, 2012 Share Posted September 28, 2012 Hi guys, nooby question - im writing a simple app that modifies registry. When i press a button it asks me for a PC name and then i supposed to hit OK to excute code. Now my problem is - if i cancel out of the window or hit X it still executes the code!!!! i want it to do nothing if i hit CANCEL or X. help! Thanks in advance!! code: #RequireAdmin #include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode $mainwindow = GUICreate("TRtool v0.3", 200, 250) GUICtrlSetState(-1, $GUI_DISABLE) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") $enablebutton = GUICtrlCreateButton("Enable RD", 20, 20, 80) GUICtrlSetTip( -1, "Enable Remote Control on a remote PC") $disablebutton = GUICtrlCreateButton("Disable RD", 20, 50, 80) GUICtrlSetTip( -1, "Disable Remote Control on a remote PC") GUICtrlSetOnEvent($enablebutton, "EnableButton") GUICtrlSetOnEvent($disablebutton, "DisableButton") GUISetState(@SW_SHOW) While 1 Sleep(1000) ; Idle around WEnd Func EnableButton() Dim $Computer $Computer = Inputbox("Enter PC Name", "Enter PC name you want to modify", "") RegWrite("\\" & $Computer & "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server", "fDenyTSConnections", "REG_DWORD", "0") if @error Then MsgBox(0, "Enable RD", "Unable to access registry, please check if PC is online.") Else EndIf EndFunc Func DisableButton() Dim $Computer $Computer = Inputbox("Enter PC Name", "Enter PC name you want to modify", "") RegWrite("\\" & $Computer & "\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server", "fDenyTSConnections", "REG_DWORD", "1") if @error Then MsgBox(0, "Disable RD", "Unable to access registry, please check if PC is online.") Else EndIf EndFunc Func CLOSEClicked() ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, ;and @GUI_WINHANDLE would equal $mainwindow ;MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...") Exit EndFunc Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 28, 2012 Moderators Share Posted September 28, 2012 (edited) Mazaxist,Your script works as you want when I run it - the buttons run their respective functions and [X] ends the script. Try this functionally equivalent version and see if you get the same problem:#RequireAdmin #include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode $mainwindow = GUICreate("TRtool v0.3", 200, 250) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") $enablebutton = GUICtrlCreateButton("Enable RD", 20, 20, 80) GUICtrlSetOnEvent($enablebutton, "EnableButton") $disablebutton = GUICtrlCreateButton("Disable RD", 20, 50, 80) GUICtrlSetOnEvent($disablebutton, "DisableButton") GUISetState(@SW_SHOW) While 1 Sleep(1000) ; Idle around WEnd Func EnableButton() $Computer = Inputbox("Enter PC Name", "Enter PC name you want to modify", "") MsgBox(0, "Button", "Enable") Return EndFunc ;==>EnableButton Func DisableButton() $Computer = Inputbox("Enter PC Name", "Enter PC name you want to modify", "") MsgBox(0, "Button", "Disable") Return EndFunc ;==>DisableButton Func CLOSEClicked() MsgBox(0, "[X]", "Exiting...") Exit EndFunc ;==>CLOSEClickedStill there? Completely misunderstood the fact it was the InputBox you were cancelling - JohnOne has the answer. Time for bed I feel! M23 Edited September 28, 2012 by Melba23 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...
JohnOne Posted September 28, 2012 Share Posted September 28, 2012 You should check @error immediately after your inputbox func it might work correctly as it is because regwrite may be @error with a blank sting passed to it. AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. Link to comment Share on other sites More sharing options...
Mazaxist Posted September 28, 2012 Author Share Posted September 28, 2012 (edited) You should check @error immediately after your inputbox funcit might work correctly as it is because regwrite may be @error with a blank sting passed to it.i still dont qute understand... i think i need to find some kind of lessons for autoit. i think that @error has nothing to do with CANCEL button. i only added @error to get an error if remote PC was uinavailable to connect.so for example if i click the button and input PC name and then hit CANCEL - it will still execute the code and mod regedit - but i want it to simply CANCEL! ;( Edited September 28, 2012 by Mazaxist Link to comment Share on other sites More sharing options...
kylomas Posted September 28, 2012 Share Posted September 28, 2012 (edited) Mazaxist, Use a case stmt for the result of your input controls as follows (as JohnOne suggested) expandcollapse popup#RequireAdmin #include <GUIConstantsEx.au3> Opt("GUIOnEventMode", 1) ; Change to OnEvent mode $mainwindow = GUICreate("TRtool v0.3", 200, 250) GUICtrlSetState(-1, $GUI_DISABLE) GUISetOnEvent($GUI_EVENT_CLOSE, "CLOSEClicked") $enablebutton = GUICtrlCreateButton("Enable RD", 20, 20, 80) GUICtrlSetTip(-1, "Enable Remote Control on a remote PC") $disablebutton = GUICtrlCreateButton("Disable RD", 20, 50, 80) GUICtrlSetTip(-1, "Disable Remote Control on a remote PC") GUICtrlSetOnEvent($enablebutton, "EnableButton") GUICtrlSetOnEvent($disablebutton, "DisableButton") GUISetState(@SW_SHOW) While 1 Sleep(1000) ; Idle around WEnd Func EnableButton() Dim $Computer $Computer = InputBox("Enter PC Name", "Enter PC name you want to modify", "") switch @error case 0 ; do you reg work, string is valid case 1 ; do exit stuff, cancel button was pushed endswitch EndFunc ;==>EnableButton Func DisableButton() Dim $Computer $Computer = InputBox("Enter PC Name", "Enter PC name you want to modify", "") switch @error case 0 ; do you reg work, string is valid case 1 ; do exit stuff, cancel button was pushed endswitch EndFunc ;==>DisableButton Func CLOSEClicked() ;Note: at this point @GUI_CTRLID would equal $GUI_EVENT_CLOSE, ;and @GUI_WINHANDLE would equal $mainwindow ;MsgBox(0, "GUI Event", "You clicked CLOSE! Exiting...") Exit EndFunc ;==>CLOSEClicked kylomas edit: changed case stmt as had wron @error code Edited September 28, 2012 by kylomas Forum Rules Procedure for posting code "I like pigs. Dogs look up to us. Cats look down on us. Pigs treat us as equals." - Sir Winston Churchill Link to comment Share on other sites More sharing options...
JohnOne Posted September 29, 2012 Share Posted September 29, 2012 Regarding tutorials, click the wiki link at top of page, you will see tutorials. If you are new to any scripting try the link in my signature. When a function is used, @error is set, usually even if there is no error it is still set to 0. So to check it, you need to do so immediately after the function, or store @error in a variable for later use. $var = SomeFunction() $error = @error AutoIt Absolute Beginners Require a serial Pause Script Video Tutorials by Morthawt ipify Monkey's are, like, natures humans. 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