Delphi Tutorials

Build your own Dialogs in a snap

So you want to design your own dialogs, that work just like the Delphi dialog components, such as a simplied version of Delphi's OpenDialog. But you don't want to develop a full blown component (with published properties and methods for creation and destroying, and more complicated stuff...) because that would be a bit overkill. And besides, most beginner and intermediate programmers are not to keen on developing components ;) ...hey, isn't Delphi about R.A.D., Rapid Application Development ?

Let's suppose that you want a simple dialogbox that looks like the one above. You want to call that dialog from your code just like you call Delphi's dialogs, for example call it from clicking Button1 on your main form:

procedure TFormMain.Button1Click(Sender: TObject);
  if UserDialog.Execute then
    Label1.Caption := UserDialog.UserName
    Label1.Caption := 'Error: no User ID entered';

Here's a quick and simple solution:

  1. Add a new form to your project and call it UserDialog. Save the new unit as UserDlg (or any other name that's not already used in your project).
  2. Set the form's Caption to 'Please enter your username:' and set its BorderStyle property to bsDialog.
  3. Add three components:
    - a TEdit with the name edUserName; clear its Text property.
    - a TBitButton with the name btnOK; set its property Kind to bkOK.
    - a TBitButton with the name btnCancel; set its property Kind to bkCancel.
  4. In the PUBLIC section of your unit, declare a string variable named UserName.
    Below that line, declare a function with the name Execute, that returns a Boolean value.

So far, most of the interface section is written by Delphi itself, you only added the two short lines from step 4! (shown in bold):

unit UserDlg;
  Windows, Messages, SysUtils, Classes, Graphics, Controls,
  Forms, Dialogs, StdCtrls, Buttons;
  TUserDialog = class(TForm)
  edUserName: TEdit;
  btnOK: TBitBtn;
  btnCancel: TBitBtn;
  { Private declarations }
  { Public declarations }
  UserName: string;          // added by you   
  function Execute: Boolean; // added by you
  UserDialog: TUserDialog;

Next, let's have a look at what you add to the implementation section -- it's surprisingly short:

function TUserDialog.Execute: Boolean;
  Result := (ShowModal = mrOK);
  UserName := edUserName.Text;

That's all!

How does Delphi's magic work?

The function Execute shows the UserDialog form in a "modal" way, that means: the window will open on top of the other window(s) and the application does not continue before UserDialog is closed.
If the user clicks the OK button, the dialog is automatically closed and our Execute function returns True. When the Cancel button is clicked, the dialog also closes, but we receive False. Also when the UserDialog is closed in another way, we get False.
Finally, the text of the edit-box is copied to the public variable UserName, from where other units can access it.

Note: don't forget to add the unit UserDlg to the uses directive in each unit from where you call UserDialog, like this:

  uses ..., ..., UserDlg;


TOP    DC Library  FAQ  Crash Course Delphi  Tips  Source Code  Downloads  Links

Copyright 1999-2019