Message Boxes: The Input Box



The InputBox() function allows you to display a message box that would request a piece of information from the user. The message box is equipped with a text box and two buttons. The edit box accepts a string from the user.

The user can type anything but it is up to you to use the content of that edit box as your program needs it. When the user clicks OK, the Input Box returns the content of its text box. If the user clicks Cancel or presses Esc, the content of its edit box is dismissed.

The syntax of the InputBox() function is:


AnsiString __fastcall InputBox(const AnsiString Caption,
			       const AnsiString Prompt,
			       const AnsiString Default);

The Caption argument specifies the string that would display on the title bar of the dialog box. The Prompt is a sentence that would display to the user as to what to type in the provided text box. The Default argument is a suggested value you can display in the edit box to guide the user as the type of value expected. If you do not want to specify a default value, you can set its string value to empty. Here is example:

WINAPI _tWinMain(HINSTANCE hInstance,
				   HINSTANCE hPrevInstance,
				   LPSTR lpCmdLine,
				   int nCmdShow)
	InputBox(L"Distance and Measurement",
		 L"Enter the distance in kilometer:", L"");
	return 0;

This would produce:

If you specify the Default argument and the user clicks OK without changing the content of the edit box, the compiler would consider the default value as valid. After using the dialog box, the user would click OK, press Enter, click Cancel, or press Esc. If the user clicks OK or presses Enter, the function returns the value that the user would have typed in the edit box. This allows you to write a conditional statement that would consider the new value returned by clicking OK on the InputBox dialog. If the user clicks Cancel or presses Esc, whatever the text box was displaying would be ignored. But the function would still return the default value. If the returned value is intended for mathematical, date, or time calculations, you should convert it appropriately.


Home Copyright © 2010-2011 FunctionX, Inc.