In a previous blog we used the Note statement, which is the most basic of MapInfo’s standard dialogs. The Help section on Dialogs states: - “The Dialog statement lets you create a custom dialog box. If you want to display a standard dialog box (for example, a File>Open dialog box), use one of the following statements or functions: Ask() function, Note statement, ProgressBar statement, FileOpenDlg() function, FileSaveAsDlg() function, or GetSeamlessSheet() function).”
The Ask() function displays a dialog box which asks the user a yes or no question. The example in the Help section is as follows:-
Dim more As Logical
more = Ask("Do you want to continue?", "OK", "Stop")
This code by itself will not show an Ask dialogue box so please do not try and run this example. This is because it will cause MapInfo to run the program in the background without displaying the Ask dialogue box. The code will compile and can be run, but with the result of having to reboot your computer to cancel the code from the system. If curiosity gets the better of you and you run this code you will only be aware of the problem as the MapInfo icon at the bottom of your computer screen will flash intermittently. If you attempt to close MapInfo you will get a message saying that you cannot quit MapInfo. You can resolve the problem by restarting which will close MapInfo and the underlying problem. Needless to say you are better off just accepting that this code is to be run only as part of a larger section of code as shown in the code sample below:-
'** Ask Function Example
'**
'** Author: Joe Short
'** Date: 12th December 2016
'** Version: AppVersion (see below)
'** Example created with the help of code developed by Egge-Jan Pollé
'************************************************************************************
Include "MAPBASIC.DEF"
Include "ICONS.DEF"
Include "MENU.DEF"
Declare Sub Main
Declare Sub About
Declare Sub End_Program
Define AppName "Ask Function Example"
Define AppVersion 1.0
'Defining Dialog Width and Height
'This allows you to specify width and height clauses in terms of characters (i.e., Width 30dW, Height 10dH).
Define dW *4 'Four dialog units equals one character in width
Define dH *8 'Eight dialog units equals one character in height
'*********************************************************************************************
Sub Main
'*********************************************************************************************
Call About
End Sub Main
'*********************************************************************************************
Sub About
'*********************************************************************************************
Dialog
Title "About " + AppName + " (Version " + AppVersion + ")"
Control StaticText title "This simple application demonstrates how the Ask function works." Position 3dW, 1dH
Control StaticText title "To see how it works:" Position 3dW, 3dH
Control StaticText title "- Look at the Sub End_Program section of the code" Position 3dW, 4dH
Control StaticText title " You have the option of closing the application " Position 3dW, 5dH
Control StaticText title "If you select Cancel the program closes" Position 3dW, 7dH
Control StaticText title "If you select Exit the application will display the Ask dialogue box" Position 3dW, 8dH
Control StaticText title "This gives you the option of cancelling or continuing the program" Position 3dW, 9dH
Control StaticText title "This is a simple example of how to use the Ask() function" Position 3dW, 10dH
Control StaticText title Chr$(169) + " 2016 - Joe Short" Position 50dW, 14dH
Control OKButton Title "&Exit" Calling End_Program
Control CancelButton
End Sub About
'*********************************************************************************************
Sub End_Program
'*********************************************************************************************
Dim Finished as Logical
Finished = Ask("Are you sure you want to remove the application " + AppName + "?", "&Yes", "&No")
If not Finished then Dialog Preserve
Else
End Program
End if
End Sub End_Program
'*********************************************************************************************
If you click on the Exit button the Ask dialogue box appears as below:-
If you look up the Dialog Preserve statement in MapBasic Help the following code sample is shown:-
Sub confirm_cancel
If Ask("Do you really want to lose your changes?",
"Yes", "No" ) = FALSE Then
Dialog Preserve
End If
End Sub
This code checks to see if the user wants to lose any changes. If the response is No (FALSE) then the Dialog Preserve statement reactivates the original dialogue box. For more information on Dialog Preserve look up this option in the MapBasic Help facility. For more information on customising dialogue boxes check out the Dialog statement in MapBasic Help.