Working with ListCtrl and ListItem

Timo Springmann
Sat, 24 Oct 2009 09:17:09 -0000

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