How can I add and delete items from a list

Before you can display a list, you need to add text information to it. In an icon list, you can add new items as icons, strings, or both. In a report view, you can also set information for the different columns in the row after you set the initial icon and/or string. The API and naming conventions for the item manipulation methods differ from those used by some of the other controls we've seen so far, so even if you already understand how menus or list boxes work, you still should read this section.

Adding text information to the list control is a one step process for an icon list, but a multi-step process for a report list. The first step, common to each list, is adding the first item in the row. For report lists, you must separately add the columns and the information in columns other than the leftmost one.

Adding a new row

To add a new row, use one of the Insertltem() methods. Which one you use depends on the kind of item you are inserting. If you are just inserting a string into the list, use InsertStringItem(index, label), where the index is the row in the list where the new item will be displayed. If you just have an image, then use InsertImageItem(index, imagelndex). In this case, the first index is the row in the list control, and the imagelndex is an index into the image list attached to the list control. To insert an image item, the image list must already have been created and assigned. If the image index you use is out of bounds in the image list, then you'll get a blank image. If you want to add an item that has both an image and a string label, use InsertImageStringItem(index, label, imagelndex). This method combines the parameters of the previous two such that the index is still the index within the list control, label is the string display, and imagelndex is the index into the appropriate image list.

Internally, the list control manages information about its items by using instances of the class wx.Listltem. We mention that because the last method for inserting items to the list control is InsertItem(index, item), where item is an instance of wx.Listltem that you have already created. We're not going into very much detail about list items here because it's unlikely you'll be using them much, and because the class is not very complicated—it's almost completely made up of getter and setter methods. Nearly all of the properties of a list item are exposed via methods in the list control itself.

Adding columns

To add columns to a list control in report mode, create the columns, then set the individual data cells for each row/column pair. Creating columns is done with the lnsertColumn() method, which has the following signature.

InsertColumn(col, heading, format=wx.LIST_FORMAT_LEFT, width=-1)

In this method call, the col parameter is the index of the new column within the list—as with rows, you must supply this value. The heading parameter is the string text at the head of the column. The format parameter controls the alignment of the text in that column, acceptable values are wx.list_format_centre, wx.LlST_FORMAT_LEFT, and wx.list_format_right. The width parameter is the initial display width of the column in pixels—the user can change that by dragging the column header edges around. To use a wx.Listltem object to set the column, there is also a version of this method named Insertcolumnlnfo(info), which takes a list item as the parameter.

Was this article helpful?

0 0

Post a comment