[wxruby-users] ListCtrl.get_selections broken?

Alex Fenton alex at pressure.to
Sun Mar 8 03:24:10 EDT 2009


Christian Schmidt wrote:
> I'm always getting 0 as the size of the returned array while
> get_selected_item_count is reporting the correct number.
>   

It works OK for me. I think the problem is the way you are adding items 
to the list.

>     @list = ListCtrl.new(self, -1, DEFAULT_POSITION, DEFAULT_SIZE,
> LC_REPORT)
>   

Note - this can be written ListCtrl.new(self, :style => LC_REPORT). You 
almost never need to use the clumsy 'DEFAULT_POSITION', 'DEFAULT_SIZE' 
etc. Use named arguments and these will be taken care of.

>     li = ListItem.new()
>     li.set_state 
>
>     li.set_text '123'
>     @list.insert_item(li)
>     li.set_text '234'
>     @list.insert_item(li)
>   

Your problem is here, I think. Each ListItem object represents a unique 
object in the list. The row it's in is identified by an attribute 'id'. 
However, you're adding the same object multiple times. Do it like this 
instead, creating a new ListItem for each:

li = Wx::ListItem.new
li.state = LIST_STATE_SELECTED
li.id = 0 # For row 0
li.text = '123'
@list.insert_item li

alex


More information about the wxruby-users mailing list