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;
// THE ITER WILL POINT TO THE SELECTED ROW
if(selection.GetSelected(out model, out iter)){
Console.WriteLine("Selected Value:"+model.GetValue (iter, 0).ToString()+model.GetValue (iter, 1).ToString());
}
}
}
<pre>
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…
Output:
Thats all folks for this post!




sspanzer 2:49 pm on September 7, 2010 Permalink |
en,how can I get from the model,because it’s object in liststore,such as
ListStore evData = new ListStore(typeof(Event));
treeview1.Model = evData;
marked 11:59 am on July 21, 2011 Permalink |
Cheers man! Extremely helpful, clear and concise