在一个Form1窗体上放置了一个DBCtrlGrid和一个Button1,DBCtrlGrid用来显示数据表里所有的用户,选中某个用户,点Button1,便可创建一个相应的动态窗体,
但是当同时打开几个用户的对应窗体时,(比如依次动态创建了f1,f2,f3),只能对f3操作,就算对f1,f2操作(比如说,按f1窗体的“退出”按钮,就会把f3的窗体关掉,而且f3关掉后,f1,f2任何按钮都失效)为什么,怎么解决啊,
第一次发贴,毕业设计着急,谢谢啦
但是当同时打开几个用户的对应窗体时,(比如依次动态创建了f1,f2,f3),只能对f3操作,就算对f1,f2操作(比如说,按f1窗体的“退出”按钮,就会把f3的窗体关掉,而且f3关掉后,f1,f2任何按钮都失效)为什么,怎么解决啊,
第一次发贴,毕业设计着急,谢谢啦
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;
如果是f3调用了f2或者f1的api函数,退出之后api的调用会失效,自然不能操作了
我总不能100个用户就写一百个创建的函数啊
具体是什么问题也不好说……
SmallHand前辈,看你了……
肯定是这的问题
将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;
Access violation at address 0045EC49 in module 'CLIENT.EXE'.Read of address 000000E5.
我觉得你的很对的啊
下面的代码可以动态创建多个窗体,并正确关闭之(省略了动态创建其他组件部分)。
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.
procedure Tdiaer.FormClose_Self(Sender: TObject; var Action: TCloseAction);
begin
Action := caFree;
end;测试了你的程序,Error reading Form1.OnClose :Invalid property value
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啊
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;
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;
我为每个控件都保存了自己的tag值,才正确引用的
我现在的全局变量也需要编程数组,starttime[0..100]:array of integer,第i个窗体用starttime[i],但是i的值不能用TForm的索引,确定不了,怎么解决啊?