Hi guys, I need to create one dynamic form and I need to place one button and assign onclick event to that button, how to I assign that event in my coding.

Hi guys, I need to create one dynamic form and I need to place one button and assign onclick event to that button, how to I assign that event in my coding. 



unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.DBGrids, Data.DB,Bde.DBTables,
  Vcl.grids;

function Dynamicform(OwnerFrom : TComponent;DetailName : string;parentcom : TWinControl):Boolean;

implementation


function Dynamicform(OwnerFrom : TComponent;DetailName : string;parentcom : TWinControl):Boolean;
var FDetailForm : TForm;
    dButton : TBitBtn;
begin
  with OwnerFrom do
  begin
     if not Assigned(TForm(OwnerFrom.FindComponent(DetailName))) then
     begin
        TForm.Create(OwnerFrom).Name := DetailName;
        FDetailForm := TForm(OwnerFrom.FindComponent(DetailName));
        with FDetailForm do
        begin
          Position := poOwnerFormCenter;
          BorderIcons := [biSystemMenu];
          Width := 400;
          Height := 600;
          TBitBtn.Create(FDetailForm).Name := 'BB'+DetailName;
          dButton := TBitBtn(FindComponent('BB'+DetailName));
          with dButton do
          begin
            Parent := FDetailForm;
            Align := alBottom;
            Caption := '&Okay';
            //how can assign procedre
            OnClick := FORMCLOSE(dButton);
            //how can assign procedre
          end;
          FDetailForm.ShowModal;
        end;
     end;
  end;
end;


end.

Comments

  1. Hi, I got it, Now My dynamic form is shown and when I click that button I got a message and my dynamic form closed; 

    thank you. 


    unit Unit1;

    interface

    uses
      Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
      Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Buttons, Vcl.DBGrids, Data.DB,Bde.DBTables,
      Vcl.grids;


    type

      TVIEWMODEL = CLASS
      Public
          TESTFORM : TForm;
          PROCEDURE FROMCLO(SENDER : TObject);
       END;


    function Dynamicform(OwnerFrom : TComponent;DetailName : string;parentcom : TWinControl):Boolean;

    implementation

    function Dynamicform(OwnerFrom : TComponent;DetailName : string;parentcom : TWinControl):Boolean;
    var FDetailForm : TForm;
        dButton : TBitBtn;
        test : TVIEWMODEL;
    begin
      with OwnerFrom do
      begin
         test := TVIEWMODEL.Create;
         try
         if not Assigned(TForm(OwnerFrom.FindComponent(DetailName))) then
         begin
            TForm.Create(OwnerFrom).Name := DetailName;
            FDetailForm := TForm(OwnerFrom.FindComponent(DetailName));
            with FDetailForm do
            begin
              Position := poOwnerFormCenter;
              BorderIcons := [biSystemMenu];
              Width := 400;
              Height := 600;
              TBitBtn.Create(FDetailForm).Name := 'BB'+DetailName;
              dButton := TBitBtn(FindComponent('BB'+DetailName));
              with dButton do
              begin
                Parent := FDetailForm;
                Align := alBottom;
                Caption := '&Okay';
       test.TESTFORM := FDetailForm;
                OnClick := test.FROMCLO;
              end;
              FDetailForm.ShowModal;
            end;
         end;
         finally
            FreeAndNil(test);
         end; 
      end;
    end;



    { TVIEWMODEL }

    procedure TVIEWMODEL.FROMCLO(SENDER: TObject);
    begin
       ShowMessage('Sucess');
       TESTFORM.Close;
    end;

    end.

    ReplyDelete
  2. Too many nested 'with' statements is dangerous, and is probably why your original code didn't work properly.

    Just out of curiosity, why do you create components anonymously then use FindComponent, rather than just assign newly created components to a temp variable? If the Create failed for some reason, the use of the .Name property for assignment would throw an exception and it would be very misleading. Especially because it's extremely difficult to debug code inside of 'with' statements, particularly when they're nested like this.

    Some people call this a "stylistic expression". After many, many years of spending untold hours tracking down bugs in code like this, I'd say it's just very dangerous. If your maintenance people down the line work for free, then fine. But you really need to write code that's not going to make maintenance work harder than it needs to be. I don't care if you're just "playing around" here. You should use the same style in your sandbox as you use in real programs.

    ReplyDelete
  3. David Schwartz +1. I said many of the same things in the linked thread (the question was double-posted in two Delphi groups.)  I think you phrased it better though.

    I hope Gowrisankar Sengottuvel listens to one of us!

    ReplyDelete

Post a Comment