你可以参照Delphi的VCL实例来写啊,应该问题不大。比如DBGRID中的TColumns就是一例。

解决方案 »

  1.   

    大家给我一个简单的例子吧,就建立一个collecion,有添加删除,就行,答案正确者的高分。
      

  2.   

    问题是,好像不需要,因为Delphi已经都做了。TCollection有Add,Delete,Insert等方法,有这些方法,难道还需要重写一遍吗?
      

  3.   

    你理解错了,我知道有这些功能,我的意思是建立一个collection的实例,我在建立过程中遇到了错误,不能实现。
      

  4.   

    啃了半天
    做了一个简单的例子,只要生成一个新工程,然后把代码全部覆盖就可以了 。
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;type
      TForm1 = class(TForm)
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
          Shift: TShiftState; X, Y: Integer);
        procedure FormPaint(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        List:TCollection;
      end;type
        TMyPoint=Class(TCollectionItem)
        private
            fx:Integer;
            fy:Integer;
        Public
            procedure Paint(Canvas:TCanvas);
            procedure Assign(P:TPersistent);Override;
        Published
            property x:Integer read fx write fx;
            property y:Integer read fy write fy;
        end;var
      Form1: TForm1;implementation{$R *.DFM}procedure TMyPoint.Assign(P:TPersistent);
    begin
            if p is TMyPoint then
            begin
                    fx:=TMyPoint(p).fx;
                    fy:=TMyPoint(p).fy;
            end
            else
                    inherited Assign(p);
    end;procedure TMyPoint.Paint(Canvas:TCanvas);
    begin
            Canvas.Pen.Color :=clRed;
            Canvas.Ellipse (fx-2,fy-2,fx+2,fy+2);
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
         List:=TCollection.Create (TMyPoint);
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
         List.Clear ;
         List.Free ;
    end;procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    var
       p:TMyPoint;
    begin
         p:=List.Add as TMyPoint;
         p.x:=x;
         P.y:=Y;
         Invalidate;
    end;procedure TForm1.FormPaint(Sender: TObject);
    var
            I:integer;
    begin
         For i:=0 to List.Count -1 do
            TMyPoint(List.Items[i]).Paint(Canvas);
    end;end.如果你要动态生成其他类,你要注意他是否有TCollection属性。
    否则你就要给他包个套子,添加一个TCollection属性。
      

  5.   

    感谢,wk_knife(我的脑袋也该升级了),,我要在麻烦一下,    p:=List.Add as TMyPoint;  每一个都要这样在声明一次么?(as tmypoint),很麻烦的。什么办法可以解决?
      
      

  6.   

    Add 不带参数,如果是类似以下定义Add(s:TCollectionItem)就可以不用as了。这个as是少不了的,否则会出现类型转换错误。
    我想你的问题是不是就在这呢?“建立后不能添加”
      

  7.   

    这个TCollectionItem很麻烦的。我也是一知半解,平时好象没什么用处,即使做控件时也相当复杂,做一个完善的带TCollectionItem控件,还要提供属性编辑器。很麻烦的。help上有几段例子,让人头大。