Jump to content

Options being ignored in Yes/No Messagebox


Go to solution Solved by Melba23,

Recommended Posts

First, I want to say I am VERY new to AutoIT, and I think this is probably a very simple mistake.  I am having no luck seeing where my script differs from the samples in the helpfiles and the forums, and I would definitely appreciate some fresh eyes and feedback on this.

Task:  I want to have a confirmation dialog validating the user's earlier selection.

Function:  I'm using a Yes/No MsgBox and wish to take the specified action if yes, or go back to my first function if no.

Issue 1:  The Make Second button Default flag (256) is being ignored.

Issue 2:  The Yes option is being executed regardless of the Yes/No selection.

I have tried replacing the If/Then with Select and Switch with no success.

Relevant Script Lines:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

-Script omitted where my main function is MainGUI and a local variable $sItem is defined by an earlier list selection.-

Here's what is not behaving as I would expect it to:

==

Global $result = MsgBox(4096+4+32+256,"Confirm Selection", "You selected " & $sItem & ", Is this Correct?",0)
If $result = 6 Then
    RunWait("C:\Windows\System32\tzutil.exe /s " & $sItem)
ElseIf $result = 7 Then
    Call (MainGUI)
EndIf

==

What am I missing here?  Thank you in advance for your help.

Link to comment
Share on other sites

Should be simple as such 4096 + 4 + 32 + 256 = 4388 for msgbox value.

Global $result = MsgBox(4388, "Confirm Selection", "You selected " & $sItem & ", Is this Correct?")

If $result = 6 Then
    RunWait("C:\Windows\System32\tzutil.exe /s " & $sItem)
ElseIf $result = 7 Then
    Call(MainGUI)
EndIf

Does that help? :)

P.S. Welcome to the AutoIt forum! :D

Edited by MikahS

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

Please, post your whole script so that we can better help you. That worked fine for me, so it must be something other than this in your script. ;)

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

OK.  Here's the whole beastly thing so far.  As a note, everything works EXCEPT the No selection and default.  The script simply changes the timezone now even if I tell it no. -.-

==

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>
 Opt("GUIOnEventMode", 1)

MainGUI()

 ; ----- GUIs
Func MainGUI()
  Global $listview
  $listGUI = GUICreate("Timezone Selection Utility", 400, 200, 100, 200, -1)
  GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Main")
  $listview = GUICtrlCreateListView("Timezones", 10, 10, 300, 150)
  _GUICtrlListView_SetColumnWidth($listview, 0, 250)

  GUICtrlCreateListViewItem("Central Standard Time", $listview)
  GUICtrlCreateListViewItem("Central Standard Time (Mexico)", $listview)
  GUICtrlCreateListViewItem("Mountain Standard Time (Mexico)", $listview)

   $BtnSelect = GUICtrlCreateButton("Select", 100, 165, 80, 30)
  GUICtrlSetOnEvent(-1, "SelectItem")

  GUISetState()
 
  While 1
    Sleep(10)
  WEnd
EndFunc
 ; ///// Functions

; Take the Selection and transform it into the expected TZID
Func SelectItem()
  $sItem = GUICtrlRead(GUICtrlRead($listview))
  $sItem = StringTrimRight($sItem, 1) ; Will remove the pipe "|" from the end of the string
  $sItem = '"' & $sItem & '"' ; Add the mandatory quotation marks to pass to tzutil
 
Global $result = MsgBox(4388,"Confirm Selection", "You selected " & $sItem & ", Is this Correct?",0)
If $result = 6 Then
    RunWait("C:\Windows\System32\tzutil.exe /s " & $sItem)
ElseIf $result = 7 Then
    Call (MainGUI)
EndIf

EndFunc
 
Func On_Close_Main()
   Exit
EndFunc

Edited by vyperhand
Link to comment
Share on other sites

  • Moderators

vyperhand,

That code works perfectly for me - although I would prefer to see named constants rather than "magic numbers": ;)

#include <MsgBoxConstants.au3>

$iResult = MsgBox($MB_YESNO + $MB_ICONQUESTION + $MB_DEFBUTTON2 + $MB_SYSTEMMODAL, "Confirm Selection", "Is this Correct?") ; No need for 0 - that is the default
If $iResult = 6 Then
    ConsoleWrite("Yes" & @CRLF)
ElseIf $iResult = 7 Then
    ConsoleWrite("No" & @CRLF)
EndIf
I get the correct return in the console each time. :)

M23

P.S. No need to use Call unless you have the function name as a string - a simple MainGUI() will suffice. ;)

P.P.S And I hope this snippet is not inside the MainGUI function or recursion raises its ugly head. :ohmy:

Edit: I see it is! Let me try and rehash your script to avoid the impending meltdown. :(

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

  • Moderators
  • Solution

vyperhand,

Here is the modified script - I have explained why I have made the changes:

#include <GUIConstantsEx.au3>
#include <GuiListView.au3>
#include <MsgBoxConstants.au3>

Opt("GUIOnEventMode", 1)

Global $listGUI, $listview ; Do not declare Global inside functions

MainGUI()

; Always return to the main script for the idle loop
While 1
    Sleep(10)
WEnd

; ----- GUIs
Func MainGUI()
    $listGUI = GUICreate("Timezone Selection Utility", 400, 200, 100, 200, -1)
    GUISetOnEvent($GUI_EVENT_CLOSE, "On_Close_Main")

    $listview = GUICtrlCreateListView("Timezones", 10, 10, 300, 150)
    _GUICtrlListView_SetColumnWidth($listview, 0, 250)

    GUICtrlCreateListViewItem("Central Standard Time", $listview)
    GUICtrlCreateListViewItem("Central Standard Time (Mexico)", $listview)
    GUICtrlCreateListViewItem("Mountain Standard Time (Mexico)", $listview)

    $BtnSelect = GUICtrlCreateButton("Select", 100, 165, 80, 30)
    GUICtrlSetOnEvent(-1, "SelectItem")

    GUISetState()

EndFunc   ;==>MainGUI
; ///// Functions

; Take the Selection and transform it into the expected TZID
Func SelectItem()

    GUISetState(@SW_HIDE, $listGUI) ; Hide the main GUI

    $sItem = GUICtrlRead(GUICtrlRead($listview))
    $sItem = StringTrimRight($sItem, 1) ; Will remove the pipe "|" from the end of the string
    $sItem = '"' & $sItem & '"' ; Add the mandatory quotation marks to pass to tzutil

    Global $result = MsgBox($MB_YESNO + $MB_ICONQUESTION + $MB_DEFBUTTON2 + $MB_SYSTEMMODAL, "Confirm Selection", "You selected " & $sItem & ", Is this Correct?", 0)
    If $result = 6 Then
        GUIDelete($listGUI) ; Delete the main GUI
        ConsoleWrite("RunWait" & @CRLF) ; RunWait("C:\Windows\System32\tzutil.exe /s " & $sItem)
        Exit ; Exit
    ElseIf $result = 7 Then
        GUISetState(@SW_SHOW, $listGUI) ; Show the main GUI again
    EndIf

EndFunc   ;==>SelectItem

Func On_Close_Main()
    Exit
EndFunc   ;==>On_Close_Main
That works perfectly for me. Please ask if you have any questions. :)

M23

P.S. When you post code please use Code tags - see here how to do it. Then you get a scrolling box and syntax colouring as you can in my code above. ;)

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

Vyperhand,

Melba's code works perfect for me as well.

^_^

Snips & Scripts


My Snips: graphCPUTemp ~ getENVvars
My Scripts: Short-Order Encrypter - message and file encryption V1.6.1 ~ AuPad - Notepad written entirely in AutoIt V1.9.4

Feel free to use any of my code for your own use.                                                                                                                                                           Forum FAQ

 

Link to comment
Share on other sites

  • Moderators

vyperhand,

Delighted to hear it. As I asked above: Do you understand why I made the changes I did? Please ask if not because I am quite happy to explain in more detail if necessary. :)

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

  • Moderators

vyperhand,

Here you go. :)

- 1. Declare Global variables in the main script. These variables are visible to every function in the script so declare them early to make sure that they are visible when needed. AutoIt does not recognise variables that have not yet been seen by the interpreter, so the declaration line has to have been parsed at run-time for the declaration to be valid - if the function has not yet been called then the variables within are not considered to have been declared as AutoIt cannot read your mind. Au3Check (the syntax checker) will throw a warning if you declare them inside a function, but will not error. The Variables - using Global, Local, Static and ByRef tutorial in the Wiki offers further information.

- 2. Idle loop in main script. While the script is inside a function AutoIt will not, in general, run another function - so putting the idle loop inside a function (meaning you never actually leave it) will prevent most other functions from running. Obviously this is not "a good thing" - you might find the Interrupting a running function tutorial in the Wiki useful reading to understand in even more detail.

- 3. Recursion. I mentioned that you were calling the MainGUI function from inside itself and that this would cause problems. Before you say "But I call the function from within the SelectItem function" I would point out that you actually call that function from within the MainGUI function so there is an unbroken chain. Calling functions again before they have finished is known as recursion (and yes there is a Wiki tutorial: Recursion) which can lead to major crashes. As explained above, you want to get out of functions cleanly so that AutoIt can run other ones without problem. In this case we just hide the initial choice GUI and then either reshow or destroy it depending on the MsgBox return - that way we do not get involved in recursion at all.

- 4. Magic numbers. Although it can appear to be a good idea to use a single integer to define the MsgBox parameters, will you remember what that value represents in a few days, weeks, months time when you next take a look at the code. Much better to use the constants which explain (pretty well) what is intended. There are a number of utilities available which can reduce the execution time of the code by replacing these constants with their numeric values before compiling - guinness' PreExpand is the best I have found.

I hope that helps. :)

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