有三个窗体,form1,form2,form3,用form1调用form2,form3
form2中有两个按钮 :button1,button2 ;有一个方法:button1click
我将form3创建为form2的子类,然后在form3中覆盖button1click方法成功
但是我在form3中创建button2click方法:没有语法错误,但是没有效果
如果我在form2中建一个button2click方法,仅仅加一个分号,form3中的button2click有效
  那么如果我不在form2中加button2click,在form3中实现button2click要如何做
  或者,出现这样的效果,是有什么的理由的吗??《我没有学懂这条理由不算,呵呵!》

解决方案 »

  1.   

    我不知道你是怎么继承的你看看我的unit Unit2;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Unit1, StdCtrls;
    type
      TForm2= class(TForm1)
        button1:TButton;
        button2:TButton;
        procedure Button1click(sender:TObject);
      public
        constructor Create(AOwner:TComponent);override;
      end;implementation{ TForm2 }procedure TForm2.Button1click(sender: TObject);
    begin
      showmessage('button1 click');
    end;constructor TForm2.Create(AOwner: TComponent);
    begin
      inherited;
      button1:=TButton.Create(self);
      button1.Parent:=self;
      button1.Left:=10;
      button1.Top:=10;
      button1.Caption:='Button1';
      button1.OnClick:=self.Button1click;
      button2:=TButton.Create(self);
      button2.Parent:=self;
      button2.Left:=100;
      button2.Top:=100;
      button2.Caption:='Button2';
    end;end.
    ---------------------------------------------------------unit Unit3;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Unit2, StdCtrls;type
      TForm3= class(TForm2)
        procedure Button2click(sender:TObject);
      public
        constructor Create(AOwner:TComponent);override;
      end;implementation{ TForm3 }procedure TForm3.Button2click(sender: TObject);
    begin
      showmessage('Button2 click');
    end;
    constructor TForm3.Create(AOwner: TComponent);
    begin
      inherited;
      self.button2.OnClick:=self.Button2click;
    end;end.
      

  2.   

    如果是通过new project1里面的继承 也没有问题,不用重载Create,直接双击按钮就能定义事件没有问题-----------------------------------
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Unit1, StdCtrls;type
      TForm2 = class(TForm1)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementation{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
      inherited;
      showmessage('TForm2 button1 click');
    end;end.-------------------------------------unit Unit3;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Unit2, StdCtrls;type
      TForm3 = class(TForm2)
        procedure Button2Click(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form3: TForm3;implementation{$R *.dfm}procedure TForm3.Button2Click(Sender: TObject);
    begin
      inherited;
      showmessage('TForm3 button2 click');
    end;procedure TForm3.Button1Click(Sender: TObject);
    begin
    //  inherited;
      showmessage('TForm3 button1 click');
    end;end.
    这样产生的按钮事件代码里面会有inherited;如果不继承父类里的按钮事件,就把这句去掉
      

  3.   

    我是用第二种方法,但是我的窗体显示加了一个函数:
    function form1_create() :tmodalresult  ;
    begin
     with tform1.Create(application) do
       try
          Result:=ShowModal;
      finally
       free;
        end;
    end;
    现在的问题是我的程序中button2click事件没有效果!!!
      

  4.   

    不好意思,应该是form2和form3用了这个函数,form1没有,是自动创建的!
      

  5.   

    我用第二中方法实验成功了,
    但是第二个没有,我的是DELPHI5。0,WIN98是我的版本问题,还是DELPHI中有什么开关没有打开呢???
    请教 citytramper(从开始到现在) 和各位!
      

  6.   

    function form1_create() :tmodalresult  ;
    begin
     with tform1.Create(application) do   //这个是创建哪个窗口
       try
          Result:=ShowModal;
      finally
       free;
        end;
    end;
    现在的问题是我的程序中button2click事件没有效果!!!
    --------------------------------------------------------Form2和Form3是在什么时候创建的
      

  7.   

    form2和form3是用上面的类似form1_create() 的函数创建的,在希望显示的时候调用
    如果我在form3中加一个这样的函数,button2click事件就可以了:
    constructor TForm3.Create(AOwner: TComponent);
    begin
      inherited;
      self.button2.OnClick:=self.Button2click;
    end;
    我想这是一个构造函数,但是我不是很明白  self.button2.OnClick:=self.Button2click;
    的含义,sefl分别指什么东西???
      

  8.   

    self.button2.OnClick:=self.Button2click;事件触发,说白了就是收到了某个消息,消息处理过程通过assigned(xx);判断这个事件处理过程是否定义了,定义了就执行它。不过这时要求过程ButtonClick是窗体成员,
      

  9.   

    哦,我想self就是指窗体本身,然后self.button2.OnClick:=self.Button2click;就是:
    当窗体的button2被点击是做button2click过程
    但是为什么一定要加这么一句才有效呢?
    下面是我的一点点想法:
    form3是form2的子类,form2没有这么一个过程,所以在form3中要实现这个功能就要加这么一个过程,要在构造函数中定义,是为了要将button2click与button2被点击联系起来,
    那么为什么在form2 中不需要加这么一句话呢???是不是form2默认的构造函数已经加了这么一个过程???
    可是不懂的是form2继承了tform,form3继承了tform2,为什么form2不需要自己添加self.button1.onclick=self.button1click,
    form3却需要自己添加self.button2.onclick=self.button2click
      菜鸟继续请教!!!
      

  10.   

    真是不幸,我这模拟不出你的那种情况,我试的两种方法都行.
    你创建form2,form3时应该是 Tform2.create  TForm3.create 吧,不要都写成TForm2了
      

  11.   

    事件处理函数重载一下就可以呀,没有那么麻烦吧unit Unit2;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Unit1, StdCtrls;
    type
      TForm2= class(TForm1)
        button1:TButton;
        button2:TButton;
        procedure Button1click(sender:TObject);virtual;
        procedure Button2click(sender:TObject);virtual;
      public
    end;unit Unit2;interface
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Unit1, StdCtrls;
    type
      TForm3= class(TForm1)
        button1:TButton;
        button2:TButton;
        procedure Button1click(sender:TObject);override;
        procedure Button2click(sender:TObject);override;
      public
    end;