# Filling Wx::ListCtrl with contents

**URL:** <https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995>\
**Category:** wxRuby\
**Created:** [September 5, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995 "2009-09-05T14:59:41Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/u/Maximilian_BrSSS_ut)\
**Post date:** [September 5, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995/1 "2009-09-05T14:59:41Z")

</div>

Hi all,

I’ve got a question concerning ListCtrl\_virtual of wxRuby since I see no  
way filling the list with dynamic contents. I’m new to Ruby so I maybe  
have to apologise for my request.

The problem is, that I have to define the on\_get\_item\_text(item, col)  
function. There I would like to use the item and col variables to read  
data from a two-dimensional array. My very problem is, that I do not  
know how to transfer the array from another function to  
on\_get\_item\_text(item, col) or better on\_get\_item\_text(item, col,  
array). I attached a rubyscript showing what I mean.

I would be very grateful, if you could give me a solution of this  
problem â€• maybe modify my testscript.

And/Or please show me how to use the approach via ListItems because the  
samplefile  
[http://wxruby.rubyforge.org/doc/samplelistctrl.html](http://wxruby.rubyforge.org/doc/samplelistctrl.html)  
at the website  
[http://wxruby.rubyforge.org/doc/listctrl.html](http://wxruby.rubyforge.org/doc/listctrl.html)  
is not accessible. I tinkered with it but it doesn’t really work.

Thank you very much in advance for your kind help.  
Kind regards,  
der Max

---

<div class="post-metadata">

**Author:** ![Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/u/Maximilian_BrSSS_ut)\
**Post date:** [September 5, 2009, 3:28pm UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995/2 "2009-09-05T15:28:22Z")

</div>

Hi

Maximilian BrÃ¤utigam wrote:

> The problem is, that I have to define the on\_get\_item\_text(item, col)  
> function. There I would like to use the item and col variables to read  
> data from a two-dimensional array. My very problem is, that I do not  
> know how to transfer the array from another function to  
> on\_get\_item\_text(item, col) or better on\_get\_item\_text(item, col,  
> array). I attached a rubyscript showing what I mean.

I’m not 100% sure this is what you want, but if you want the ListCtrl to  
access the data from the frame, you could just write (in your  
on\_get\_item\_text method)

parent.get\_contents.at(item).at(col)

“parent” is a method available on all Windows which returns the  
containing frame/panel/etc.

Or you could also split out your listctrl into a separate subclass.

Also, there is much simpler syntax than you’re using for event handlers,  
where you have

```
@listctrl.evt_list_item_selected(@listctrl.get_id()) {|event|

```

on\_item\_selected(event)}

you could just writen

evt\_list\_item\_selected(@listctrl, :on\_item\_selected)

hth  
alex

---

<div class="post-metadata">

**Author:** ![Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/u/Maximilian_BrSSS_ut)\
**Post date:** [September 5, 2009, 8:22pm UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995/3 "2009-09-05T20:22:47Z")

</div>

Alex F. wrote:

> 

Hi Alex,

Is there any sample code for a Wx::ListCtrl? On this page:

[http://wxruby.rubyforge.org/doc/listctrl.html](http://wxruby.rubyforge.org/doc/listctrl.html)

the link to the sample code located here:

[http://wxruby.rubyforge.org/doc/samplelistctrl.html](http://wxruby.rubyforge.org/doc/samplelistctrl.html)

produces a 404 Not Found error.

On my own, I could not figure out how to add items to the different  
columns of a “report view” styled Wx::ListCtrl,

require ‘rubygems’  
require ‘wx’

class MyFrame \< Wx::Frame

def initialize  
super(nil,  
:title =\> “Test”,  
:size =\> [600, 400])

```
panel = Wx::Panel.new(self)

my_list = Wx::ListCtrl.new(panel,
                       :name => "Drinks",
                       :size => [400, 100],
                       :style => Wx::LC_REPORT)

my_list.insert_column(0, "Drinks")
my_list.insert_column(1, "Extras")

```

=begin  
my\_list.set\_item(0, 0, “coffee”)  
my\_list.set\_item(1, 0, “tea”)  
my\_list.set\_item(2, 0, “water”)  
=end

```
my_list.insert_item(0, "coffee")
my_list.insert_item(1, "tea")
my_list.insert_item(2, "water")

show

```

end

end

class MyApp \< Wx::App  
def on\_init  
MyFrame.new  
end  
end

MyApp.new.main\_loop

How would I add items to the second column?

---

<div class="post-metadata">

**Author:** ![Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/u/Maximilian_BrSSS_ut)\
**Post date:** [September 5, 2009, 9:16pm UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995/4 "2009-09-05T21:16:46Z")

</div>

Am Samstag, den 05.09.2009, 20:22 +0200 schrieb 7stud --:

> ```
> :name => "Drinks",
> 
> ```
> 
> =end  
> class MyApp \< Wx::App  
> def on\_init  
> MyFrame.new  
> end  
> end
> 
> MyApp.new.main\_loop
> 
> How would I add items to the second column?

Hi 7stud,

with the help of Alex I was able to modify my testscript so it’s  
working. Honestly, I’m not using ListItems but ListCtrl\_virtual.

Try it out.  
Kind regards,  
der Max

---

<div class="post-metadata">

**Author:** ![Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/u/Maximilian_BrSSS_ut)\
**Post date:** [September 6, 2009, 8:32pm UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995/5 "2009-09-06T20:32:18Z")

</div>

hi

7stud – wrote:

> On my own, I could not figure out how to add items to the different  
> columns of a “report view” styled Wx::ListCtrl,

…

> How would I add items to the second column?

Call insert\_item first to create the row, then use set\_item to set the  
text in the secondary columns. In your sample, something like:

```
my_list.insert_item(0, "coffee")
my_list.set_item(0, 1, "milk")
my_list.insert_item(1, "tea")
my_list.set_item(1, 1, "sugar")
my_list.insert_item(2, "water")
my_list.set_item(2, 1, "bubbles")

```

alex

---

<div class="post-metadata">

**Author:** ![Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a87d85/32.png) [@Maximilian\_BrSSS\_ut](https://www.ruby-forum.com/u/Maximilian_BrSSS_ut)\
**Post date:** [September 7, 2009, 1:29am UTC](https://www.ruby-forum.com/t/filling-wx-listctrl-with-contents/196995/6 "2009-09-07T01:29:19Z")

</div>

Alex F. wrote:

> Call insert\_item first to create the row, then use set\_item to set the  
> text in the secondary columns. In your sample, something like:
> 
> ```
> my_list.insert_item(0, "coffee")
> my_list.set_item(0, 1, "milk")
> my_list.insert_item(1, "tea")
> my_list.set_item(1, 1, "sugar")
> my_list.insert_item(2, "water")
> my_list.set_item(2, 1, "bubbles")
> 
> ```
> 
> alex

Thanks Alex. Can you fix that broken link as well?
