# Colouring text in a ComboBoxEntry

**URL:** <https://www.ruby-forum.com/t/colouring-text-in-a-comboboxentry/114407>\
**Category:** Ruby-Gnome 2\
**Created:** [September 12, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/colouring-text-in-a-comboboxentry/114407 "2007-09-12T12:18:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [September 12, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/colouring-text-in-a-comboboxentry/114407/1 "2007-09-12T12:18:04Z")

</div>

Hi,

I want to react to a user’s text entry by showing the text in red  
when certain conditions are not met. All that I try results in the  
text remaining black. Example nugget of code below, which doesn’t work  
for me.

Is this even possible? What have I missed?

This is using Gtk 2.10.11, rubygnome 2-0.16.0-1, Ruby 1.8.5, Windows  
XP.

Thanks, Martin.

================  
Gtk.init

win = Gtk::Window.new  
win.set\_default\_size( 640,480 )

blue\_button = Gtk::Button.new(“blue”)  
red\_button = Gtk::Button.new(“red”)

combo = Gtk::ComboBoxEntry.new( true )  
%w(hello world and beyond ).each{|t| combo.append\_text( t ) }

blue\_button.signal\_connect(“clicked”){  
combo.modify\_text( Gtk::STATE\_NORMAL, Gdk::Color.parse(‘blue’) )  
}

red\_button.signal\_connect(“clicked”){  
combo.modify\_text( Gtk::STATE\_NORMAL, Gdk::Color.parse(‘red’) )  
}

hb = Gtk::HBox.new  
hb.pack\_start( blue\_button )  
hb.pack\_start( red\_button )  
hb.pack\_start( combo )

win.add( hb )

win.show\_all

Gtk.main

* * *

This [SF.net](http://SF.net) email is sponsored by: Microsoft  
Defy all challenges. Microsoft(R) Visual Studio 2005.  
[http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/](http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/)

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [September 12, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/colouring-text-in-a-comboboxentry/114407/2 "2007-09-12T13:15:09Z")

</div>

Martin P. wrote:

> Hi,
> 
> I want to react to a user’s text entry by showing the text in red  
> when certain conditions are not met. All that I try results in the  
> text remaining black. Example nugget of code below, which doesn’t work  
> for me.
> 
> Is this even possible?

Absolutely, but it’s not immediately obvious what’s going on.  
Fortunately this is one of those things where once it’s been pointed  
out, it’s obvious.

Fact is, the Gtk::ComboBoxEntry doesn’t draw any text itself, it has a  
child Gtk::Entry that handles the text entry and display. Therefore, to  
set the colour of the text you have to modify the style of the  
Gtk::Entry.

> combo.child.modify\_text( Gtk::STATE\_NORMAL, Gdk::Color.parse(‘blue’) )

This is more obvious if you try it on a Gtk::Button. You can modify the  
button text style till you’re blue in the face, but it’s the Gtk::Label  
that is a child of the button that actually displays the text.

best,  
Dan

---

<div class="post-metadata">

**Author:** ![dhstudio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b487fb/32.png) [@dhstudio](https://www.ruby-forum.com/u/dhstudio)\
**Post date:** [September 12, 2007, 4:01pm UTC](https://www.ruby-forum.com/t/colouring-text-in-a-comboboxentry/114407/3 "2007-09-12T16:01:38Z")

</div>

Daniel L. wrote:

> Fact is, the Gtk::ComboBoxEntry doesn’t draw any text itself, it has a  
> child Gtk::Entry that handles the text entry and display. Therefore, to  
> set the colour of the text you have to modify the style of the  
> Gtk::Entry.
> 
> > combo.child.modify\_text( Gtk::STATE\_NORMAL, Gdk::Color.parse(‘blue’) )

super!  
martin

* * *

This [SF.net](http://SF.net) email is sponsored by: Microsoft  
Defy all challenges. Microsoft(R) Visual Studio 2005.  
[http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/](http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/)
