delphi中想在按钮点击事件中,按一下按钮重新加载窗体一次。。

解决方案 »

  1.   

    在 button 的點擊事件中 調用 窗體的 Create()方法.
      

  2.   

    没明你说的重新加载是什么意思?Form1.show.?
      

  3.   

    form1是主窗体。调用form1.create()就报错了。我是想form1窗体按一下按钮的单击事件中,重新加载了form1本身窗体。就相当于重新执行一次可执行文件。。
      

  4.   

    非主窗体没什么难度,如果是主窗体的话会比较复杂些,这里给一个主窗体的例子unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure ModifyObjectField(Obj: TObject; Offset: Integer; V: TObject);
    begin 
      if(Assigned(Obj))then 
      try 
        PInteger(PAnsiChar(Obj) + Offset)^  := Integer(V);
      except 
      end; 
    end;function GetErrorAddress(E: Exception; out Address: Cardinal): Boolean; 
    var 
      s  : string; 
      p : PChar; 
      i  : Integer; 
    begin 
      s  := E.Message; 
      p  := Pointer(s);
      Result  := False;
      for i:=Length(s)-1 downto 0 do 
      begin 
        if(p[i]=' ')then 
        try 
          p[i]    := '$'; 
          Address  := StrToInt(p+i); 
          Result  := True; 
          Exit; 
        except 
          Result  := False; 
          Exit; 
        end;   
      end;   
    end; function GetApplication_MainForm_Offset: Cardinal; 
    begin 
      try 
        Result  := Cardinal(TApplication(nil).MainForm); { 只是为了引发一个异常,用来取得TApplication.FMainForm的相对地址 }
      except 
        on E: Exception do 
          GetErrorAddress(E, Result);
      end; 
    end; procedure CreateMainForm; 
    begin
      Form1  := TForm1.Create(Application);
      ModifyObjectField(Application, GetApplication_MainForm_Offset, Form1);
      Form1.Show;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      CreateMainForm;
      Free;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Caption := 'Old Form';
    end;end.
      

  5.   

    突然发现我把问题想复杂了,其实很简单就能实现:unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        procedure ReloadForm;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      ReloadForm;
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      Caption := 'Old Form';
    end;procedure TForm1.ReloadForm;
    var
      i : Integer;
    begin
      Hide;
      try
        for i:=ComponentCount-1 downto 0 do
          RemoveComponent(Components[i]);
        InitInheritedComponent(Self, TForm);
      finally
        Show;
      end;
    end;end.
      

  6.   

    现在终于会delphi和VC了。当时还不懂。