[Gtk-sharp-list] how to search for a value in a ListStore

Peter Johanson peter at peterjohanson.com
Tue Jan 30 12:39:11 EST 2007


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



More information about the Gtk-sharp-list mailing list