Mono: Getting the selected item from a treeview ~ Gtk#[GtkSharp]

Hi again; Today I wrote about getting the selected item from a treeview @gtk#, I googled it and get some solutions about it but not in well documented so I decided to post a good example…

 * Created by SharpDevelop.
 * User: cakirh
 * Date: 12.01.2010
 * Time: 23:03
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 using Gtk;
 using System;

public class TreeViewExample

 public static void Main ()

 Gtk.Application.Init ();
 new TreeViewExample ();
 Gtk.Application.Run ();

 public TreeViewExample ()
 Gtk.TreeIter iter;
 Gtk.Window window = new Gtk.Window ("TreeView Example");
 window.SetSizeRequest (500,200);

 Gtk.TreeView tree = new Gtk.TreeView ();
 Gtk.ListStore musicListStore = new Gtk.ListStore (typeof (string),  typeof (string));

 tree.AppendColumn ("Artist", new Gtk.CellRendererText (), "text", 0);
 tree.AppendColumn ("Title", new Gtk.CellRendererText (), "text", 1);

 iter=musicListStore.AppendValues ("hello", "world!");
 musicListStore.AppendValues ("hello", "mono!");
 tree.Model = musicListStore;

 tree.CursorChanged+= new EventHandler(tree_CursorChanged);
 window.Add (tree);
 window.ShowAll ();

 void tree_CursorChanged(object sender, EventArgs e)
 TreeSelection selection = (sender as TreeView).Selection;
 TreeModel model;
 TreeIter iter;

 if(selection.GetSelected(out model, out iter)){
 Console.WriteLine("Selected Value:"+model.GetValue (iter, 0).ToString()+model.GetValue (iter, 1).ToString());


Note that you should look at mono page for treeview usage:  GtkSharp TreeView Tutorial , Also my example taken from that page but a little bit updated for getting selected items in treeview…


gtksharptreeviewget selected

Thats all folks for this post!

kick it on