unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Shape5: TShape;
Shape6: TShape;
Shape7: TShape;
Shape8: TShape;
Shape9: TShape;
Shape10: TShape;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Zs:integer;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Zs:=1;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
TShape(Components[Zs]).Pen.Color:=clred;
end;
BitBtn1Click运行后出错 不知道错在哪里?
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons;type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Shape5: TShape;
Shape6: TShape;
Shape7: TShape;
Shape8: TShape;
Shape9: TShape;
Shape10: TShape;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
Zs:integer;
implementation{$R *.dfm}procedure TForm1.FormCreate(Sender: TObject);
begin
Zs:=1;
end;procedure TForm1.BitBtn1Click(Sender: TObject);
begin
TShape(Components[Zs]).Pen.Color:=clred;
end;
BitBtn1Click运行后出错 不知道错在哪里?
解决方案 »
- 在dbgrid如何实现列值随着别一列改变而改变
- P2P应用中的UDP文件传输出现问题。。。想请帮一下,有点急~~
- 如何得到组合的结果?
- Delphi怎么做关键字搜索(从数据表里某一列搜索)
- 兄弟姐妹们请帮帮我★★★★★★★★★★★★★★★★★★★★★★★★。。。。。。。。。。。。。。。。
- 怎样知道SQL server 2000的服务管理器是否正在运行?
- 为什么把我的客户端与应用程序服务器移到别的机子上,程序运行时就找不到应用程序服务器了呢?
- DBGrid+Excel如何实现?
- 数据库中的的'1','2'如何显示成'男'''女'
- 从网上邻居连接nt时在小窗口中输入用户名和口令的过程如何在程序中实现?
- 问一个关于treeview的问题,请各位帮帮忙
- 进入软件时,会出现[DBNETLIB] ConnetionOpen (Connect().) SQL Server 不存在或拒绝访问?如何解决
执行前先判断if Components[Zs] is TShape then
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
if Components[Zs] is TShape then
TShape(Components[Zs]).Pen.Color:=clred;
end;
var
i:integer;
begin
for i:=0 to self.ComponentCount - 1 do
begin
if Self.Components[i] is TShape then
TShape(Self.Components[i]).Pen.Color:=clred;
end;
//TShape(Self.Components[1]).Pen.Color:=clred;
end;
楼主的方法中,components索引为1的不一定是TShape型的,这样转换不太对。
我试了用as强制转换也不行,
我的问题是:如果用类型转换是不是两个类型要用共同的父类啊?
多态的意思,是不是也要类型有共同的父类啊?
begin
if Components[Zs] is TShape then
TShape(Components[Zs]).Pen.Color:=clred;
end;
还需要判断一下才可以!
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls, StdCtrls, Buttons; type
TForm1 = class(TForm)
GroupBox1: TGroupBox;
Shape1: TShape;
Shape2: TShape;
Shape3: TShape;
Shape4: TShape;
Shape5: TShape;
Shape6: TShape;
Shape7: TShape;
Shape8: TShape;
Shape9: TShape;
Edit1: TEdit;
Edit2: TEdit;
Edit3: TEdit;
Edit4: TEdit;
Edit5: TEdit;
Edit6: TEdit;
Edit7: TEdit;
Edit8: TEdit;
Edit9: TEdit;
procedure FormCreate(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; var
Form1: TForm1;
Zs:integer;
implementation {$R *.dfm}
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to self.ComponentCount - 1 do
begin
if Self.Components[i] is TShape then
TShape(Self.Components[i]).Pen.Color:=clred;
end; end;
这代码 click后 我所有的shape都变Red色 我要的是按1次 变1个 然后i:=i+1 那再click后 shape(i)变色
procedure TForm1.BitBtn1Click(Sender: TObject);
var
i:integer;
begin
for i:=0 to self.ComponentCount - 1 do
begin
if Self.Components[i] is TShape then
if TShape(Self.Components[i]).Pen.Color <> clred then
begin
TShape(Self.Components[i]).Pen.Color:=clred;
Break;
end;
end;
end; 那就先判断后改变,改变一个后退出
若是让TShape(Components[Zs])改变可以这么写:
if Components[Zs] is TShape then
TShape(Components[Zs]).Pen.Color:=clred;
这代码可以用 但我想问下 我showmessage后i出来的数字 好象不对啊 我按了第1下后应该i要=2啊 他是18?
procedure TForm1.BitBtn1Click(Sender: TObject);
{$J+}{低版本的IDE如果不支持此参数则注释掉}
const
i:integer = 0;
{$J-} {低版本的IDE如果不支持此参数则注释掉}
begin
if i >= self.ComponentCount then i := 0;
while i < self.ComponentCount do
begin
if Self.Components[i] is TShape then begin
TShape(Self.Components[i]).Pen.Color:=clred;
break;
end;
end;