[Gtk-sharp-list] Getting the selected item from a treeview?

Charles-Louis charlouis.mono@wanadoo.be
25 Apr 2003 15:33:37 +0200


--=-2b2/5UWMUxE3/+ROQXeW
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

Hello,

I have constructed a TreeView with glade, for use in my program. I have
added some eventhandlers for the RowChanged and RowActivated in the
properties window of the TreeView, but I wanted to know how to display
the selected element in, say, the statusbar.

using System;
using System.Collections;
using System.ComponentModel;
using Gtk;
using GtkSharp;
using GLib;
using Glade;
using GladeSharp;

namespace ConfigServer{
	public class GtkTree{
	
		[Glade.Widget]
		Gtk.Window window1;
		private static Gtk.TreeStore store;
		Gtk.TreeView tv;
		Gtk.Label label1;
		Gtk.Statusbar statusbar1;
		
		public GtkTree(ArrayList al){
			Gtk.Application.Init();
			Glade.XML gxml = new Glade.XML("tree.glade","window1",null);
			gxml.Autoconnect(this);

			label1 = gxml.GetWidget("label1") as Gtk.Label;
			statusbar1 = gxml.GetWidget("statusbar1") as Gtk.Statusbar;
			
			window1 = gxml.GetWidget("window1") as Gtk.Window;
			window1.Title = "Tree test";
			
   //Fills the tree with the necessary elements
			PopulateStore(al);
			
			tv = gxml.GetWidget("treeview1") as Gtk.TreeView;
			tv.Model = store;
			
			Gtk.TreeViewColumn column1 = new TreeViewColumn();
			Gtk.CellRenderer column1Renderer = new CellRendererText();
			
			column1.Title = "Name";
			column1.PackStart(column1Renderer,true);
			column1.AddAttribute(column1Renderer,"text",0);
			
			tv.AppendColumn(column1);
			
			Application.Run();
			window1.Show();
		}
		
  #region Tree Construction
		private void PopulateStore(ArrayList al)
		{
			if (store!=null)
				return;
			store = new Gtk.TreeStore((int)TypeFundamentals.TypeString);
			
			TreeIter iter = new TreeIter();
			store.Append(out iter);
			store.SetValue(iter,0,new GLib.Value("-Configurations-"));
   //recursively build the tree
			this.BuildTree(al, iter);
		}
		
		private void BuildTree(ArrayList al, TreeIter parent)
		{
			TreeIter previous = new TreeIter();
			foreach(object o in al)
			{
				if(o is string)
				{
					store.Append(out previous, parent);
					store.SetValue(previous,0,new GLib.Value((string) o));
				}
				else if(o is ArrayList)
				{
				BuildTree((ArrayList) o,previous);
				}
			}
		}
  #endregion

		private void Quit()
		{
			Application.Quit();
		}

		#region EventHandlers
		public void OnWindowDeleteEvent(object o, DeleteEventArgs args)
		{
			this.Quit();
			args.RetVal = true;
		}


		public void OnTreeview1RowActivated(object o, EventArgs args)
		{
			label1.Text = "Toto";
			statusbar1.Pop(1);
			statusbar1.Pop(0);
			statusbar1.Push(0,"Selection made: ");
		}

		public void OnTreeview1CursorChanged(object o, EventArgs args)
		{
			statusbar1.Pop(0);
			statusbar1.Push(1,"Selection Changed: ");

		}
		#endregion
	}
}


I didn't found there was a 'GetSelected' method in the Gtk.TreeSelection
class, but the doc isn't written, so I don't know if I can use this...

Any help would be greatly apreciated!


-- 
Charles-Louis <charlouis.mono@wanadoo.be>

--=-2b2/5UWMUxE3/+ROQXeW
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: 7bit

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 TRANSITIONAL//EN">
<HTML>
<HEAD>
  <META HTTP-EQUIV="Content-Type" CONTENT="text/html; CHARSET=UTF-8">
  <META NAME="GENERATOR" CONTENT="GtkHTML/1.1.8">
</HEAD>
<BODY>
Hello,<BR>
<BR>
I have constructed a TreeView with glade, for use in my program. I have added some eventhandlers for the RowChanged and RowActivated in the properties window of the TreeView, but I wanted to know how to display the selected element in, say, the statusbar.<BR>
<BR>
<TT>using System;<BR>
using System.Collections;<BR>
using System.ComponentModel;<BR>
using Gtk;<BR>
using GtkSharp;<BR>
using GLib;<BR>
using Glade;<BR>
using GladeSharp;<BR>
<BR>
namespace ConfigServer{<BR>
	public class GtkTree{<BR>
	<BR>
		[Glade.Widget]<BR>
		Gtk.Window window1;<BR>
		private static Gtk.TreeStore store;<BR>
		Gtk.TreeView tv;<BR>
		Gtk.Label label1;<BR>
		Gtk.Statusbar statusbar1;<BR>
		<BR>
		public GtkTree(ArrayList al){<BR>
			Gtk.Application.Init();<BR>
			Glade.XML gxml = new Glade.XML(&quot;tree.glade&quot;,&quot;window1&quot;,null);<BR>
			gxml.Autoconnect(this);<BR>
<BR>
			label1 = gxml.GetWidget(&quot;label1&quot;) as Gtk.Label;<BR>
			statusbar1 = gxml.GetWidget(&quot;statusbar1&quot;) as Gtk.Statusbar;<BR>
			<BR>
			window1 = gxml.GetWidget(&quot;window1&quot;) as Gtk.Window;<BR>
			window1.Title = &quot;Tree test&quot;;<BR>
			<BR>
&nbsp;&nbsp; //Fills the tree with the necessary elements<BR>
			PopulateStore(al);<BR>
			<BR>
			tv = gxml.GetWidget(&quot;treeview1&quot;) as Gtk.TreeView;<BR>
			tv.Model = store;<BR>
			<BR>
			Gtk.TreeViewColumn column1 = new TreeViewColumn();<BR>
			Gtk.CellRenderer column1Renderer = new CellRendererText();<BR>
			<BR>
			column1.Title = &quot;Name&quot;;<BR>
			column1.PackStart(column1Renderer,true);<BR>
			column1.AddAttribute(column1Renderer,&quot;text&quot;,0);<BR>
			<BR>
			tv.AppendColumn(column1);<BR>
			<BR>
			Application.Run();<BR>
			window1.Show();<BR>
		}<BR>
		<BR>
&nbsp; #region Tree Construction<BR>
		private void PopulateStore(ArrayList al)<BR>
		{<BR>
			if (store!=null)<BR>
				return;<BR>
			store = new Gtk.TreeStore((int)TypeFundamentals.TypeString);<BR>
			<BR>
			TreeIter iter = new TreeIter();<BR>
			store.Append(out iter);<BR>
			store.SetValue(iter,0,new GLib.Value(&quot;-Configurations-&quot;));<BR>
&nbsp;&nbsp; //recursively build the tree<BR>
			this.BuildTree(al, iter);<BR>
		}<BR>
		<BR>
		private void BuildTree(ArrayList al, TreeIter parent)<BR>
		{<BR>
			TreeIter previous = new TreeIter();<BR>
			foreach(object o in al)<BR>
			{<BR>
				if(o is string)<BR>
				{<BR>
					store.Append(out previous, parent);<BR>
					store.SetValue(previous,0,new GLib.Value((string) o));<BR>
				}<BR>
				else if(o is ArrayList)<BR>
				{<BR>
				BuildTree((ArrayList) o,previous);<BR>
				}<BR>
			}<BR>
		}<BR>
&nbsp; #endregion<BR>
<BR>
		private void Quit()<BR>
		{<BR>
			Application.Quit();<BR>
		}<BR>
<BR>
		#region EventHandlers<BR>
		public void OnWindowDeleteEvent(object o, DeleteEventArgs args)<BR>
		{<BR>
			this.Quit();<BR>
			args.RetVal = true;<BR>
		}<BR>
<BR>
<BR>
		public void OnTreeview1RowActivated(object o, EventArgs args)<BR>
		{<BR>
			label1.Text = &quot;Toto&quot;;<BR>
			statusbar1.Pop(1);<BR>
			statusbar1.Pop(0);<BR>
			statusbar1.Push(0,&quot;Selection made: &quot;);<BR>
		}<BR>
<BR>
		public void OnTreeview1CursorChanged(object o, EventArgs args)<BR>
		{<BR>
			statusbar1.Pop(0);<BR>
			statusbar1.Push(1,&quot;Selection Changed: &quot;);<BR>
<BR>
		}<BR>
		#endregion<BR>
	}<BR>
}</TT><BR>
<BR>
<BR>
I didn't found there was a 'GetSelected' method in the Gtk.TreeSelection class, but the doc isn't written, so I don't know if I can use this...<BR>
<BR>
Any help would be greatly apreciated!<BR>
<BR>
<BR>
<TABLE CELLSPACING="0" CELLPADDING="0" WIDTH="100%">
<TR>
<TD>
-- <BR>
Charles-Louis &lt;<A HREF="mailto:charlouis.mono@wanadoo.be">charlouis.mono@wanadoo.be</A>&gt;
</TD>
</TR>
</TABLE>

</BODY>
</HTML>

--=-2b2/5UWMUxE3/+ROQXeW--