//*************************************************************************
//本过程控制输入框按键
procedure TFormMain.LabeledEditKeyPress(Sender: TObject; var Key: Char);
var
i: integer;
begin
For i := 1 to 5 do
begin
if LabeledEditArray[i].Focused then
begin
//回车时焦点到下一个输入框
if key = #13 then Perform(WM_NEXTDLGCTL,0,0) ;
//代码型输入框
if LabeledEditArray[i].Tag = 0 then
begin
if not (key in [ '0'..'9' , 'a'..'z' , 'A'..'Z' , #8 ]) then key := #0 ;
end;
//整数型输入框
if LabeledEditArray[i].Tag = 1 then
begin
if not (key in [ '0'..'9' , #8 ]) then key := #0 ;
end;
//小数型输入框
if LabeledEditArray[i].Tag = 2 then
begin
if not (key in [ '0'..'9' , #46 , #8 ]) then key := #0 ;
end;
//*************************************************************************
//本过程控制输入框变化
procedure TFormMain.LabeledEditChange(Sender: TObject);
var
i,j: integer;
OKFlag: boolean;
m: integer;//整数部分位数
n: integer;//小数部分位数
SingleKey: Char;
begin
m := 6 ;
n := 4 ;
OKFlag := true ;
For i := 1 to 5 do
begin
if LabeledEditArray[i].Focused then
begin
//代码型输入框
if LabeledEditArray[i].Tag = 0 then
begin
For j := 1 to Length(LabeledEditArray[i].Text) do
begin
SingleKey := LabeledEditArray[i].Text[j];
if not (SingleKey in [ '0'..'9' , 'a'..'z' , 'A'..'Z' , #8 ]) then OKFlag := false;
end;
end;
//整数型输入框
if LabeledEditArray[i].Tag = 1 then
begin
For j := 1 to Length(LabeledEditArray[i].Text) do
begin
SingleKey := LabeledEditArray[i].Text[j];
if not (SingleKey in [ '0'..'9' , #8 ]) then OKFlag := false;
end;
end;
//小数型输入框
if LabeledEditArray[i].Tag = 2 then
begin
For j := 1 to Length(LabeledEditArray[i].Text) do
begin
SingleKey := LabeledEditArray[i].Text[j];
if not (SingleKey in [ '0'..'9' , #46 , #8 ]) then OKFlag := false;
end;
//小数点位置
if pos('.',LabeledEditArray[i].Text) <> 0 then
//有小数的情况
begin
//整数位数长度判断
if pos('.',LabeledEditArray[i].Text) - 1 > 6 then OKFlag := false ;
//小数位数长度判断
if Length(LabeledEditArray[i].Text) - pos('.',LabeledEditArray[i].Text) > n then OKFlag := false ;
//判断是否有两个小数点
if pos('.',copy(LabeledEditArray[i].Text,pos('.',LabeledEditArray[i].Text) + 1,Length(LabeledEditArray[i].Text) - pos('.',LabeledEditArray[i].Text))) <> 0 then OKFlag := false ;
end
else
//没有小数点的情况
begin
if Length(LabeledEditArray[i].Text) > m then OKFlag := false;
end;
end;
if OKFlag then
TempString := LabeledEditArray[i].Text
else
begin
LabeledEditArray[i].Text := TempString;
Keybd_event(VK_END,0, 0, 0);
Keybd_event(VK_END,0, KEYEVENTF_KEYUP, 0);
end;
end;
end;
end;
//本过程控制输入框变化
//*************************************************************************
//*************************************************************************
//本过程控制输入框获得焦点
procedure TFormMain.LabeledEditEnter(Sender: TObject);
var
i: integer;
begin
For i := 1 to 5 do
begin
LabeledEditArray[i].EditLabel.Font.Color := clBlack ;
LabeledEditArray[i].Font.Color := clBlack ;
if LabeledEditArray[i].Focused then
begin
TempString := LabeledEditArray[i].Text ;
LabeledEditArray[i].EditLabel.Font.Color := clBlue ;
LabeledEditArray[i].Font.Color := clBlue ;
end;
end;
end;
//本过程控制输入框获得焦点
//*************************************************************************
var
A:array[1..5] of TEdit;
i:Integetr;
begin
for i:=1 to 5 do
begin
A[i]:=TEdit.create(self);
A[i].Parent:=Self;
A[i].Left:=10+i*2;
A[i].Text:='ArrayEdit'+IntToStr(i);
end;
End;
当然有啊,
定义一个控件数组
var
EditArray:array of TEdit
begin
setlength(EditArray,theLengthYouWanted);
for i:=0 to setlength(EditArray,theLengthYouWanted)-1 do
begin
EditArray[i]:=TEdit.create(self);
with EditArray[i] do
begin
// 属性编写
top:=....
//事件编写
OnKeyPress:=...
end;
end;
注意事项,在定义它的top,height,left等属性时要小心设置,最好和i关联,不然会出现所有的edit重叠在一起,只见到一个的现象的