在一个Form1窗体上放置了一个DBCtrlGrid和一个Button1,DBCtrlGrid用来显示数据表里所有的用户,选中某个用户,点Button1,便可创建一个相应的动态窗体,
但是当同时打开几个用户的对应窗体时,(比如依次动态创建了f1,f2,f3),只能对f3操作,就算对f1,f2操作(比如说,按f1窗体的“退出”按钮,就会把f3的窗体关掉,而且f3关掉后,f1,f2任何按钮都失效)为什么,怎么解决啊,
第一次发贴,毕业设计着急,谢谢啦

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject);//确定
    begin
      StatusBar1.Panels[0].Text:='用户:['+UserQuery.FieldByName('c_code').Value+']';
      CreateForm;
    end;procedure   TForm1.CreateForm;
      begin
        f1:=TForm.Create(Self);
        f1.Name:='jianpan'+inttostr(Count);
        Inc(Count);
        ......//f1的属性设置
        f1.OnClose:=FormClose;
        f1.show;    GroupBox3:=TGroupBox.Create(nil);
        ......  //GroupBox3的属性设置
        GroupBox3.Caption:='数字键';
        GroupBox3.Parent:=f1;
        GroupBox3.Visible:=True;    ......//在动态创建的f1窗体上动态创建动态组件
        
        ......//在f1上动态创建ADOQuery    end;
      

  2.   

    怀疑是你没有把代码独立开来写
    如果是f3调用了f2或者f1的api函数,退出之后api的调用会失效,自然不能操作了
      

  3.   

    (gyk120)能给出个解决方法吗
    我总不能100个用户就写一百个创建的函数啊
      

  4.   

    恩,句柄就相当于窗口的一个特殊印记,每个窗口都有一个句柄,相关问题可以上baidu搜索
    具体是什么问题也不好说……
    SmallHand前辈,看你了……
      

  5.   

    FormClose方法怎么写的?
    肯定是这的问题
      

  6.   

    你的CreateForm过程会被多次调用,变量f1在每次调用时都会被重新赋值(指向新创建的窗体),这样就会丢失掉之前窗体的指针,你的程序也就没有办法再引用到之前创建的窗体。建议楼主使用一个数组来存放动态创建的多个窗体。
    将f1的声明改为
      f1: array of TForm;procedure TForm1.CreateForm; 
    begin 
      Inc(Count); //Count变量初值为0
      SetLength(f1, Count);
      f1[Count - 1] := TForm.Create(Application);
      with f1[Count - 1] do begin
        ......//属性设置 
      end;
      f1[Count - 1].OnClose := FormClose;
      f1[Count - 1].Show;
      
      GroupBox3:=TGroupBox.Create(nil); 
      ......  //GroupBox3的属性设置 
      GroupBox3.Caption:='数字键'; 
      GroupBox3.Parent:=f1[Count - 1]; 
      GroupBox3.Visible:=True;   ......//在动态创建的f1[Count - 1]窗体上动态创建动态组件 
      ......//在f1[Count - 1]上动态创建ADOQuery 
    end;
      

  7.   

    我试了一下,出现错误:
    Access violation at address 0045EC49 in module 'CLIENT.EXE'.Read of address 000000E5.
    我觉得你的很对的啊
      

  8.   

    是不是因为我组件的parent是GroupBox,还要将GroupBox设成数组?
      

  9.   

    如果你GroupBox也是动态创建多个的话,也需要用数组对其进行管理。
      

  10.   

    不知道你在FormClose事件里是如何写的。
    下面的代码可以动态创建多个窗体,并正确关闭之(省略了动态创建其他组件部分)。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
        FFormCount: Integer;
        FormArray: array of TForm;
      protected
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      FFormCount := 0;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      Inc(FFormCount);
      SetLength(FormArray, FFormCount);
      FormArray[FFormCount - 1] := TForm.Create(Application);
      FormArray[FFormCount - 1].OnClose := FormClose;
      FormArray[FFormCount - 1].Caption := Format('Form%d', [FFormCount]);
      FormArray[FFormCount - 1].Width := 200;
      FormArray[FFormCount - 1].Height := 200;
      FormArray[FFormCount - 1].Position := poScreenCenter;
      FormArray[FFormCount - 1].Show;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      ShowMessage(TForm(Sender).Caption + ' has been closed.');
      TForm(Sender).Free;
    end;end.
      

  11.   

    我关闭这么写的
    procedure   Tdiaer.FormClose_Self(Sender:   TObject;   var   Action:   TCloseAction);
      begin
          Action   :=   caFree;   
      end;测试了你的程序,Error reading Form1.OnClose :Invalid property value
      

  12.   

    Form1的OnClose不需要指定事件!
      

  13.   

    各个窗体独立了 呵呵 谢谢你
    unit phone;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls;
    type
        TForm1 = class(TForm)
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);  private
        FFormCount: Integer;
        FormArray: array of TForm;
        Button15Array: array of TButton;
        GroupBox3Array: array of TGroupBox;
        k:array[0..100] of integer;
        Presstimes:integer;
        { Private declarations }
    protected
        procedure FormClose(Sender: TObject; var Action: TCloseAction);
        procedure Button15Click(Sender: TObject);  public
        { Public declarations }
      end;var
      Form1: TForm1;
       implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
    begin
      Presstimes := 0;
      FFormCount := 0;
    end;procedure TForm1.Button1Click(Sender: TObject);begin
      Inc(Presstimes);
      Inc(FFormCount);
      k[Presstimes-1]:=FFormCount-1;  
      SetLength(FormArray, FFormCount);
      FormArray[FFormCount - 1] := TForm.Create(Application);
      FormArray[FFormCount - 1].OnClose := FormClose;
      FormArray[FFormCount - 1].Caption := Format('Form%d', [FFormCount]);
      FormArray[FFormCount - 1].Width := 303;
      FormArray[FFormCount - 1].Height := 439;
      FormArray[FFormCount - 1].Position := poScreenCenter;
      FormArray[FFormCount - 1].Show;  SetLength(GroupBox3Array, FFormCount);
      GroupBox3Array[FFormCount - 1]:=TGroupBox.Create(nil);
      GroupBox3Array[FFormCount - 1].Width:=265;
      GroupBox3Array[FFormCount - 1].Height:=225;
      GroupBox3Array[FFormCount - 1].Top:=16;
      GroupBox3Array[FFormCount - 1].Left:=16;
      GroupBox3Array[FFormCount - 1].Caption:='数字键';
      GroupBox3Array[FFormCount - 1].Parent:=FormArray[FFormCount - 1];
      GroupBox3Array[FFormCount - 1].Visible:=True;  SetLength(Button15Array, FFormCount);
      Button15Array[FFormCount - 1]:=TButton.Create(nil);
      Button15Array[FFormCount - 1].Width:=76;
      Button15Array[FFormCount - 1].Height:=25;
      Button15Array[FFormCount - 1].Top:=376;
      Button15Array[FFormCount - 1].Left:=104;
      Button15Array[FFormCount - 1].Caption:='退出';
      Button15Array[FFormCount - 1].Parent:=FormArray[FFormCount - 1];
      Button15Array[FFormCount - 1].OnClick:=Button15Click;//总是无法确定要关闭的窗体
      Button15Array[FFormCount - 1].Visible:=True;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
    begin
      ShowMessage(TForm(Sender).Caption + ' has been closed.');
      TForm(Sender).Free;
    end;procedure TForm1.Button15Click(Sender: TObject);begin
      FormArray[k[Presstimes]].Hide;
    end;end.很想把函数也放进数组里
    每个创建的窗体没法获得唯一标记啊,指教(chirs_mao)怎么写Button15Click啊
      

  14.   

    你可以把创建的窗体序号,即FormArray中的索引值存放在窗体的Tag属性中。procedure TForm1.Button1Click(Sender: TObject); begin 
      Inc(Presstimes); 
      Inc(FFormCount); 
      k[Presstimes-1]:=FFormCount-1;   
      SetLength(FormArray, FFormCount); 
      FormArray[FFormCount - 1] := TForm.Create(Application); 
      FormArray[FFormCount - 1].OnClose := FormClose; 
      FormArray[FFormCount - 1].Tag := FFormCount - 1;
      FormArray[FFormCount - 1].Caption := Format('Form%d', [FFormCount]); 
      FormArray[FFormCount - 1].Width := 303; 
      FormArray[FFormCount - 1].Height := 439; 
      FormArray[FFormCount - 1].Position := poScreenCenter; 
      FormArray[FFormCount - 1].Show;   SetLength(GroupBox3Array, FFormCount); 
      GroupBox3Array[FFormCount - 1]:=TGroupBox.Create(nil); 
      GroupBox3Array[FFormCount - 1].Width:=265; 
      GroupBox3Array[FFormCount - 1].Height:=225; 
      GroupBox3Array[FFormCount - 1].Top:=16; 
      GroupBox3Array[FFormCount - 1].Left:=16; 
      GroupBox3Array[FFormCount - 1].Caption:='数字键'; 
      GroupBox3Array[FFormCount - 1].Parent:=FormArray[FFormCount - 1]; 
      GroupBox3Array[FFormCount - 1].Visible:=True;   SetLength(Button15Array, FFormCount); 
      Button15Array[FFormCount - 1]:=TButton.Create(nil); 
      Button15Array[FFormCount - 1].Width:=76; 
      Button15Array[FFormCount - 1].Height:=25; 
      Button15Array[FFormCount - 1].Top:=376; 
      Button15Array[FFormCount - 1].Left:=104; 
      Button15Array[FFormCount - 1].Caption:='退出'; 
      Button15Array[FFormCount - 1].Parent:=FormArray[FFormCount - 1]; 
      Button15Array[FFormCount - 1].OnClick:=Button15Click;//总是无法确定要关闭的窗体 
      Button15Array[FFormCount - 1].Visible:=True; 
    end; procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); 
    begin 
      ShowMessage(TForm(Sender).Caption + ' has been closed.'); 
      TForm(Sender).Free; 
    end; procedure TForm1.Button15Click(Sender: TObject); begin 
      //FormArray[k[Presstimes]].Hide; 
      FormArray[TForm(TButton(Sender).Parent).Tag].Hide;
    end; 
      

  15.   


    procedure TForm1.Button1Click(Sender: TObject);begin
    ......
    SetLength(Butn0Array, FFormCount);
      Butn0Array[FFormCount - 1]:=TButton.Create(nil);
      Butn0Array[FFormCount - 1].Width:=41;
      Butn0Array[FFormCount - 1].Height:=41;
      Butn0Array[FFormCount - 1].Top:=176;
      Butn0Array[FFormCount - 1].Left:=8;
      Butn0Array[FFormCount - 1].Caption:='0';
      Butn0Array[FFormCount - 1].Parent:=GroupBox3Array[FFormCount - 1];
      Butn0Array[FFormCount - 1].OnClick:=Butn0Click;
      Butn0Array[FFormCount - 1].Visible:=True;  SetLength(Memo1Array, FFormCount);
      Memo1Array[FFormCount - 1]:=TMemo.Create(nil);
      Memo1Array[FFormCount - 1].Width:=81;
      Memo1Array[FFormCount - 1].Height:=33;
      Memo1Array[FFormCount - 1].Top:=184;
      Memo1Array[FFormCount - 1].Left:=168;
      Memo1Array[FFormCount - 1].Text:='';
      Memo1Array[FFormCount - 1].Parent:=GroupBox3Array[FFormCount - 1];
      Memo1Array[FFormCount - 1].Visible:=True;
    ......
    end;procedure TForm1.Butn0Click(Sender: TObject);
    begin
    PostMessage((Memo1Array[TForm(TButton(Sender).Parent).tag]).Handle,   WM_KEYDOWN,   48,   0);//0为什么总是显示在第一个创建的窗里?
    end;
      

  16.   

    Tag属性默认值是0,你需要检查一下你取到的Tag值是多少!
      

  17.   

    为什么TForm(sender)的tag值不能被其他数组引用?
    我为每个控件都保存了自己的tag值,才正确引用的
    我现在的全局变量也需要编程数组,starttime[0..100]:array of integer,第i个窗体用starttime[i],但是i的值不能用TForm的索引,确定不了,怎么解决啊?