Wrapping the call to MessageBox()



The MessageBox() function 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'.

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 .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 MsgBox because you can embed it into an 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 - we have written a "Confirm" function in both languages.

Back to FoxPro Developers' page

Hints & tips

The textbox class in Visual FoxPro 9 has a new Autocomplete property which shows the user the previous values that have been entered in that textbox.
Autocomplete in VFP 9

Your Access database will look more impressive if you add custom toolbars...
Custom toolbars

FoxPro has always had functions to read and write files at a low level...
Foxpro low level file functions

More...
More pages of hints and tips for users of Microsoft FoxPro and Access databases.

Site Map