Home

Message Boxes: The Input Query Request

   

Introduction

Like the InputBox() function, the InputQuery() function is used to display a prompting dialog box to the user. The syntax of this function is:

extern PACKAGE bool __fastcall InputQuery(const AnsiString ACaption,
                                          const AnsiString APrompt,
                                          AnsiString &Value);
 

This takes three strings. The ACaption parameter is a string that displays on the title bar of the dialog box. The APrompt parameter is the sentence that indicates to the user what to type in the edit box. Like the InputBox() function, the Value parameter provides a default and sample value to the user. Like the InputBox() function, the user can type a new value.

 

Here is an example of calling the InputQuery() function:

//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE hInstance,
		   HINSTANCE hPrevInstance,
		   LPSTR lpCmdLine,
		   int nCmdShow)
{
	AnsiString Value;

	InputQuery(L"Exiting Application",
			   L"Are you sure you want to exist (y=Yes/n=No)?",
			   Value);
	return 0;
}
//---------------------------------------------------------------------------

This would produce:

Unlike the InputBox() function that returns a string, the InputQuery() function returns two values. By its declaration, this function returns a Boolean value of true or false. If the user clicks OK or presses Enter after using the dialog box, the function returns true. If the user presses Esc or clicks Cancel, the function returns false.

If the user clicks OK (or presses Enter), like the InputBox() function, whether the user had changed the value of the text box or not, the content of the text box, provided as the Value argument, would be returned. Because the Value argument is passed by reference, the function can return two values. If the user clicks Cancel (or presses Esc) after dealing with the dialog box, the value of the Value argument would be ignored.

You can validate the returned Value by writing a conditional statement that examines whether the user had clicked OK or Cancel. In the following example, when the user clicks a button on the form, the compiler finds out if the user had clicked OK:

//---------------------------------------------------------------------------
WINAPI _tWinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
{
    AnsiString Answer;

    if( InputQuery(L"Exiting Application",
		   L"Are you sure you want to exist (Yes/No)?",
		   Answer) == True )
	ShowMessage("Your answer was " + Answer);
    return 0;
}
//---------------------------------------------------------------------------

Here is an example of running the program:

    
 

Home Copyright © 2010-2016, FunctionX