procedure FormShow(Sender: TObject);
begin
  //自定义的代码
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
  Form2 := TForm.Create(nil);
  Form2.Caption := 'Hello Caption';
  Form2.BorderStyle := bsDialog;
  Form2.Position := poScreenCenter;
  Form2.OnShow := //问题就在这里,如何让动态创建的窗口Form2,在Show的时候调用上面的FormShow函数?  Form2.ShowModal;
  Form2.Free;
end;

解决方案 »

  1.   

    直接用一个过程不行么?或者用某个按钮事件: form2.onshow:=bitbtn3.onclick(bitbtn3)
      

  2.   

    procedure FormShow(Sender: TObject); 
    begin 
      //自定义的代码 
    end; 
    把这个过程要定义到类里面。不能是一般普通的过程。
    *************************************
    When you are creating components at run time, and want to assign a procedure to an event handler of a component, like in the following code: 
    procedure ButtonClick(Sender:TObject) ;
    begin
    //whatever
    end;procedure SomeProcedure...
    var aButton:TButton;
    begin
       aButton := TButton.Create(nil) ;
       aButton.OnClick = ButtonClick
    ...
    end;You will get the next error:
    Incompatible type: 'method pointer and regular procedure'.The problem is that a method pointer (OnClick) needs to be a procedure of an object (like TForm), not a regular procedure.One way around is to create an object to define the procedure. You can start by creating a dummy class...interfacetype
       TEventHandlers = class // create a dummy class
           procedure ButtonClick(Sender: TObject) ;
       end;...var EvHandler:TEventHandlers;implementationprocedure TEventHandlers.ButtonClick(Sender: TObject) ;
    begin
       // your code here
    end;...
    procedure SomeProcedure...
    var aButton:TButton;
    begin
       aButton := TButton.Create(nil) ;
       aButton.OnClick = ButtonClick;
    ...
    end;This compiles!
    }
      

  3.   

    procedure TForm1.Button1Click(Sender: TObject);
    楼主的代码编译通过,未做测试
    begin
      Form2 := TForm.Create(nil);
      Form2.Caption := 'Hello Caption';
      Form2.BorderStyle := bsDialog;
      Form2.Position := poScreenCenter;
      Form2.OnShow := Form2_Show;//问题就在这里,如何让动态创建的窗口Form2,在Show的时候调用上面的FormShow函数?  Form2.ShowModal;
      Form2.Free;
    end;procedure TForm1.Form2_Show(Sender: TObject);
    begin
      showmessage((Sender as TForm).Caption);
    end;
      

  4.   

    按照onShow的代码自己写一个过程就可以了
      

  5.   

    1. 自定义一个窗口基类
    -----------------------
    unit Unit2;interfaceuses
      Forms, Classes, SysUtils;type
      TMyForm = class(TForm)
      private
        FMyShow: TNotifyEvent;
      protected
        procedure DoShow; override; //重载TForm类中的DoShow方法
      published
        property OnMyShow: TNotifyEvent read FMyShow write FMyShow;
      end;implementation{ TMyForm }procedure TMyForm.DoShow;
    begin
      inherited;
      if Assigned(FMyShow) then FMyShow(self);
    end;end.2. Form2从TMyForm继承下来3. Form1中创建Form2procedure FormShow(Sender: TObject); 
    begin 
      //自定义的代码 
    end; 
    procedure TForm1.Button1Click(Sender: TObject); 
    begin 
      Form2 := TForm.Create(nil); 
      Form2.Caption := 'Hello Caption'; 
      Form2.BorderStyle := bsDialog; 
      Form2.Position := poScreenCenter;
      Form2.OnMyShow := FormShow; //事件赋值
      Form2.ShowModal; 
      Form2.Free; 
    end;
      

  6.   

    Form2.OnMyShow := FormShow; //事件赋值 
    [Error] Unit1.pas(38): Undeclared identifier: 'OnMyShow'
    这句报错
    已经引用unit2
      

  7.   

    type
      TForm2 = class(TForm)修改成type
      TForm2 = class(TMyForm)
      

  8.   

    Form2.OnShow := tb.click;  //这样就可以了的;将你的代码写在一个按钮的点击事件里