Jump to content

Code executed wether i press OK or CANCEL help!


Mazaxist
 Share

Recommended Posts

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

  • Moderators

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   ;==>CLOSEClicked

Still there? :huh:

Completely misunderstood the fact it was the InputBox you were cancelling - JohnOne has the answer. Time for bed I feel! :D

M23

Edited by Melba23

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

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.

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 by Mazaxist
Link to comment
Share on other sites

Mazaxist,

Use a case stmt for the result of your input controls as follows (as JohnOne suggested)

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

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

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