Wrapping the call to MessageBox()

The MessageBox() function in Visual FoxPro lets you display a standard Windows dialog to get a Yes/No answer from the user. The dialog is reliable and familiar but the call to this function can be a bit verbose:

If MessageBox ("Are you sure",
  MB_ICONQUESTION + MB_YESNO,
  "Delete record") = IDYES ...

This call then gives you a return value of 6 or 7 so you need to write further code to determine whether the user said 'Yes' or 'No'. The task is not difficult but the extra parameters do make the code look messy.

You can make life a lot easier by writing a function to wrap the MessageBox call. We use a function named "Confirm" which takes the text and title as parameters and converts the numeric return from MessageBox into the True or False values .T. or .F..

The body of the function is very simple:

Function Confirm
Lparameter tcText, tcTitle

lnResponse = Messagebox(tcText, ;
  MB_ICONQUESTION + MB_YESNO, ;
  tcTitle)

Return lnResponse = IDYES

EndFunc

The code to use this function is much easier to write than the call to the MessageBox function because you can embed it into an If statement:

If Confirm("Are you sure", "Delete record") ...

A more sophisticated function

Our basic Confirm() function has grown more sophisticated over the years. The second parameter is now optional and the function uses PCount() to count the number of parameters received. If there is only one parameter then it defaults to using the screen caption as the title for the MessageBox dialog. This makes calling the function even simpler.

An additional bonus in our work is that we have no longer have to remember the different calling conventions for the FoxPro and Access messageboxes.

Both languages use the standard Windows dialog and both require a parameter of 36 and will return 6 or 7 as the result. The difference is that Access calls it with the MsgBox() function rather than MessageBox() and has constants named vbYes and vbQuestion corresponding to IDYES and MB_ICONQUESTION in Visual FoxPro. We've now got a "Confirm()" function written in both languages so we don't have to worry about these annoying differences any more.