1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Donation with Paypal!!!

    Go to your paypal account and send directly donation to [email protected]

    1 month - 10 $ - Standart VIP

    6 months - 20 $- Standart VIP

    1 year - 30 $- Standart VIP

    2 years - 50 $- Standart VIP

    Gold member for life - 150 $- Standart VIP

    High Vip (Standart VIP include) group please send PM or email to [email protected] for info

    After Donation please send email to [email protected]

  3. Donation Ways 2020


    Paysend
  4. Telegram
Dismiss Notice

Donation with Paypal!!!

Go to your paypal account and send directly donation to [email protected]

1 month - 10 $ - Standart VIP

6 months - 20 $- Standart VIP

1 year - 30 $- Standart VIP

2 years - 50 $- Standart VIP

Gold member for life - 150 $- Standart VIP

High Vip (Standart VIP include) group please send PM or email to [email protected] for info

After Donation please send email to [email protected]

Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Build your own Dialogs in a snap

Discussion in 'Delphi Programming' started by AdminDF, Dec 5, 2013.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member Delphifan Staff Member DF Staff

    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 ?


    [​IMG]


    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:


    Code:
    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:


    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).
    Set the form's Caption to 'Please enter your username:' and set its BorderStyle property to bsDialog.
    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.
    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):


    Code:
    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:


    Code:
    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:


     
    Code:
    uses ..., ..., UserDlg;
     
  2. wozengcong
    Online

    wozengcong Guest

    It's very useful, Thanks!
     

Share This Page