# Refresh combobox context

**URL:** <https://www.ruby-forum.com/t/refresh-combobox-context/210819>\
**Category:** Ruby-Gnome 2\
**Created:** [August 18, 2011, 7:00am UTC](https://www.ruby-forum.com/t/refresh-combobox-context/210819 "2011-08-18T07:00:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [August 18, 2011, 7:00am UTC](https://www.ruby-forum.com/t/refresh-combobox-context/210819/1 "2011-08-18T07:00:30Z")

</div>

How to refresh combobox context?

I want to load a file which it have lots of header on each column,  
I put each header into combobox, let someone to select which header  
column  
does he want to parser.

When I re-select other files I want it refresh the combobox header  
context.

I used “clear” but it will cause the combobox empty.  
When I re-select other files it also show empty.

How to fix the problem?

Thanks.

---

<div class="post-metadata">

**Author:** ![cc\_c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/91b2a8/32.png) [@cc\_c](https://www.ruby-forum.com/u/cc_c)\
**Post date:** [August 18, 2011, 12:01pm UTC](https://www.ruby-forum.com/t/refresh-combobox-context/210819/2 "2011-08-18T12:01:58Z")

</div>

CC Chen wrote in post #1017232:

> How to refresh combobox context?

You mean : How to refresh items of a combobox ?

> I used “clear” but it will cause the combobox empty.  
> When I re-select other files it also show empty.

Gtk::Combobox#clear affects the combobox itself, not what it displays.  
That is, you are clearing the widget of its layout, not its content.

> How to fix the problem?

I guess you are using combobox with simple text. The only way is to  
iterate on items and remove them one by one. But I urge you not to do  
that.

If you want to update the combobox content, use a model. Take a look at  
the examples in the sources , gtk2/sample/misc.

Here is a sample for your needs, as I understand them :

require ‘gtk2’

model = Gtk::ListStore.new( String)  
[“foo”, “bar”, “baz”].each do |text|  
iter = model.append  
iter[0] = text  
end

combo = Gtk::ComboBox.new(model)

renderer = Gtk::CellRendererText.new  
combo.pack\_start(renderer, true)  
combo.set\_attributes(renderer, :text =\> 0)

combo.active = 1

combo.signal\_connect(“changed”) do |c|  
p “combo: #{c.active}, #{c.active\_iter[0]}” if c.active \> -1  
end

button = Gtk::Button.new(“Click me”)  
button.signal\_connect(“clicked”) do |button|  
model.clear  
[“ruby”, “python”, “perl”].each do |text|  
iter = model.append  
iter[0] = text  
end  
combo.active = 0  
end

window = Gtk::Window.new  
window.signal\_connect(“destroy”) { Gtk.main\_quit }  
vbox = Gtk::VBox.new  
vbox.add(combo).add(button)  
window.add(vbox).show\_all

Gtk.main

Hope it helps.

Simon

---

<div class="post-metadata">

**Author:** ![cc\_c](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/91b2a8/32.png) [@cc\_c](https://www.ruby-forum.com/u/cc_c)\
**Post date:** [August 23, 2011, 6:47am UTC](https://www.ruby-forum.com/t/refresh-combobox-context/210819/3 "2011-08-23T06:47:53Z")

</div>

Simon A. wrote in post #1017273:

> CC Chen wrote in post #1017232:

> Here is a sample for your needs, as I understand them :
> 
> require ‘gtk2’
> 
> model = Gtk::ListStore.new( String)  
> [“foo”, “bar”, “baz”].each do |text|  
> iter = model.append  
> iter[0] = text  
> end
> 
> combo = Gtk::ComboBox.new(model)
> 
> renderer = Gtk::CellRendererText.new  
> combo.pack\_start(renderer, true)  
> combo.set\_attributes(renderer, :text =\> 0)
> 
> combo.active = 1
> 
> combo.signal\_connect(“changed”) do |c|  
> p “combo: #{c.active}, #{c.active\_iter[0]}” if c.active \> -1  
> end
> 
> button = Gtk::Button.new(“Click me”)  
> button.signal\_connect(“clicked”) do |button|  
> model.clear  
> [“ruby”, “python”, “perl”].each do |text|  
> iter = model.append  
> iter[0] = text  
> end  
> combo.active = 0  
> end
> 
> window = Gtk::Window.new  
> window.signal\_connect(“destroy”) { Gtk.main\_quit }  
> vbox = Gtk::VBox.new  
> vbox.add(combo).add(button)  
> window.add(vbox).show\_all
> 
> Gtk.main
> 
> Hope it helps.
> 
> Simon

I tried your method, and could refresh combobox content.  
You really give me a big hand.

Thanks your help.

CC
