On Tue, Jan 30, 2007 at 07:28:31PM +0200, Radu wrote:
> How can I search for a value of a field in a ListStore object?
> 
> For example, I have this list:
> ListStore list = new ListStore(typeof(int), typeof(string));
> list.AppendValues(1, "first string");
> list.AppendValues(2, "second string");
> 
> Now, how can I search for a string/integer in this list? I want to get 
> the iter for the row that contains the given string/integer.

There are basically two ways (there's a third, but it's fairly
innefficient, so I won't bother):

1)

TreeIter iter;

if (list.GetIterFirst (out iter)) {
        do {
                string s = (string)list.GetValue (iter, 1);

                if (s == "first string") {
                        break;
                }
        } while (list.IterNext (ref iter));
}

2)

list.Foreach (new TreeModelForeachFunc (SearchForeach));

bool SearchForeach (TreeModel m, TreePath p, TreeIter i)
{
        string s = (string)m.GetValue (i, 1);

        if (s == "my search term") {
                found_iter_class_field = i;
                return true;
        }

        return false;
}


HTH,

-pete

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

Reply via email to