Delphi中没有提供现成的控件数组,这使每一个由VB转型到DELPHI程序员都会不由自主的怀念VB中那无处不在的控件数组以及控件数组给我们带来的方便。那么DELPHI中是不是就没有办法使用控件数组了呢?办法是有的,在很多DELPHI的教材中都提到一个方法:TLISTSTRING;使用TLISTSTRING类控制容器中的控件以达到使用控件数组的目的;另外一种途径是由用户自己来开发带数组性质的VCL组件。这两种方法对DELPHI的是初级入门者是不现实的。使用TLISTSTRING类代码量大,难以阅读,使用时非常吃力;由用户自己制作组件这又不是DELPHI初级使用者所能完成的工作。下面我给大家介绍介绍我在使用DELPHI中用控件数组的一点小经验。
在DELPHI的每一个VCL组件中都带有一个属性:COMPONENTINDEX;利用这个属性就可以实现控件数组
use
windows,forms,messages,dialogs,controls,comobj,db,sysutils;
interface
type
Form1=Class(TForm)
Edit1:TEdit;
Edit2:TEdit;
Edit3:TEdit;
Edit4:TEdit;
Button1:TButton;
Button2:TButton;
Button3:TButton;
procedure TForm1.Button1(Sender:TObject);
Procedure TForm1.Edit1Exit(Sender:TObject);
Procedure TForm1.Edit1Enter(Sender:TObject);
private
Public
end;
var
Form1:TForm;
Impmeintation;
Procedure TForm1.Edit1Enter(Sender:TObject);
begin
(Sender As TEdit).Color:=ClInfo;
file://改变Edit获得焦点时的背景颜色为黄色
end;
Procedure TForm1.Edit1Exit(Sender:TObject);
Begin
(Sender As TEdit).Color:=ClWhite;
file://改变Edit失去焦点时的背景颜色为白色
End;
file://设置窗体中所有的Edit背景颜色为ClLime
Procedure TForm1.Button1(Sender:TObject);
var
Index:Integer;
Begin
For Index:=0 to ControlCount-1 do
Begin
if Components[Index] is TEdit then
begin
(Components[Index] As TEdit).Color:=ClLime;
end
End
End;
转载于:https://www.cnblogs.com/myamanda/articles/1539380.html