假设有如下数组:
Type 
   TPaperInfo=packed Record
       PaperName: string;
       PaperSize: integer;
       PaperWidth:integer;
       PaperHeight: integer;
end;var 
   PaperInfos: array of TPaperInfo;   SetLength(PaperInfos,10);   for i:=Low() to High() do
   begin
     //为数组各元素赋值
   end; 窗体中有个comboBox1, 我想把数组中各元素作为TObject插入到该组合框的Objects列表中, 以及在需要时从各个Object中取出各元素的成员
请问各位大侠有可以这么做吗??或者有其它折冲的办法???

解决方案 »

  1.   

    for i:=Low() to High() do
       begin
         ComboBox1.Items.Add('');
         ComboBox1.Items.Objects[i] := PaperInfos[i];
       end;建议TPaperInfo定义为class,而非record
      

  2.   

    我知道用Items.addItem()方法,但不知道该如何把数组元素转换成TObject, TObject转换成数组元素
      

  3.   

    可以的,不过程序要改一下:
       PPaperInfo=^TpaperInfo;//加一行,定义其指针类型
       TPaperInfo=packed Record
           PaperName: string;
           PaperSize: integer;
           PaperWidth:integer;
           PaperHeight: integer;
    end;以后可以使用combobox.items.addobject(@PaperInfos[i]);取出:
    var p : PPaperInfo;
    ...
    p := combobox.item.objects[i];
      

  4.   

    但我需要数组,这样可以吗?' var arr : array of class ;' 这样的语法可以吗???我目的是要用GetDevCapabilitiesA()函数获得打印机支持的所有纸张,放在一个数组中. 你有更好的方法吗?
      

  5.   

    Type 
       TPaperInfo=class
           PaperName: string;
           PaperSize: integer;
           PaperWidth:integer;
           PaperHeight: integer;
      end;PaperInfos: array of TPaperInfo;TPaperInfo类就已经是一个TObject了,可以直接ComboBox1.Items.Objects[i] := PaperInfos[i];取出的时候:if ComboBox1.Items.Objects[i] is TPaperInfo then
      with ( ComboBox1.Items.Objects[i] as TPaperInfo ) do
      begin  
       ...
      end;