Repopulate a combo box

2019-07-27 10:29发布

问题:

This is how I populate the combo box:

        foreach(string s in entries)
        {
            string[] fields = someSplit(s);
            threadComboBox.AppendText(fields[0]);
        }

How would I remove all items and add new ones? I tried calling Clear(), but while it does remove old values, new ones don't get added.

回答1:

try

    threadComboBox.Clear();
    ListStore store = new ListStore(typeof (string));
    threadComboBox.Model = store;

    foreach(string s in entries)
    {
        string[] fields = someSplit(s);
        store.AppendValues (fields[0]);          
    }


回答2:

The accepted answer did not work for me by itself. I had to use the snippet from the FAQ:

cb.Clear();
CellRendererText cell = new CellRendererText();
cb.PackStart(cell, false);
cb.AddAttribute(cell, "text", 0);
ListStore store = new ListStore(typeof (string));
cb.Model = store;

//now this works:
cb.AppendText("test");


标签: c# mono gtk