Wednesday, May 5, 2010

GTK#: How to Change Font of Entry

This tutorial assumes that you are using MonoDevelop and created a C# GUI application using GTK#.

Supposing you already have a Window with an Entry, named txtSource, this is how you change the Font.

First, include Pango in imports.
  • using Pango;

Second, use the FontDescription class and instanciate one.
  • FontDescription fontDesc = new FontDescription();

Third, set the Family property of FontDescription object to any font family you want (ex, "Arial", "Tahoma", "Courier).
  • fontDesc.Family = "Courier";

Lastly, set the FontDescription object to Entry using ModifyFont method.
  • txtSource.ModifyFont(fontDesc);

The whole code should look something like this:

using System;
using Gtk;
using Pango;

public partial class MainWindow : Gtk.Window {
  public MainWindow () :base(Gtk.WindowType.Toplevel) {
    Build();

    FontDescription fontDesc = new FontDescription();
    
    fontDesc.Family = "Courier";
    
    txtSource.ModifyFont(fontDesc);
  }
}

No comments: