On 4/19/05, xiii29 <[EMAIL PROTECTED]> wrote:
> Hi,
> 
> I'm wondering if someone can send me an example of using the
> Gtk.ComboBox with AddAttributes ...
> 
> I'have a collection of object with Id and Name property. My goal is to
> be able to show the name but get the id ...
> 
> I can't find any help doc ...
> 
> Thanks for any help !
> 

You can store anything you want in a ListStore, and you don't need to
show all columns you add in the ListStore

for example:

using System;
using Gtk;

public class ComboBoxDemo : Window {

        private ListStore store;

        public ComboBoxDemo () : base ("ComboBoxDemo") {
                this.SetSizeRequest(420, 360);

                this.WindowPosition = WindowPosition.Center;
                this.DeleteEvent += new DeleteEventHandler (WindowClose);

                VBox vbox = new VBox (false, 5);
                vbox.BorderWidth = 10;


                CellRendererPixbuf pixbuf = new CellRendererPixbuf ();
                CellRendererText text = new CellRendererText ();

                store = new ListStore (typeof (Gdk.Pixbuf), typeof (string), 
typeof (int));
                ComboBox combo = new ComboBox (store);
                combo.Changed += new EventHandler (OnChange);

                combo.PackStart (pixbuf, false);
                combo.PackStart (text, false);

                combo.AddAttribute (pixbuf, "pixbuf", 0);
                combo.AddAttribute (text, "text", 1);

                int id = 0;
                string[] stockIds = Gtk.Stock.ListIds();

                foreach (string stockItemName in stockIds) {

                        Gdk.Pixbuf image = this.RenderIcon (stockItemName,
IconSize.SmallToolbar, "");

                        store.AppendValues (image, stockItemName, id);
                        id++;

                }
                TreeIter iter;
                if (store.GetIterFirst (out iter))
                        combo.SetActiveIter (iter);

                vbox.PackStart (combo, false, false, 0);

                this.Add (vbox);

                this.ShowAll ();
        }

        private void WindowClose (object o, DeleteEventArgs args)
        {
                this.Destroy ();
                Application.Quit ();
        }

        private void OnChange (object o, EventArgs args)
        {
                TreeIter iter;
                if ((o as ComboBox).GetActiveIter (out iter)) {
                        string text = (string)store.GetValue (iter, 1);
                        int id = (int)store.GetValue (iter, 2);
                        Console.WriteLine (text + " (id=" + id + ")");
                }
        }

        public static void Main (string[] args)
        {
                Application.Init ();
                new ComboBoxDemo ();
                Application.Run ();
        }
}

(compile with:  mcs -pkg:gtk-sharp-2.0 ComboBoxDemo.cs)

It will show an image and some text, but when you change the
selection, the text and the ID is written to the console

-- Ben
_______________________________________________
Gtk-sharp-list maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/gtk-sharp-list

Reply via email to