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);
begin
  if UserDialog.Execute then
    Label1.Caption := UserDialog.UserName
  else
    Label1.Caption := 'Error: no User ID entered';
end;

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;
interface
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons;
type
  TUserDialog = class(TForm)
  edUserName: TEdit;
  btnOK: TBitBtn;
  btnCancel: TBitBtn;
  private
  { Private declarations }
  public
  { Public declarations }
  UserName: string;          // added by you   
  function Execute: Boolean; // added by you
  end;
var
  UserDialog: TUserDialog;

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

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

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;

 

Crash Course Delphi Become member of the DelphiLand Club and get our Crash Course Delphi, plus the fully commented source code of numerous projects, plus guaranteed answers from our Q/A Forum. Membership is for life!

TOP   DelphiLand Club Membership  DC Library  Forum  Forum Archives  Crash Course Delphi  Tips  Source Code  Downloads  Links

© Copyright 1999-2016 
Studiebureau Festraets