下面第一段是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;
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;
解决方案 »
- 请教delphi大牛
- 怎样不换页打印.
- 莫名其妙的invalid pointer operation
- 用SPCOMM怎样处理这样的串口通信协议???
- 权限高手进来啊!考验你们的时间到了(关于ActionList或 ActionManager 另类用法)
- 如何在dxDBGrid中动态加列????
- 是不是在窗体的showmodal后,以后代码会等窗体关闭后才执行
- 如何将ADOCONNECTION作为一个参数传给DLL
- 关于qtintf.dll的问题,进者有分
- form2.print打印似的字体很粗糙,请教如何解决?
- dll中动态添加的flash.ocx如何释放?
- delphi 比如Label1后加点后面不能自动展开
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]);采样上面这个语句的时候就不报错,好奇怪
Item[2].SubItems[0]呢?
Item[3].SubItems[0]呢?
我的listview2是从listview1复制过来的,为了方便,就在页面上放了两个listview,呵呵,动态增加才会用到subitems.add,
我是在原来编辑items的时候,就把子项写进去了
A B C //表头
---------
A1 B1 C1
A2
A3 B2则Item[1].SubItems[0]、 Item[1].SubItems[1] 、Item[2].SubItems[1]都是不存在的子项
访日时就会出错,即使没有值,你也要添加进去(New SubItem、caption可以不填)