Understanding InsertStringItem in ListCtrl

The attached little program creates a ListCtrl in report style. The question I am trying to answer is: What does the first argument of
InsertStringItem represent?
The wxPython documentation says it is the “index”, without explaining what that means.
The wxWidgets documentation says it is the “index of the new item, supplied by the application.” It also says that this command “inserts an item,
returning the index of the new item if successful” but, in my example, the index returned isn’t the first argument of InsertStringItem for any row.
The book wxPython in Action says on page 403: “If you are just inserting a string item into the list, use InsertStringItem(index, label), where the
index is the row in the list where the new item will be displayed.” But in my example the row number is the displayed index, which differs from the
first argument of InsertStringItem on every row. Also, the example given on page 398 of the book uses sys.maxint (i.e., the largest integer that can
be represented) for every row and it can’t be that every row has the same number.

The tutorial at http://www.zetcode.com/wxpython/advanced/ says: “The first parameter of [InsertStringItem] specifies the row number.” This is wrong
for the reasons just given.

I tried playing around with different values for the first argument and found that lots of different combinations work and produce exactly the same
table. But negative numbers give an error. Also, if the first argument of InsertStringItem equals the row number of a preceding row (e.g., give number
0 or 1 to the last row) then rows have their order changed and different rows are assigned the same index.

But I still don’t know what the first argument of InsertStringItem represents. I don’t need to know but I’m curious and so I’d be grateful if someone
could tell me.

Patrick Maher
http://patrick.maher1.net

test.py (925 Bytes)

Patrick,

test.py (1.12 KB)

···

On 10/05/2011 09:33 PM, Patrick Maher wrote:

The attached little program creates a ListCtrl in report style. The question I am trying to answer is: What does the first argument of
InsertStringItem represent?

The wxPython documentation says it is the "index", without explaining what that means.

The wxWidgets documentation says it is the "index of the new item, supplied by the application." It also says that this command "inserts an item,
returning the index of the new item if successful" but, in my example, the index returned isn't the first argument of InsertStringItem for any row.

Maybe the attached will make it clearer, a slightly modified version of your code.

Another hint, if you use sys.maxint the item will be appended to the list:-) .

Werner

It is the position in the list where you want to insert the new item. It is not displayed in the listctrl unless you make it part of the data associated with one of the columns for that item. If you pass zero then the item will be positioned as the first item in the listctrl. If you pass some number larger than the number of items in the list then it will be appended at the end. That is when it is helpful to check the return value in case you need to know where it was actually inserted.

···

On 10/5/11 12:33 PM, Patrick Maher wrote:

The attached little program creates a ListCtrl in report style. The
question I am trying to answer is: What does the first argument of
InsertStringItem represent?

--
Robin Dunn
Software Craftsman

Thank you, Robin, that makes sense of everything. In particular: The index numbers shown by my program are the position at the time the item was added but the true index number (=row number) gets changed if I add another item with the same or a smaller index number.

Patrick