Jump to content

Suspend script execution in an If...Else...EndIf statement?


Recommended Posts

Hi guys! How are you? Hope you're fine! :) I'm working with GUI's ( especially forms ), and I would like to know what I have to do to suspend the execution of my script If the user didn't enter anything in a form's field? 
Something like:
 

If($sAddDescrizioneProdotto = "") Then
    MsgBox($MB_ICONERROR, "Errore!", "Inserisci una 'Descrizione Prodotto' per continuare.", 3)
    ; Here, I need to stop the execution of the script, setting the focus on the input "missed"
    GUICtrlSetState($edit_Descrizione, $GUI_FOCUS)
Else
    _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,$sAddDescrizioneProdotto, "E" & $iIndiceRigaVuota)            ; Descrizione
EndIf

How can I do it? :D Thanks :) 
 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Depending on what you're doing, most functions have an @error flag that will be set if something goes wrong. For example you can do:

Func browse() ;browse
    Global $listPath = FileOpenDialog("Choose a list file", @ScriptDir, "Excel Spreadsheet (*.xlsx;*.xls)", 1, "list.xlsx")
    If @error Then
        MsgBox(1, "Error", "Error with input file, try again.")
    Else
        GUICtrlSetData($Input1, $listPath)
    EndIf
EndFunc   ;==>browse

If you look at the documentation for a function you can see what it sets its @error to.

And if you're doing something like empty checking, just doing:

If (GUICtrlRead($Input1) == "") Then
        MsgBox(1, "Error", "No number was entered.")
    EndIf

should suffice.

Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

Just now, anthonyjr2 said:

Depending on what you're doing, most functions have an @error flag that will be set if something goes wrong. For example you can do:

Func browse() ;browse
    Global $listPath = FileOpenDialog("Choose a list file", @ScriptDir, "Excel Spreadsheet (*.xlsx;*.xls)", 1, "list.xlsx")
    If @error Then
        MsgBox(1, "Error", "Error with input file, try again.")
    Else
        GUICtrlSetData($Input1, $listPath)
    EndIf
EndFunc   ;==>browse

If you look at the documentation for a function you can see what it sets its @error to.

Hi @anthonyjr2!
I'm working with GUI, so, the only "tool" I have for read from a GUI is 
 

GUICtrlRead()

which returns 0 if failure...
So... :D 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

You could use a Case in your gui for example:

#include <Excel.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $iIndiceRigaVuota = 1
    Local $oExcel = _Excel_Open()
    Local $oWorkbook = _Excel_BookNew($oExcel)
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 65)
    Local $sAddDescrizioneProdotto = GUICtrlCreateInput('', 5, 10, 290, 20)
    Local $idOK = GUICtrlCreateButton("OK", 190, 35, 100, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idOK
                If GUICtrlRead($sAddDescrizioneProdotto) = '' Then
                    GUICtrlSetState($sAddDescrizioneProdotto, $GUI_FOCUS)
                    ContinueLoop
                EndIf
                _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,GUICtrlRead($sAddDescrizioneProdotto), "E" & $iIndiceRigaVuota)
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

 

Link to comment
Share on other sites

I'm not sure if you saw my edit, but that is what you could do to check if the input is empty. The way I do it in my scripts is that my checking is in a separate function, so I can call my browse() function that I posted earlier, and if it fails it will just return back to the main GUI and wait for the next action. So instead of halting the entire program, the user can just try to browse again.

EDIT: A Case statement as Subz just posted is also a viable option.

Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

6 minutes ago, Subz said:

You could use a Case in your gui for example:

#include <Excel.au3>
#include <GUIConstantsEx.au3>

Example()

Func Example()
    Local $iIndiceRigaVuota = 1
    Local $oExcel = _Excel_Open()
    Local $oWorkbook = _Excel_BookNew($oExcel)
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 65)
    Local $sAddDescrizioneProdotto = GUICtrlCreateInput('', 5, 10, 290, 20)
    Local $idOK = GUICtrlCreateButton("OK", 190, 35, 100, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idOK
                If GUICtrlRead($sAddDescrizioneProdotto) = '' Then
                    GUICtrlSetState($sAddDescrizioneProdotto, $GUI_FOCUS)
                    ContinueLoop
                EndIf
                _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,GUICtrlRead($sAddDescrizioneProdotto), "E" & $iIndiceRigaVuota)
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

 

Thanks for the reply @Subz :)
I manage different GUI's, and, for doing this, I need to call a lot of function ( I like to divide the program in more of them... ), and, I can't adapt your script to what I'd like to develop in my script. 
I have something like this:
 

Case $form_AggiungiProdotto
                Switch $aMsg[0]
                    Case $GUI_EVENT_CLOSE
                        GUICtrlSetData($input_CodiceProdottoPrelievo, "")
                        GUIDelete($form_AggiungiProdotto)
                    Case $button_MagazzinoRockwell
                        $sFlagMagazzino = "ROCKWELL"
                        GUICtrlSetData($input_MagazzinoSelezionato, "ROCKWELL")
                        GUICtrlSetState($input_CodiceProdottoAggiunta, $GUI_FOCUS)
                    Case $button_MagazzinoSiemens
                        $sFlagMagazzino = "SIEMENS"
                        GUICtrlSetData($input_MagazzinoSelezionato, "SIEMENS")
                        GUICtrlSetState($input_CodiceProdottoAggiunta, $GUI_FOCUS)
                EndSwitch

This is one of my form... All the GUICtrlRead are managed in another function... So, how can I proceed in this case? I'd move my GUICtrlRead in "global" scope, and then pass them to the function that does the _Excel_RangeWrite()... Am I wrong? Thanks for your reply :) 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

12 minutes ago, anthonyjr2 said:

I'm not sure if you saw my edit, but that is what you could do to check if the input is empty. The way I do it in my scripts is that my checking is in a separate function, so I can call my browse() function that I posted earlier, and if it fails it will just return back to the main GUI and wait for the next action. So instead of halting the entire program, the user can just try to browse again.

EDIT: A Case statement as Subz just posted is also a viable option.

I saw it, but maybe you don't read that I'm working with GUICtrlRead, so, I don't have any @error flag set by the function GUICtrlRead. Your solution is good, but it's not applicable on my script :) 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

Yes that is correct something like this, similar to anthonyjr2 first post except using GuiCtrlRead.

#include <Excel.au3>
#include <GUIConstantsEx.au3>

Opt('ExpandVarStrings', 1)

    Global $_XLS_ProductCode, $_XLS_ProductBrand, $_XLS_ProductQuantity ;~ Added to Global Scope
    Global $oExcel = _Excel_Open()
    Global $oWorkbook = _Excel_BookNew($oExcel)

Example()

Func Example()
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate('Example', 300, 135)
    $_XLS_ProductCode = GUICtrlCreateInput('Product Code', 5, 10, 290, 20)
    $_XLS_ProductBrand = GUICtrlCreateInput('Product Brand', 5, 40, 290, 20)
    $_XLS_ProductQuantity = GUICtrlCreateInput('Product Quantity', 5, 70, 290, 20)
    Local $idOK = GUICtrlCreateButton('Insert', 190, 100, 100, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idOK
                _XLS_PRODUCT($_XLS_ProductCode, GUICtrlRead($_XLS_ProductCode), 'A', 1) ;~ Added Function
                _XLS_PRODUCT($_XLS_ProductBrand, GUICtrlRead($_XLS_ProductBrand), 'B', 1) ;~ Added Function
                _XLS_PRODUCT($_XLS_ProductQuantity, GUICtrlRead($_XLS_ProductQuantity), 'C', 1) ;~ Added Function
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _XLS_PRODUCT($hProduct, $sProduct, $sCOL = 'A', $iCOL = 1)
    If $sProduct = '' Then
        GUICtrlSetState($hProduct, $GUI_FOCUS)
        Return
    EndIf
    _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $sProduct, '$sCOL$$iCOL$')
EndFunc

 

Edited by Subz
Link to comment
Share on other sites

@FrancescoDiMuro I meant the second half of my post, but no worries maybe I can try to explain it better here.

Func checkBlank()
  If(GuiCtrlRead($sAddDescrizioneProdotto) = "") Then
      MsgBox($MB_ICONERROR, "Errore!", "Inserisci una 'Descrizione Prodotto' per continuare.", 3)
      ; Here, I need to stop the execution of the script, setting the focus on the input "missed"
      GUICtrlSetState($edit_Descrizione, $GUI_FOCUS)
  Else
      _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,$sAddDescrizioneProdotto, "E" & $iIndiceRigaVuota)            ; Descrizione
  EndIf
EndFunc

I meant to wrap this into a function and then call it. If the error happens, the function will just return to the GUI and it can be called again. So effectively you can "halt" after the error but you will still be able to do things again, such as fix the empty input box.

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

7 minutes ago, Subz said:

Yes that is correct something like this, similar to anthonyjr2 first post except using GuiCtrlRead.

#include <Excel.au3>
#include <GUIConstantsEx.au3>
Global $sAddDescrizioneProdotto ;~ Added to Global Scope

Example()

Func Example()
    Local $iIndiceRigaVuota = 1
    Local $oExcel = _Excel_Open()
    Local $oWorkbook = _Excel_BookNew($oExcel)
    ; Create a GUI with various controls.
    Local $hGUI = GUICreate("Example", 300, 65)
    $sAddDescrizioneProdotto = GUICtrlCreateInput('', 5, 10, 290, 20) ;~ Removed Local Scope
    Local $idOK = GUICtrlCreateButton("OK", 190, 35, 100, 25)

    ; Display the GUI.
    GUISetState(@SW_SHOW, $hGUI)

    ; Loop until the user exits.
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $idOK
                sAddDescrizioneProdotto() ;~ Added Function
        EndSwitch
    WEnd

    ; Delete the previous GUI and all controls.
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func sAddDescrizioneProdotto()
    If GUICtrlRead($sAddDescrizioneProdotto) = '' Then
        GUICtrlSetState($sAddDescrizioneProdotto, $GUI_FOCUS)
        Return
    EndIf
    _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,GUICtrlRead($sAddDescrizioneProdotto), "E" & $iIndiceRigaVuota)
EndFunc

 

Or, something like:
 

Global $sAddDescrizioneProdotto = GUICtrlRead(...) ; Here I read the input 
; Forms...
    Case $button_Ok
        If($sAddDescrizioneProdotto = "") Then
            MsgBox($MB_ICONERROR, "Error!", "Error...")
            GUICtrlSetFocus(...)
            ContinueLoop ; >>>>> I've never really understood what this function does!
    EndSwitch

In this case, the script will stop always until the user prompt something in the input? Thanks :) 
 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

40 minutes ago, anthonyjr2 said:

Depending on what you're doing, most functions have an @error flag that will be set if something goes wrong. For example you can do:

Func browse() ;browse
    Global $listPath = FileOpenDialog("Choose a list file", @ScriptDir, "Excel Spreadsheet (*.xlsx;*.xls)", 1, "list.xlsx")
    If @error Then
        MsgBox(1, "Error", "Error with input file, try again.")
    Else
        GUICtrlSetData($Input1, $listPath)
    EndIf
EndFunc   ;==>browse

If you look at the documentation for a function you can see what it sets its @error to.

And if you're doing something like empty checking, just doing:

If (GUICtrlRead($Input1) == "") Then
        MsgBox(1, "Error", "No number was entered.")
    EndIf

should suffice.

Thanks for the reply :) I still missing the concept you're trying to explain to me... I'm not doing just error checking, but, stop the script if the user didn't prompt anything in a/some field/s... 
I.E.: You didn't prompt your name? Something, will bring you always at your name until you don't prompt some text in the 'Name' field.
Thanks :) 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

3 minutes ago, FrancescoDiMuro said:

Thanks for the reply :) I still missing the concept you're trying to explain to me... I'm not doing just error checking, but, stop the script if the user didn't prompt anything in a/some field/s... 
I.E.: You didn't prompt your name? Something, will bring you always at your name until you don't prompt some text in the 'Name' field.
Thanks :) 

 

17 minutes ago, anthonyjr2 said:

@FrancescoDiMuro I meant the second half of my post, but no worries maybe I can try to explain it better here.

Func checkBlank()
  If(GuiCtrlRead($sAddDescrizioneProdotto) = "") Then
      MsgBox($MB_ICONERROR, "Errore!", "Inserisci una 'Descrizione Prodotto' per continuare.", 3)
      ; Here, I need to stop the execution of the script, setting the focus on the input "missed"
      GUICtrlSetState($edit_Descrizione, $GUI_FOCUS)
  Else
      _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,$sAddDescrizioneProdotto, "E" & $iIndiceRigaVuota)            ; Descrizione
  EndIf
EndFunc

I meant to wrap this into a function and then call it. If the error happens, the function will just return to the GUI and it can be called again. So effectively you can "halt" after the error but you will still be able to do things again, such as fix the empty input box.

I just posted it here for you. If you keep calling this function on a button press or loop it will not let you continue until you have something in the input box.

Edited by anthonyjr2

UHJvZmVzc2lvbmFsIENvbXB1dGVyZXI=

Link to comment
Share on other sites

You should only use the Global scope if you're using that variable across multiple functions, if you're using it within just one function it should be Local scope.  So you'll notice in Post#10 that I make the variable Global before the Gui function, I can then use this variable in the second function.  If you can post all your code we can look over it and give you pointers.

Link to comment
Share on other sites

2 minutes ago, anthonyjr2 said:

 

I just posted it here for you. If you keep calling this function on a button press or loop it will not let you continue until you have something in the input box.

Oh, thanks :) As I mentioned above, I call several times ( with different parameters ), the function _Excel_RangeWrite() in another function... So, I have to find the way to do a "check" passage in the function or outside, and then, do a "write" passage... I don't know if I've been clear now :) Thanks for your help :) 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

2 minutes ago, Subz said:

You should only use the Global scope if you're using that variable across multiple functions, if you're using it within just one function it should be Local scope.  So you'll notice in Post#10 that I make the variable Global before the Gui function, I can then use this variable in the second function.  If you can post all your code we can look over it and give you pointers.

I know right @Subz! Indeed, I have a lot of global variables that I use in several function :)

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

The code I'm using is covered by my company copyright, so I can't post the whole code, and do an example would be a lot of work to do... Sorry :/ But, I'll try your scripts... If they will work, I'd be very happy to notice you guys! :) If you have any suggestion, I am here :D Thanks for your time guys! 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

Link to comment
Share on other sites

17 hours ago, anthonyjr2 said:

@FrancescoDiMuro I meant the second half of my post, but no worries maybe I can try to explain it better here.

Func checkBlank()
  If(GuiCtrlRead($sAddDescrizioneProdotto) = "") Then
      MsgBox($MB_ICONERROR, "Errore!", "Inserisci una 'Descrizione Prodotto' per continuare.", 3)
      ; Here, I need to stop the execution of the script, setting the focus on the input "missed"
      GUICtrlSetState($edit_Descrizione, $GUI_FOCUS)
  Else
      _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet,$sAddDescrizioneProdotto, "E" & $iIndiceRigaVuota)            ; Descrizione
  EndIf
EndFunc

I meant to wrap this into a function and then call it. If the error happens, the function will just return to the GUI and it can be called again. So effectively you can "halt" after the error but you will still be able to do things again, such as fix the empty input box.

Good morning :)
I did this, but seems to not work properly...
 

Func ControllaInserimento($sParametro)
    If($sParametro = "") Then
        MsgBox($MB_ICONERROR, "Errore!", "Inserisci un 'Codice Prodotto' per continuare.", 3)
        GUICtrlSetState($input_Codice, $GUI_FOCUS)
        Return False
    Else
        Return True
    EndIf
EndFunc

When I call the function, I pass as parameter, the variable I memorize the data in, read from GUICtrlRead()...
In another function, I do something like this... But seems to not work...
 

If(ControllaInserimento($sAddCodiceProdotto)) Then
                MsgBox($MB_ICONERROR, "Errore!", "Inserisci un 'Codice Prodotto' per continuare.", 3)
                GUICtrlSetState($input_Codice, $GUI_FOCUS)
            Else
                _Excel_RangeWrite($oWorkbook, $oWorkbook.ActiveSheet, $sAddCodiceProdotto, "A" & $iIndiceRigaVuota)                ; Codice Prodotto
            EndIf

Where am I wrong? Thanks :) 

Click here to see my signature:

Spoiler

ALWAYS GOOD TO READ:

 

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