下面第一段是cunstomdraw的,一访问subitems就出错,第二段没出错procedure TForm1.ListView1AdvancedCustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; Stage: TCustomDrawStage;
  var DefaultDraw: Boolean);
var
  i: integer;
  rect, BoundRect: TRect;
begin  BoundRect:=Item.DisplayRect(drBounds);
  if cdsFocused in State then
  begin
    Sender.Canvas.Brush.Color := clRed;  end
  else
  begin
    Sender.Canvas.Brush.Color := clGray ;
  end;
   Sender.Canvas.Font.Color :=clWhite ;
   ListView1.Canvas.FillRect(BoundRect);
  for i:=0 to ListView1.Columns.Count -1 do
  begin
    ListView_GetSubItemRect(Sender.Handle,Item.Index ,i,LVIR_LABEL,@rect);
    case i of
    0:
    begin
      DrawText(ListView1.Canvas.Handle,PChar(Item.Caption),-1,rect,DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or 0);
    end;
    1:
    begin
      DrawText(ListView1.Canvas.Handle,PChar(Item.SubItems[i-1]),-1,rect,DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or 0);//出错 在这里
    end;
   end;
  end;
end;可以正常访问procedure TForm1.btn_1Click(Sender: TObject);
begin
  ShowMessage(ListView2.Items.Item[1].caption);
 ShowMessage(ListView2.Items.Item[1].SubItems[0]);
end;

解决方案 »

  1.   


      1:
        begin
          DrawText(ListView1.Canvas.Handle,PChar(Item.SubItems[i-1]),-1,rect,DT_VCENTER or DT_SINGLELINE or DT_END_ELLIPSIS or 0);//出错 在这里
        end;
    错误是这个,当我访问这个子项的时候,报错 ‘list index out of bounds(0)’但是当我直接访问
    ShowMessage(ListView2.Items.Item[1].SubItems[0]);采样上面这个语句的时候就不报错,好奇怪
      

  2.   

    Item[0].SubItems[0]呢?
    Item[2].SubItems[0]呢?
    Item[3].SubItems[0]呢?
      

  3.   

    你的代码是ListView1, 正常的是ListView2呀ListView1中并没有添加子项吧,即SubItems.Add();
      

  4.   


    我的listview2是从listview1复制过来的,为了方便,就在页面上放了两个listview,呵呵,动态增加才会用到subitems.add,
    我是在原来编辑items的时候,就把子项写进去了
      

  5.   

    设计期编辑的??? 那肯定有没添加的子项比如,你看到这样的效果
    A B C  //表头
    ---------
    A1 B1 C1
    A2
    A3 B2则Item[1].SubItems[0]、 Item[1].SubItems[1] 、Item[2].SubItems[1]都是不存在的子项
    访日时就会出错,即使没有值,你也要添加进去(New SubItem、caption可以不填)
      

  6.   

    你应该先判断一下是否为caption先
      

  7.   

    先判断 items.Count > 0