Working with ListCtrl and ListItem

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

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

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

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.

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

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

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:
http://wiki.wxwidgets.org/WxListCtrl

a

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