1.为什么下面这段程序procedure Tform1.FormShow(Sender: TObject);
begin
edit3.focused
end;不能使焦点聚到edit3上?我的程序是一个试验程序,在form1上放了3个Tedit,
程序只有这么一条,除此之外,什么也没有了!
但程序启动后焦点总在edit1上。2.下面这段程序为什么通不过?
procedure TForm1.FormCreate(Sender: TObject);
var m:array[0..7]of tedit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i].Create(self);
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
end;
begin
edit3.focused
end;不能使焦点聚到edit3上?我的程序是一个试验程序,在form1上放了3个Tedit,
程序只有这么一条,除此之外,什么也没有了!
但程序启动后焦点总在edit1上。2.下面这段程序为什么通不过?
procedure TForm1.FormCreate(Sender: TObject);
var m:array[0..7]of tedit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i].Create(self);
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
end;
设计时将TAB顺序调成edit3为第一即可,或者这样
procedure Tform1.FormShow(Sender: TObject);
begin
postmessage(edit3,WM_SETFOCUS,0,0);
end;
第二个问题:
没有给父属性,加该句就好了:m[i].parent:=self;
设计时将TAB顺序调成edit3为第一即可,或者这样
procedure Tform1.FormShow(Sender: TObject);
begin
postmessage(edit3,WM_SETFOCUS,0,0);
end;
第二个问题:
没有给父属性,加该句就好了:m[i].parent:=self;
procedure Tform1.FormShow(Sender: TObject);
begin
edt3.SetFocus;
end;第二个问题:
procedure TForm1.FormCreate(Sender: TObject);
var
m:array[0..7]of TEdit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
end;
改为:
edit3.SetFocus前者是判断是否处于焦点状态,后者是设置焦点(是你需要的)
第一个问题加上这句即可:m[i].parent:=self;
按各位的提示方法都试验了一下,全没有用。其中第一题,将focused改为setfocuse后,
一运行就出错(本来不会出错),出来
can not focus a disabled or insisible window
的出错信息,但加上
edit3.visible:=true;
edit3.enabled:=true;
也无用,不知道这里的window指的是什么?第二题加与不加
m[i].parent:=self;
一个样,总是出来access violation 错误,
出错的语句位置指示在语句
m[i].Create(self);
的头上(左边)。
如把m[i].parent:=self放在m[i].Create(self)之前,
则出错语句位置指示在语句
m[i].parent:=self
的头上(左边)。
postmessage(edit3,WM_SETFOCUS,0,0);
也没有任何作用。而且,edit3也不允许放在这里,它不是一个windows句柄。
第二个问题,m[i].Create(self);应改为:m[i] := TEdit.Create(self);后面加上m[i].Parent := self;
否则新增文本框不能显示出来。
edit3.Enabled := True;
edit3.Visible := True;
要加在:
edit3.SetFocus;
的前面才行
在from在create事件里面不能生成子控件,因为此时,form自己还没有建立起来,所以两个问题一并解决,把要写的代码全放在show事件中就ok了
procedure TForm1.FormShow(Sender: TObject);
var
m:array[0..7]of TEdit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
postmessage(edit3.handle,WM_SETFOCUS,0,0);
end;
procedure TForm1.FormShow(Sender: TObject);
var
m:array[0..7]of TEdit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
postmessage(edit3.handle,WM_SETFOCUS,0,0);
end;
postmessage向edit3发送得到焦点的消息,是在form显示后才执行的,所以是正确的,如果在show事件中就让得到焦点是不可想象的,因为此时form都没有显示,不存在谁拥用焦点的问题。
m[i]:=TEdit.Create(self);
m[i].parent:=self;
这种方法不好 edit1 和edit3都会得到焦点 这时按键 edit1中会显示输入的东西 edit3中也会输入 但全是空格
procedure TForm1.FormShow(Sender: TObject);
var
m:array[0..7]of TEdit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
postmessage(edit3.handle,WM_SETFOCUS,0,0);
end;
postmessage向edit3发送得到焦点的消息,是在form显示后才执行的,所以是正确的,如果在show事件中就让得到焦点是不可想象的,因为此时form都没有显示,不存在谁拥用焦点的问题。让edit3得到焦点,也可以通过设置tab顺序来完成,把edit3的顺利调整为第一个就行了。
“如果在show事件中就让得到焦点是不可想象的,因为此时form都没有显示,不存在谁拥用焦点的问题”
你试过了没有?试过后再说能不能设置。
1. 将语句
m[i]:=Create(self);
改为
m[i]:=TEdit.Create(self);
后,程序就能正确运行。而与语句
m[i].parent:=self;
无关,加与不加一个样。2. 在form的create事件中,
还是在form的show事件中 创建Tedit[i],
也没有关系,都一样行。3. 在退出程序时出错,出错信息仍是access violation。有兴趣的不妨自己试试,看结果如何?
procedure TForm1.FormShow(Sender: TObject);
var
i:integer;
begin
for i:=1 to 3 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
postmessage(edit3.handle,WM_SETFOCUS,0,0);
end;
//增加form的close事件,释放资源
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i:integer;
begin
for i:=1 to 3 do
m[i].free;
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation
var m:array[0..7]of TEdit;
{$R *.dfm}procedure TForm1.FormShow(Sender: TObject);
var
i:integer;
begin
for i:=1 to 3 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
edit3.SetFocus;//比postmessage好使
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var
i:integer;
begin
for i:=1 to 3 do
m[i].free;
end;end.
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
edit3.visible:=true;
edit3.enabled:=true;
edit3.SetFocus;
end;end.Project的代码是:program Project1;uses
Forms,
Unit1 in 'Unit1.pas' {Form1};{$R *.res}begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.
(注意:想把焦点聚在生成的tedit组件m[0]上不成功,
而且,对m[i]的任何其他操作也无法进行,
也就是说,动态生成的组件只能看,不能用!)unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
m:array[1..7]of tedit;{$R *.dfm}
implementation
procedure TForm1.FormCreate(Sender: TObject);
var i:integer;
begin
for i:=0 to 7 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=20;
m[i].Top:=20+i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
// m[0].SetFocus; //通不过,为什么
end;
procedure TForm1.FormShow(Sender: TObject);
begin
// m[0].SetFocus; //通不过,为什么end;end.
1.一定要在Form1.create中创建tedit,而在Form1.show中setfocue才行
2.如果Form1.create中创建tedit和setfocue,程序编译时就出错
3.如果Form1.show中创建tedit和setfocue,能编译和运行,但看不到Edit组件
第二个问题:
m:array[1..7]of tedit; 你已经指定了数组m的下标为1..7,那么你的循环自然应该为for i:=1 to 7 do ...
m[0].SetFocus; 根本就不存在,
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
edt1: TEdit;
procedure FormCreate(Sender: TObject);
private
procedure MyKeyPress(Sender: TObject; var Key: Char);
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.MyKeyPress(Sender: TObject; var Key: Char);
begin
if Key=#13 then
ShowMessage('Enter Key');
end;procedure TForm1.FormCreate(Sender: TObject);
begin
edt1.OnKeyPress:=MyKeyPress;
end;end.
begin
......
end;但无法为m[1]写写click响应程序:procedure TForm1.m[1]Click(Sender: TObject);
begin
......
end;这样的写法编译就通不过。
m[1].Click := Edit1Click;
在过程中,根据Sender来判断:
procedure TForm1.Edit1Click(Sender: TObject);
begin
if (Sender as TEdit).name = 'm1' then
...
if ....
end;
Edit1Click在dfm文件里是会保存的,比如说 OnClick = Edit1Click ,在控件被创建的时候会去关联上的, 但是你自己写的Click事件是不回关联的,所以只能自己去m[1].Click := Edit1Click;
//事先写好相应的过程,创建控件时再为相应的事件赋值你的过程名。如
procedure MyTextClick(Sender : TOBject);procedure TForm1.MyTextClick(Sender : TOBject);
beign
//
end;
创建时赋值
(m[0] as TEdit).Onclick := MyTextClick;
beign
// 这是单击事件,写你的代码进去,当然其它事件,也可事先定义好响应的过程,像这个一样。
end;
procedure TForm1.MEditChange(Sender: TObject);
begin
// 这是edit值发生变化的事件,写你的代码进去。
end;
.
.
.
.
.
其它事件处理过程。以下是事件的关联:
for i:=1 to 3 do
begin
m[i].onclick:=MEditClick; //edit单击事件
m[i].onchange:=MEditchange;//edit值发生变化的事件
.
.
m[i].on事件:=事先定义好事件处理过程;
end;就这样做,结贴吧
begin
Edit3.setfocused; // edit3.focused
end; 不能使焦点聚到edit3上? 我的程序是一个试验程序,在form1上放了3个Tedit,
程序只有这么一条,除此之外,什么也没有了!
但程序启动后焦点总在edit1上。 2.下面这段程序为什么通不过?
procedure TForm1.FormCreate(Sender: TObject);
var m:array[0..7]of tedit;
i:integer;
begin
for i:=1 to 3 do
begin
m[i].Create(self);
m[i].parent := self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=0;
m[i].Top:=i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
end;
end;
beign
// 这是单击事件,写你的代码进去,当然其它事件,也可事先定义好响应的过程,像这个一样。
end;
procedure TForm1.MEditChange(Sender: TObject);
begin
// 这是edit值发生变化的事件,写你的代码进去。
end;
.
.
.
.
.
其它事件处理过程。 以下是事件的关联:
for i:=1 to 3 do
begin
m[i].onclick:=MEditClick; //edit单击事件
m[i].onchange:=MEditchange;//edit值发生变化的事件
.
.
m[i].on事件:=事先定义好事件处理过程;
end; 就这样做,结贴吧
你的事件赋值思想是对的,判别那一个m[i]的方法也可用,
但我的Tedit数组m[i]有8个(甚至想更多),
这样为每一个i,写一条if语句,就太烦了。我在循环语句中增加了一条 (m[i] as tedit).onClick:=mClick就把一切m[i]的click事件赋值给mClick事件:
for i:=0 to 7 do
begin
m[i]:=TEdit.Create(self);
m[i].parent:=self;
m[i].Width:=120;
m[i].Height:=20;
m[i].Left:=20;
m[i].Top:=20+i*22;
m[i].Name:='m'+inttostr(i);
m[i].Text:='m'+inttostr(i);
m[i].Visible:=true;
(m[i] as tedit).onClick:=mClick
end;现在就是如何为所有m[i].click事件编写一个
统一的事件响应程序,类似于:procedure TForm1.mClick(Sender: TObject);
var i:integer;
begin
for i:=0 to 7 do
begin
if (sender as tedit).name = 'm['+inttostr(i)+']' then
showmessage( 'm['+inttostr(i)+'] clicked');
end
end;以上程序编译通过,可运行,但 showmessage()不执行。
我的前2个问题的错:
1.edit3.Focused;
应改为 edit3.SetFocus;
-- 此问题2楼的回复
postmessage(edit3,WM_SETFOCUS,0,0);
虽然有错,但提示了WM_SETFOCUS,给10分
4楼最早给出正确答案edit3.SetFocus; 给10分
2.漏掉m[i].parent:=self;
-- 此问题由2楼最早给出答案。10分
m[i].Create(self);
应改为
m[i]:=Tedit.Create(self);
-- 此问题由4楼最早给出答案。10分
新问题:
41楼最早给出了正确答案(但不够理想的:判别时繁琐)
考虑到问题我后来自己发现并解决,也没有其他人回复,
而我不能给自己送分,所以100分也就全属于41楼了。