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运行后出错 不知道错在哪里?

解决方案 »

  1.   

    找到的Components[Zs]未必是TShape类型的组件
    执行前先判断if Components[Zs] is TShape then
      

  2.   

    楼上说的对
    procedure TForm1.BitBtn1Click(Sender: TObject);
    begin
      if Components[Zs] is TShape then
        TShape(Components[Zs]).Pen.Color:=clred;
    end;
      

  3.   

    procedure TForm2.Button2Click(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;
      //TShape(Self.Components[1]).Pen.Color:=clred;
    end;
    楼主的方法中,components索引为1的不一定是TShape型的,这样转换不太对。
    我试了用as强制转换也不行,
    我的问题是:如果用类型转换是不是两个类型要用共同的父类啊?
    多态的意思,是不是也要类型有共同的父类啊?
      

  4.   

     我寫了半天的回貼,來了個超時。CSDN到底什麼時候才不會這樣啊
      

  5.   

    procedure TForm1.BitBtn1Click(Sender: TObject); 
    begin 
      if Components[Zs] is TShape then 
        TShape(Components[Zs]).Pen.Color:=clred; 
    end;
    还需要判断一下才可以!
      

  6.   

    控件创建的顺序是按照dfm文件里的顺序来的,别自己瞎猜
      

  7.   

    unit Unit1; interface uses 
      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)变色
      

  8.   


    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; 那就先判断后改变,改变一个后退出
      

  9.   

    你意思是说让TShape(Components[Zs])的颜色改变还是一个一个变啊?
    若是让TShape(Components[Zs])改变可以这么写:
     if Components[Zs] is TShape then 
        TShape(Components[Zs]).Pen.Color:=clred; 
      

  10.   


    这代码可以用 但我想问下  我showmessage后i出来的数字 好象不对啊 我按了第1下后应该i要=2啊 他是18?
      

  11.   


    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;