unit Unit1;interfaceuses
TzSz,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons, Menus, ToolWin, ActnMan, ActnCtrls,
ActnMenus, WinSkinData, SkinCaption;type
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Shape5: TShape;
Shape6: TShape;
Shape7: TShape;
Shape8: TShape;
Shape9: TShape;
Shape10: TShape;
Shape11: TShape;
Shape12: TShape;
Shape13: TShape;
Shape14: TShape;
Shape15: TShape;
Shape16: TShape;
Shape17: TShape;
Shape18: TShape;
Shape19: TShape;
Shape20: TShape;
Shape21: TShape;
Shape22: TShape;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel; private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
J:Integer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
这里该怎么写?
end;end.我想问的是 J 是一个随机的数字
Click后
Label[J].caption:=inttostr(J)
Shape[J].Width:=J
这2句我改怎么写?
TzSz,Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons, Menus, ToolWin, ActnMan, ActnCtrls,
ActnMenus, WinSkinData, SkinCaption;type
TForm1 = class(TForm)
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Shape5: TShape;
Shape6: TShape;
Shape7: TShape;
Shape8: TShape;
Shape9: TShape;
Shape10: TShape;
Shape11: TShape;
Shape12: TShape;
Shape13: TShape;
Shape14: TShape;
Shape15: TShape;
Shape16: TShape;
Shape17: TShape;
Shape18: TShape;
Shape19: TShape;
Shape20: TShape;
Shape21: TShape;
Shape22: TShape;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Label7: TLabel;
Label8: TLabel;
Label9: TLabel;
Label10: TLabel;
Label11: TLabel;
Label12: TLabel;
Label13: TLabel;
Label14: TLabel; private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
J:Integer;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);
begin
这里该怎么写?
end;end.我想问的是 J 是一个随机的数字
Click后
Label[J].caption:=inttostr(J)
Shape[J].Width:=J
这2句我改怎么写?
-------------------------------------
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls,Math;type
TForm1 = class(TForm)
Button1: TButton;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
Pan:array [1..22] of TPanel;
Lb:array [1..14] of TLabel;
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);//创建数组控件
var
i,m,r:integer;
begin
m:=0;
r:=0;
For i:=1 to 22 do
begin
r:=r+1;
pan[i]:=TPanel.Create(Self);
pan[i].Name:='pan'+IntToStr(i);
pan[i].Parent:=Self;
pan[i].Caption:=IntToStr(i);
Pan[i].Left:= (r-1)*40;
pan[i].Top:=m*40;
pan[i].Width:=40;
pan[i].Height:=40;
if (r mod 5=0) Then
begin
m:=m+1;
r:=0;
end;
end;
r:=0;
m:=m+1; for i:=1 to 14 do
begin
r:=r+1;
Lb[i]:=TLabel.Create(Self);
Lb[i].Name:='LB'+IntToStr(i);
Lb[i].Parent:=Self;
Lb[i].Caption:='Label';
Lb[i].Left:= (r-1)*40;
Lb[i].Top:=m*40;
Lb[i].Width:=40;
Lb[i].Height:=40;
if (r mod 5=0) Then
begin
m:=m+1;
r:=0;
end;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
j:integer;
begin
randomize;
j:=RandomRange(1,14);
Lb[j].Caption:=IntToStr(j);
Pan[j].Width:=j;
end;end.
-------------------------------------------------------
-------------------------------------------
j:=RandomRange(1,14); TLabel(Components[j-1]).Caption := inttostr(j);
TShape[Components[j-1]).width:=j;
------------------------------------------------
如果是TLabel,j:=RandomRange(1,14);
如果是TShape,j:=RandomRange(1,22);
1.建立控件数组
2.用运行时uses
TypInfo;var
PropInfo: PPropInfo; // 要使用 TypInfo 单元
j: integer;
begin
for j := 0 to Self.ControlCount-1 do
begin
if Self.Controls[j] is TShape then
begin
propInfo := GetPropInfo(Self.Controls[j], 'Width');
if propInfo <> nil then // 存在属性
self.Controls[j].Width := j;
end;
end;
end;
begin
Randomize;
j:=RandomRange(1,14);
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons, CheckLst;type
myCom = Record
lab : TComponent;
shape : TComponent;
end;type
TForm2 = class(TForm)
CheckListBox1: TCheckListBox;
BitBtn1: TBitBtn;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Shape5: TShape;
Shape6: TShape;
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form2: TForm2;
Collection :array of myCom;
implementation{$R *.dfm}procedure TForm2.BitBtn1Click(Sender: TObject);
procedure CollectionList;
var
i,J : integer;
begin
J := 0;
for i := 0 to Self.ComponentCount -1 do
begin
if Components[i] is TLabel then begin
inc(j);
SetLength(Collection,length(Collection)+1);
Collection[j].lab := Components[i];
end;
end; J := 0;
for i := 0 to Self.ComponentCount -1 do
begin
if Components[i] is TShape then begin
inc(j);
Collection[j].shape := Components[i];
end;
end;
end;
var
J : integer;begin
Randomize;
j := Random(6);
CollectionList; TLabel(Collection[j].lab).Caption := Inttostr(j);
TShape(collection[j].shape).Width := J;end;end.
這樣來處理,LABLE與對應的SHAPE對應起來。不過你可以考慮用更好的方法去處理更快。建議用DELPHI控件都帶的TAG來處理, 比如LABEL1與SHAPE1相對應的話,那麼他們的TAG就都用1,翠此類推,我貼的代碼必須要保證ORDER順序對應也可以
var a,b:Integer;
begin
Randomize;
a:=RandomRange(1,14);
b:=RandomRange(1,22);
(FindComponent('Label'+IntToStr(a)) as TLabel).caption:=inttostr(a) ;
(FindComponent('Shape'+IntToStr(b)) as TLabel).Width:=b;
end;