Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TListview OnCustomDrawItem

Status
Not open for further replies.

safra

Technical User
Jan 24, 2001
319
NL
Hi,

I have a TListView with an OnCustomDrawItem event. Visibility of Column 9 is set to false. This column is used as a flag in the OnCustomDrawItem event which looks like this:

Code:
procedure TEmailClientForm.ListView1CustomDrawItem(Sender: TCustomListView;
  Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
  if Item.SubItems.Strings[9] = '0' then
    lvHeaders.Canvas.Font.Style := [fsBold]
  else
     lvHeaders.Canvas.Font.Style := [];
end;


In one part of the form that fills the listview with data this is working fine.

But now I am working on a different part that uses a similar approach to fill the listview with data and now I get the error:

List index out of bounds[9]

I double checked and I am sure that the procedure to fill the listview is similar to the one that is working. I don't understand why I get the error?

procedure add items to listview:

Code:
itm := ListView1.Items.Add;
itm.ImageIndex := -1;
itm.Caption := '';
itm.SubItems.Add('FieldByName('Name').Value');
itm.SubItems.Add('FieldByName('Address').Value');
itm.SubItems.Add('FieldByName('Postcode').Value'); 
etc...

Any ideas why I get the error now?

Thanks,
Raoul
 
Code:
List index out of bounds[9]
is produced when your code is trying to access the 10th item when it doesn't actually exist.

I would guess that the second procedure that fills the listview is at fault. Run your program under the debugger and put a breakpoint in the second procedure to check that the ListView is being set up as you expect.

If you still have a problem then post the relevant code to this thread.

Andrew
Hampshire, UK
 
List index out of bounds[9]

is produced when your code is trying to access the 10th item when it doesn't actually exist.

This is actually what I expected to happen when starting to implement the code in the OnCustomDrawItem as the manual says that OnCustomDrawItem occurs immediately prior to the rendering of each list item. But it works fine in the first procedure.

The 2nd procedure really looks similar as the first. The only difference is that procedure 1 populates the list view with data from a dataset after doing:

ListView1.Items.Clear;

And the 2nd procedure adds individual records at the bottom of the listview after a record has been added to the dataset.

I solved the problem by not adding indivual records to the listview but instead execute the first procedure and refresh the complete listview each time a record has been added to the dataset. But I still wonder why I got these errors?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top