How to set the value of a Gtk.ComboBox?

我们两清 提交于 2019-12-01 08:59:52
kdt

The reason your code doesn't work is that the "items in the combobox" are actually the cell renderers packed into it for displaying columns of data. To get at the actual data, you need the TreeModel object.

If you really must select based only on what's in the combo, here's how you can do it:

string[] values = new string[]{"one", "two", "three"};
var combo = new ComboBox(values);

Gtk.TreeIter iter;
combo.Model.GetIterFirst (out iter);
do {
  GLib.Value thisRow = new GLib.Value ();
  combo.Model.GetValue (iter, 0, ref thisRow);
  if ((thisRow.Val as string).Equals("two")) {
    combo.SetActiveIter (iter);
    break;
  }
} while (combo.Model.IterNext (ref iter));

However, generally it's more succinct to keep your values indexed, like this:

List<string> values = new List<string>(){"one", "two", "three"};  
var combo = new ComboBox(values.ToArray());

// Select "two"
int row = values.IndexOf("two");
Gtk.TreeIter iter;
combo.Model.IterNthChild (out iter, row);
combo.SetActiveIter (iter);

As an alternative you can set the "selected index" like this if you have the elements of the ComboBox stored in an Array or List

for (int i = 0; i < combo.Model.IterNChildren(); ++i) //iterate over ComboBox elements
{
  if (myList[i].Equals(elementToSelect))
  {
    combo.Active = i;
    break;
  }
}

I had a similar problem and this answer in C# was helpful, but the end solution in C looks very different. I'm posting here because it was the first for google searches on it.

Basically if you are looking in a GTK ComboBox and using the GTK Tree Model and you hope to get the information out, you have to use the iter pattern just right. The wrappers for other languages like Python and C# have made it quite a bit easier, but for those still using C with GTK, here is the solution:

Assuming you have a flat gtk combobox and you just need to get something out of it, you can use something like this for c:

int set_combo_box_text(GtkComboBox * box, char * txt) 
{
   GtkTreeIter iter;
   GtkListStore * list_store;
   int valid;
   int i;
   list_store = gtk_combo_box_get_model(box);

   // Go through model's list and find the text that matches, then set it active
   i = 0; 
   valid = gtk_tree_model_get_iter_first (GTK_TREE_MODEL(list_store), &iter);
   while (valid) {
      gchar *item_text;
      gtk_tree_model_get (GTK_TREE_MODEL(list_store), &iter, 0, &item_text, -1); 
      printf("item_text: %s\n", item_text);
      if (strcmp(item_text, txt) == 0) { 
         gtk_combo_box_set_active(GTK_COMBO_BOX(box), i);
         return true;
         //break;
      }    
      i++; 
      valid = gtk_tree_model_iter_next (GTK_TREE_MODEL(list_store), &iter);
   }
   printf("failed to find the text in the entry list for the combo box\n");
}

If you are storing more information in each combobox line, you can get more info out of the iter using something like this:

valid = gtk_tree_model_get(GTK_TREE_MODEL(list_store), &iter, 0, &item_0, 1, &item_1, 2, &item_2, ... , -1); 

Hope that helps.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!