[Gtk-sharp-list] [Mono GTK#] Set Combobox Selected by Value

Moonfire, D. d.moonfire at mfgames.com
Thu Sep 19 19:56:39 UTC 2013


Basically, you want to set comboBox.Active to the index of your item. If
you happen to know it ahead of time, then go for it. If you can't, then
just loop through the list store to fine the item. I got tired of doing a
lot of these and started putting them into a subclasses ComboBox over in my
MfGames.GtkExt library, but I haven't gotten serious about merging all the
individual branches to make it that useful.

https://github.com/dmoonfire/mfgames-gtkext-cil/blob/master/src/MfGames.GtkExt/EnumComboBox.cs

Below is an example of how I did it. This predates my understanding of
LINQ, so it's a bit clumsy. Effectively, I wrote a combo box that takes an
enumeration and displays the values (and returns/sets it via said enum).

ComboBox comboBox;

// Go through the elements and find the string value
string str = value.ToString();

var store = (ListStore) comboBox.Model;
int index = 0;

foreach (object[] row in store)
{
    // Check for match
    if (str == row[0].ToString())
    {
        comboBox.Active = index;
        break;
    }

    // Increment the index so we can reference it for the active.
    index++;
}




On Thu, Sep 19, 2013 at 10:56 AM, jaymarvels <jacob.spencer80 at googlemail.com
> wrote:

> I don't want to get. I want to set.
>
> I'll explain again
>
> I have a liststore...2 columns
>
> America 10
> Belgium 16
> England 139
> Denmark 166
>
> This is then bound to the combobox.
>
>
>                         cboNations.Clear ();
>                         Gtk.CellRendererText text = new CellRendererText
> ();
>                         cboNations.Model = datalist;
>                         cboNations.PackStart (text, false);
>                         cboNations.AddAttribute (text, "text", 0);
>
>
> I want to set the initial selectedvalue = 139 or something else of my
> choosing.
>
> In winforms this would be done by setting my combobox datasource to a
> object
> class..
>
> And then doing a combobox selectedvalue = 139;
>
>
>
> --
> View this message in context:
> http://mono.1490590.n4.nabble.com/Mono-GTK-Set-Combobox-Selected-by-Value-tp4660923p4660926.html
> Sent from the Mono - Gtk# mailing list archive at Nabble.com.
> _______________________________________________
> Gtk-sharp-list maillist  -  Gtk-sharp-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/gtk-sharp-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/gtk-sharp-list/attachments/20130919/13d3ce29/attachment.html>


More information about the Gtk-sharp-list mailing list