Re: Adding items to a wxListCtrl by column

new topic     » goto parent     » topic index » view thread      » older message » newer message

Keep in mind that the index value for rows and columns is zero-based. I see you're using "i" and I have a feeling that's a one-based index. Try changing it to i-1 in those calls to insert_listctrl_item(). And I am guessing that the call to db_record_data() returns a sequence of values representing columns, so you probably don't want that wrapped in curly brackets, hence the "all in col 0" problem.

The simplest method is to insert the values as such:

integer result = insert_listctrl_item( myLV, row_index, {"Column 1", "Column 2", "Column 3", etc.}, image_id ) 
if result = -1 then 
    -- something went wrong, result should equal row_index. 
end if 

Here is a method you could use to append items instead of inserting them (so you don't have to track the last row index). It just inserts the item using the current count of items, which is always the end of the list.

function append_listctrl_item( atom list, sequence text, integer image ) 
    return insert_listctrl_item( list, list_count(list), text, image ) 
end function 

If you really want to two-step the process, then you first have to insert an empty row with insert_listctrl_item() and then you manipulate the individual cells (column values) using set_list_item(). This is pretty well tedious, though, and probably causes performance issues when inserting a lot of items at once. The set_list_item() routine is probably best used only when updating specific cells is required.

Here is an example using all three methods (insert, append, or two-step).

include wxeu/wxeud.e 
 
constant NULL = 0 
constant wxID_ANY = -1 
 
constant 
    Main = create( wxFrame, {NULL, wxID_ANY, "Presidents", -1, -1, 640, 480} ), 
    LV1  = create( wxListCtrl, {Main, wxID_ANY, -1, -1, -1, -1, wxLC_REPORT+wxNO_BORDER} ), 
$ 
 
function append_listctrl_item( atom list, sequence text, integer image ) 
    return insert_listctrl_item( list, list_count(list), text, image ) 
end function 
 
constant EMPTY_ROW = {"","","","",""} -- five empty columns 
 
insert_list_column( LV1, 0, "Name", NULL, 120 ) 
insert_list_column( LV1, 1, "Took Office", NULL, 120 ) 
insert_list_column( LV1, 2, "Left Office", NULL, 120 ) 
insert_list_column( LV1, 3, "Party", NULL, 120 ) 
insert_list_column( LV1, 4, "Vice President(s)", NULL, 120 ) 
 
-- insert a row manually 
integer row0 = insert_listctrl_item( LV1, 0, {"George Washington","1789","1797","No party","John Adams"}, NULL ) -- row0 
 
-- append a row automatically (no index required) 
integer row1 = append_listctrl_item( LV1, {"John Adams", "1797", "1801", "Federalist", "Thomas Jefferson"}, NULL ) -- row 1 
 
-- two-step the process: insert then update 
integer row2 = insert_listctrl_item( LV1, 2, EMPTY_ROW, NULL ) -- row 2 
set_list_item( LV1, row2, 0, "Thomas Jefferson", NULL ) 
set_list_item( LV1, row2, 1, "1801", NULL ) 
set_list_item( LV1, row2, 2, "1809", NULL ) 
set_list_item( LV1, row2, 3, "Democratic-Republican", NULL ) 
set_list_item( LV1, row2, 4, "Aaron Burr, George Clinton", NULL ) 
 
wxMain( Main ) 

Hope this helps,

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu