# ListCtrl.get\_selections broken?

**URL:** <https://www.ruby-forum.com/t/listctrl-get-selections-broken/162283>\
**Category:** wxRuby\
**Created:** [March 8, 2009, 2:19am UTC](https://www.ruby-forum.com/t/listctrl-get-selections-broken/162283 "2009-03-08T02:19:44Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 8, 2009, 2:19am UTC](https://www.ruby-forum.com/t/listctrl-get-selections-broken/162283/1 "2009-03-08T02:19:44Z")

</div>

Hi again,

may it be that ListCtrl.get\_selections is broken? Or did I misunderstand  
sth. in the docs?

I’m always getting 0 as the size of the returned array while  
get\_selected\_item\_count is reporting the correct number.

See the attachment or the following lines for a program that  
demonstrates this.

require ‘wx’  
include Wx

class MyFrame \< Frame  
def initialize  
super nil, -1, “ListCtrl Error”

```
@list = ListCtrl.new(self, -1, DEFAULT_POSITION, DEFAULT_SIZE,

```

LC\_REPORT)  
@list.insert\_column(0, ‘abc’)

```
li = ListItem.new()
li.set_state LIST_STATE_SELECTED

li.set_text '123'
@list.insert_item(li)
li.set_text '234'
@list.insert_item(li)
li.set_text '345'
@list.insert_item(li)
li.set_text '456'
@list.insert_item(li)

puts @list.get_selected_item_count # --> 4
puts @list.get_selections.size # --> 0

```

end  
end

class MyApp \< App  
def on\_init  
f = MyFrame.new  
f.show  
end  
end

MyApp.new.main\_loop

I hope I’m doing something wrong.

Cheers,  
Christian.

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 8, 2009, 8:58am UTC](https://www.ruby-forum.com/t/listctrl-get-selections-broken/162283/2 "2009-03-08T08:58:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![caefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/e36b37/32.png) [@caefer](https://www.ruby-forum.com/u/caefer)\
**Post date:** [March 8, 2009, 2:09pm UTC](https://www.ruby-forum.com/t/listctrl-get-selections-broken/162283/3 "2009-03-08T14:09:25Z")

</div>

Alex F. wrote:

> 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.

This is great! I’ve always been annoyed by that, since I practically  
never need to explicitly set those using sizer based layout.

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

I didn’t use the ListItem helper-class in the original program. Just  
wanted to make it easier for you to reproduce the problem by selecting  
the items at startup. I was using @list.insert\_item(pos, ‘item’) and  
@list.set\_item(pos, col, ‘property’) originally.

> 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

I’ve been playing around with this now. I thought, it might also be a  
problem of references getting lost somewhere. So I modified it as  
follows:

```
@li1 = ListItem.new()
@li1.set_state LIST_STATE_SELECTED
@li1.set_text '123'
@li1.id = 0
@list.insert_item(@li1)

@li2 = ListItem.new()
@li2.set_state LIST_STATE_SELECTED
@li2.id = 1
@li2.set_text '234'
@list.insert_item(@li2)

...

```

But still no luck: get\_selections.size == 0!

Christian.
