Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
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 Administrator 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
    Offline

    wozengcong Guest

    It's very useful, Thanks!
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block