Delphi Basics
InputQuery
Function
Display a dialog that asks for user text input Dialogs unit
 function InputQuery ( const Caption, Prompt : string; var UserValue : string ) : Boolean;
Description
The InputQuery function displays a simple dialog box with the given Caption and Prompt message. It asks the user to enter data in a text box on the dialog.
 
If the user presses OK, the entered data is stored in the UserValue variable and the return value is True.
 
If the user cancels the dialog, then the return value is False and any entered data is lost.
 
Use to ask the user for simple data such as a name.
Related commands
InputBox Display a dialog that asks for user text input, with default
PromptForFileName Shows a dialog allowing the user to select a file
ShowMessage Display a string in a simple dialog with an OK button
ShowMessageFmt Display formatted data in a simple dialog with an OK button
ShowMessagePos Display a string in a simple dialog at a given screen position
 
Example code : Keep asking the user for their name until they do so
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  value : string;

begin
  // Keep asking the user for their name
  repeat
    if not InputQuery('Test program', 'Please type your name', value)
    then ShowMessage('User cancelled the dialog');
  until value <> '';

  // Show their name
  ShowMessage('Hello '+value);
end;
 
end.
Hide full unit code
   A dialog is displayed asking for the user name.
   If the user types their name, 'Fred' and hits OK :
  
   Hello Fred    
  
   is then displayed
 
 
© CodeGear 2006 - 2007. All rights reserved.  |  Home Page