最近在做一个数据库系统,碰到一个问题,如:在form2、form3中都要执行form1.showmodal调用form1窗口,form1打开后需要判断到底是哪个form执行了该代码,从而执行相应的代码,该怎么解决?请高手赐教!

解决方案 »

  1.   

    可以在
    form1.showmodal
    之前就传递了,如form1.xxx='123'xxx为form1的公共变量
      

  2.   

    procedure TForm1.FormShow(Sender: TObject);
    begin
      if Sender = form2 then ...;
      if Sender = form3 then ...;
    end;
      

  3.   

    form1中重载create方法
    procedure TForm1.Create(target: integer);
    begin
      Self.FormRunner = target;//FormRunner为Form1中的属性
      //根据FormRunner值不同来执行不同的代码
    end;form2和form3中的on btn_click
    var
      objForm: TForm1;
    begin
      objForm := TForm.Create(yourValueHere);
    end;
      

  4.   

    判断窗体,你就把xxx定义为form型即可xxx:form
    xxx:=form2
      

  5.   

    csnight(帅得拖网速)我试过了,你的代码不行。
      

  6.   

    To rcom10002(KNIGHTRCOM)
    不懂你的意思,请详细说明一下
      

  7.   

    没必要传啊,你可以参考如以下两段代码
    procedure Tmainform.BitBtn4Click(Sender: TObject);
    begin
        Application.CreateForm(Tchgform, chgform);
        chgform.q1.sql.Clear;
        if node[nodeid].id=0 then
          chgform.q1.SQL.Add('select * from aa where parent_dm=0')
        else
          chgform.q1.SQL.Add('select * from aa where parent_dm='+trim(inttostr(node[nodeid].id)));
        chgform.q1.Prepared;
        chgform.q1.Open;
        if chgform.q1.Eof then
        begin
           chgform.Free;
           exit;
        end;
        chgform.ShowModal;
        chgform.Free;
    end;
    procedure Tchgform.Button1Click(Sender: TObject);
      .....
      q2.FieldByName('parent_dm').AsInteger:=mainform.node[mainform.nodeid].id;
      q2.FieldByName('cs').AsInteger:=mainform.node[mainform.nodeid].cs+1;
      .....
    end;
      

  8.   

    ////////////////////////////////////////////
    procedure TForm1.FormShow(Sender: TObject);
    begin
      if Sender = form2 then ...;
      if Sender = form3 then ...;
    end;//////////////////////////////////////////我认为这段代码没问题
    你在showmodal之前的create写成如下方式:
      form1.create(self);  //self 即为调用窗体;然后判断:
      if Sender is Tform2 then 
      begin
      //....
      end
      else if Sender is Tfrom3 then
      begin
      //....
      end;
      

  9.   

    1. 在Form2. Form3中都是通过
       form1:=tform1.create(self); //关键是self参数
       form1.showmodal;
       模式调用form12. 在form1的create事件中控制
       if tform(self.owner).classname = 'tform2' //通过form2调用
       begin
       end
       else if tform(self.owner).classname = 'tform3' //通过form3调用
       begin
       end这种方式简单易行,不用额外添加任何变量,不需要额外修改数据
      

  10.   

    procedure TForm1.FormShow(Sender: TObject);
    var str:string;
    begin
         str:=UpperCase(Screen.ActiveForm.ClassName);
         if str='TFORM2' then
           showmessage('form2 call');
         if str='TFORM3' then
           showmessage('form3 call');
    end;
      

  11.   

    procedure TForm1.FormShow(Sender: TObject);
    var str:string;
    begin
         str:=UpperCase(Screen.ActiveForm.ClassName);end;
      

  12.   

    procedure TForm1.FormShow(Sender: TObject);
    var str:string;
    begin
         str:=UpperCase(Screen.ActiveForm.Name);
         showmessage(str);
    end;
      

  13.   

    procedure TForm2.Button1Click(Sender: TObject);
    begin
      Application.CreateForm(TForm1, Form1);
      Form1.Tag:=21;
      Form1.ShowModal;
      Form1.Free;
    end;
    procedure TForm3.Button1Click(Sender: TObject);
    begin
      Application.CreateForm(TForm1, Form1);
      Form1.Tag:=31;
      Form1.ShowModal;
      Form1.Free;
    end;在Form1中判断Self.tag值.
      

  14.   

    在form1中定义一个公用变量,然后在showmodal之前,对他赋值,根据这个公用变量来判断就可以了。你也可以直接利用form的caption属性来做。同样的思路,也可以写入注册表,或者用ini文件,都可以。
      

  15.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        Ffrm: TForm;
        { Private declarations }
      public
        { Public declarations }
          procedure Setfrm(const Value: TForm);
      published
          property frm:TForm read Ffrm write Setfrm;
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Setfrm(const Value: TForm);
    begin
      Ffrm := Value;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    ShowMessage(Ffrm.Name);
    end;
    end.
    //**************
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementationuses Unit2;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
      if not Assigned(Form1) then
      begin
        Application.CreateForm(TForm1, Form1);
        Form1.Setfrm(Form2);
        Form1.ShowModal;
        Form1.Free;
      end;
      
    end;end.
      

  16.   

    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form2: TForm2;implementationuses Unit2;{$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
      if not Assigned(Form1) then
      begin
        Application.CreateForm(TForm1, Form1);
        Form1.Setfrm(Form2);
        Form1.ShowModal;
        FreeAndNil(form1);
      end;
      
    end;end.
      

  17.   

    给你一段代码吧:
    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
    uses unit3,unit2;
    {$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
    begin
      form3.flag:=true;
      form3.Show();
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      form2.Show;
    end;end.//主窗口,调用form3时,设置form3的自定义变量为true;unit Unit2;//form2调用form3时设置form3的自定义变量为false;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm2 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
         
        { Public declarations }
      end;var
      Form2: TForm2;implementation
     uses unit3;
    {$R *.dfm}procedure TForm2.Button1Click(Sender: TObject);
    begin
      form3.flag:=false;
      form3.Show;
    end;end.
    unit Unit3;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs;type
      TForm3 = class(TForm)
        procedure FormShow(Sender: TObject);
      private
        { Private declarations }
      public
        flag:boolean;//加入一个自定义变量
        { Public declarations }
      end;var
      Form3: TForm3;implementation{$R *.dfm}procedure TForm3.FormShow(Sender: TObject);//响应相应的代码;
    begin
      if flag then
        showmessage('a')
      else
        showmessage('b');
    end;end.
      

  18.   

    在form1里设个公共变量
    不同的form执行form1.showmodal前修改变量为不同的值
    从而区分调用的窗体