[Gtk-sharp-list] Set item in ComboBox (novice in GTK#)

Rafael Teixeira monoman at gmail.com
Mon Jun 20 12:28:58 EDT 2005


For those that want a simpler Text/Value pair here is a Helper class
from my upcoming cli.commons set o libraries (Commons.Gtk.Useful.dll
in this case):

DescriptionValueComboBoxHelper class (attached).

Usage is simple (using your example)

DescriptionValueComboBoxHelper Filtro =
  new DescriptionValueComboBoxHelper(cbFiltro);
Filtro.Add("Option #1", "1");
Filtro.Add("Option #2", "2");
Filtro.Add("Option #3", "3");
...
string mySelection = "2";
...
Filtro.SelectedValue = mySelection;

Note that the second parameter in the Add method is object and can be
of any type
 and can be the sole parameter (another overload in which case the
ToString() is used to determine what text to show in the ComboBox).

class Apple {
  string Color;
  public Apple(string color) { this.color = color; } 
  public void Print() { Console.WriteLine(ToString()); }
  public override string ToString() { return color + " apple"; }
}

DescriptionValueComboBoxHelper dv =
  new DescriptionValueComboBoxHelper(combobox);
dv.Add(new Apple("red");
dv.Add(new Apple("green");
combobox.Active = 0;
Apple selected = (Apple)dv.SelectedValue;
selected.Print(); // writes "red apple"

License is Creative Commons Attribution.

Enjoy,

On 6/20/05, Rafael Teixeira <monoman at gmail.com> wrote:
> Oi Paulinho,
> 
> cbFiltro.Active = 1; // index (pt_BR: apenas por posição) zero-based.
> 
> Property ActiveText has only the 'get' acessor (so it is readonly), so...
> 
> In order to select by value you'll need to iterate through the
> ListStore searching for it (as it may have been reordered) and
> counting up to find it's positional index.
> 
> <rant>
> Sometimes I miss WinForms Combos' simplicity when dealing with GTK#
> versions'  flexibility that normally means larger, and more complex,
> code.
> </rant>
> 
> :)
> 
> On 6/18/05, Paulinho <amf.paulo at gmail.com> wrote:
> > Hi all,
> >
> > I need to set a item in ComboBox, How i do that ?
> >
> > In Web i just make this:
> > myCombo.SelectedValue = "2" , automatically myCombo show me "Description
> > of Option #2"
> > sorry i'm novice in GTK# client applications
> >
> >
> > my code:
> >
> > CellRendererText descricao = new CellRendererText ();
> > CellRendererText id          = new CellRendererText ();
> >
> > comboBoxStore = new ListStore (typeof (string), typeof (string));
> > cbFiltro.Model = comboBoxStore;
> >
> > cbFiltro.PackStart (descricao, false);
> >
> > cbFiltro.AddAttribute (descricao, "text"  , 0);
> >
> > comboBoxStore.AppendValues("Option #1", "1");
> > comboBoxStore.AppendValues("Option #2", "2");
> > comboBoxStore.AppendValues("Option #3", "3");
> >
> > ...
> > ...
> >
> > string mySelection = "2";
> >
> > ???
> >
> >
> > How to set the row of ListStore or ComboBox using a value into variable
> > "mySelection" ?
> >
> >
> > Magalhaes
> >
> > /* Sorry my english */
> > _______________________________________________
> > Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> > http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
> >
> 
> 
> --
> Rafael "Monoman" Teixeira
> ---------------------------------------
> I'm trying to become a "Rosh Gadol" before my own eyes.
> See http://www.joelonsoftware.com/items/2004/12/06.html for enlightment.
> It hurts!
> 


-- 
Rafael "Monoman" Teixeira
---------------------------------------
I'm trying to become a "Rosh Gadol" before my own eyes. 
See http://www.joelonsoftware.com/items/2004/12/06.html for enlightment.
It hurts!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: ManagedReference.cs
Type: application/octet-stream
Size: 851 bytes
Desc: not available
Url : http://galactus.ximian.com/pipermail/gtk-sharp-list/attachments/20050620/56aff0e1/ManagedReference.obj
-------------- next part --------------
A non-text attachment was scrubbed...
Name: DescriptionValueComboBoxHelper.cs
Type: application/octet-stream
Size: 1928 bytes
Desc: not available
Url : http://galactus.ximian.com/pipermail/gtk-sharp-list/attachments/20050620/56aff0e1/DescriptionValueComboBoxHelper.obj


More information about the Gtk-sharp-list mailing list