# Working with ListCtrl and ListItem

**URL:** <https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811>\
**Category:** wxRuby\
**Created:** [October 24, 2009, 11:33am UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811 "2009-10-24T11:33:11Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 24, 2009, 11:33am UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/1 "2009-10-24T11:33:11Z")

</div>

Hi List,

I’m trying to get my ListCtrl to work. Currently I’ve managed to fill  
my ListCtrl with items, but I think it’s not the most beautiful way:

class MyList \< Wx::ListCtrl

def initialize(parent, \*args)  
super(parent, :style =\> Wx::LC\_REPORT|LC\_HRULES )  
create\_layout  
fill\_list  
end

def create\_layout  
self.insert\_column(0, “Name”)  
self.insert\_column(1, “Code”)  
self.insert\_column(2, “Desc”)  
self.set\_column\_width(0,80)  
self.set\_column\_width(1,100)  
self.set\_column\_width(2,150)  
end

def fill\_list  
i=0  
DataObject.find(:all).each do |data|  
self.insert\_item(i, data.name)  
self.set\_item(i,1,data.code)  
self.set\_item(i,2,data.description)  
i = i + 1  
end  
end

I think it would be better to subclass Wx::ListItem and add instances  
of this class to the List. Something like this (not tested):

class MyListItem \< Wx::ListItem  
def initialize(data\_object, \*args)  
super(\*args)  
# here I need to add the column data, but I don’t know how  
end  
end

my\_list.add\_item(MyListItem.new(data\_object))

But I have no idea how I can fill the columns in my ListItem-Object.  
Any ideas how I can do this? I think it should work with  
ListItem#set\_data and/or ListItem#set\_column but I don’t understand  
the docs.

Regards,  
Timo

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 24, 2009, 1:02pm UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/2 "2009-10-24T13:02:09Z")

</div>

hi timo

Timo S. wrote:

…

> my\_list.add\_item(MyListItem.new(data\_object))
> 
> But I have no idea how I can fill the columns in my ListItem-Object.  
> Any ideas how I can do this? I think it should work with  
> ListItem#set\_data and/or ListItem#set\_column but I don’t understand  
> the docs.

Yes, the wxWidgets API for working with multiple columns is not so  
obvious. You do need to have one ListItem for every single cell - you  
can’t combine several columns of a single row into one. Have a look at  
this thread:

[http://rubyforge.org/pipermail/wxruby-users/2009-September/004955.html](http://rubyforge.org/pipermail/wxruby-users/2009-September/004955.html)

If you have an underlying data model eg linked to a RDBMS, it can be a  
lot tidier to use Wx::LISTCTRL\_VIRTUAL, and supply a set of methods to  
populate the listctrl automatically. Take a look at the samples dir  
(there’s an example in sapmles/bigdemo I think).

cheers  
alex

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 27, 2009, 12:43am UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/3 "2009-10-27T00:43:40Z")

</div>

Hi Alex,

Am 24.10.2009 um 13:01 schrieb Alex F.:

> Yes, the wxWidgets API for working with multiple columns is not so  
> obvious. You do need to have one ListItem for every single cell -  
> you can’t combine several columns of a single row into one. Have a  
> look at this thread:

Because of the un-sortable VIRTUAL list style I took a deeper look  
into the normal ListCtrl. You wrote, that I need one ListItem for  
every single cell. After reading the docs I thought I understand how  
it works, but I still can’t get it to do what I want.

I _thought_ the following code would create a ListCrtl with two  
columns populated with one line filling both columns with some text.  
But both items (item0 and item1) will put it’s text into the first  
cell (column 0). It seems like “self.set\_column(0)” doesn’t do  
anything… any ideas?

class MyList \< Wx::ListCtrl

def initialize()  
super(:style =\> Wx::LC\_REPORT|Wx::LC\_HRULES)  
self.insert\_column(0, “Col1”)  
self.insert\_column(1, “Col2”)  
self.set\_column\_width(0,280)  
self.set\_column\_width(1,300)  
add\_entries  
end

def add\_entries  
item0 = Wx::ListItem.new  
item0.set\_text(“Some text for column 0”)  
item0.set\_column(0)  
item0.set\_id(0)  
self.insert\_item(item0)

```
   item1 = Wx::ListItem.new
   item1.set_text("Some other text for column 1")
   item1.set_column(1)
   item1.set_id(0)
   self.insert_item(item1)

```

end  
end

Regards,  
Timo

P.S.: I know I could use self.insert\_item and self.set\_item in this  
case (and it would work), but later I would like to move the creatin  
of the list items to an own class.

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 27, 2009, 3:34pm UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/4 "2009-10-27T15:34:38Z")

</div>

Hi,

I did some more testing and I’m quite sure that there’s a bug in  
wx::ListCtrl and/or in wx::ListItem when using ListCtrl#insert\_item to  
insert a ListItem into a List element.

I attached the some sample code and two screenshot showing. The code  
should in my opinion create a 3x3 ListCtrl with every cell filled with  
some sample text.

Screenshot #1 shows the result running the script in a linux box using  
wxruby 2.0.0  
Screenshot #2 shows the result running the script in Mac OS X with  
wxruby 2.0.1 (same as with 2.0.0)

Both result are not what I expected…

Timo

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 27, 2009, 9:22am UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/5 "2009-10-27T09:22:52Z")

</div>

Hi,

this morning I tested my code on a Linux box. The result is a little  
bit different. On my linux box it seems, that the set\_column call is  
working, but every “self.insert\_item” is creating a new row, although  
both items should be in the same row (set\_id is both times called with  
0).

I Just checked the versions:

Mac OS: wxruby 2.0.1  
Linux: wxruby 2.0.0

Seems like I’m trying something that should not work or there’s a bug  
in at least one wxruby version

Regards,  
Timo

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 27, 2009, 4:12pm UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/6 "2009-10-27T16:12:42Z")

</div>

Timo S. wrote:

> I did some more testing and I’m quite sure that there’s a bug in  
> wx::ListCtrl and/or in wx::ListItem when using ListCtrl#insert\_item to  
> insert a ListItem into a List element.

This has tripped me up before. The wxWidgets default docs are not very  
clear, I will try to remember to update this evening.

insert\_item inserts a new row; use set\_item to add subsequent columns to  
the same row:

```
row00 = Wx::ListItem.new
row00.set_text("Cell 0,0") # set text
row00.set_id(0) # set row
row00.set_column(0) # set column
self.insert_item row00

row01 = Wx::ListItem.new
row01.set_text("Cell 0,1") # set text
row01.set_id(0) # set row
row01.set_column(1) # set column
# SET_ITEM here
self.set_item row01

```

See:

> **[wxListCtrl - WxWiki](https://wiki.wxwidgets.org/WxListCtrl)**

a

---

<div class="post-metadata">

**Author:** ![pixellover](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/ee59a6/32.png) [@pixellover](https://www.ruby-forum.com/u/pixellover)\
**Post date:** [October 27, 2009, 4:41pm UTC](https://www.ruby-forum.com/t/working-with-listctrl-and-listitem/177811/7 "2009-10-27T16:41:24Z")

</div>

Hi Alex,

Am 27.10.2009 um 16:12 schrieb Alex F.:

> Timo S. wrote:
> 
> > I did some more testing and I’m quite sure that there’s a bug in  
> > wx::ListCtrl and/or in wx::ListItem when using ListCtrl#insert\_item  
> > to insert a ListItem into a List element.
> 
> This has tripped me up before. The wxWidgets default docs are not  
> very clear, I will try to remember to update this evening.
> 
> insert\_item inserts a new row; use set\_item to add subsequent  
> columns to the same row:

That did the trick. Actually you can use ListCtrl#set\_item instead of  
ListCtrl#insert\_item all the time. Even for the first column you can  
use set\_item. That’s exactly what I was looking for. Thanks.

Regards,  
Timo
