为什么小弟我访问listview的subitems的内容会出错呢,求解
为什么我访问listview的subitems的内容会出错呢,求解
下面第一段是cunstomdraw的,一访问subitems就出错,第二段没出错
可以正常访问
------解决方案--------------------
设计期编辑的??? 那肯定有没添加的子项
比如,你看到这样的效果
A B C //表头
---------
A1 B1 C1
A2
A3 B2
则Item[1].SubItems[0]、 Item[1].SubItems[1] 、Item[2].SubItems[1]都是不存在的子项
访日时就会出错,即使没有值,你也要添加进去(New SubItem、caption可以不填)
下面第一段是cunstomdraw的,一访问subitems就出错,第二段没出错
- Delphi(Pascal) code
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;
可以正常访问
- Delphi(Pascal) code
procedure TForm1.btn_1Click(Sender: TObject); begin ShowMessage(ListView2.Items.Item[1].caption); ShowMessage(ListView2.Items.Item[1].SubItems[0]); end;
------解决方案--------------------
设计期编辑的??? 那肯定有没添加的子项
比如,你看到这样的效果
A B C //表头
---------
A1 B1 C1
A2
A3 B2
则Item[1].SubItems[0]、 Item[1].SubItems[1] 、Item[2].SubItems[1]都是不存在的子项
访日时就会出错,即使没有值,你也要添加进去(New SubItem、caption可以不填)