Brian J Hoskins
Brian J Hoskins

Reputation: 559

Best way to count parent items in GTK# TreeStore

I am new to GTK and I am learning to use a TreeStore. I am using C#.

I would like to count the parent items in a TreeStore so that when I append an item I can concatenate the title with an appropriate number. (e.g. "Product 0", "Product 1" etc.

I have found a way to do what I want by iterating through the entire tree and counting the items. Like this:

trs_inspectionStore.GetIterFirst (out iter);    // Go to first item in store.
    int n = 0;
    while (trs_inspectionStore.IterIsValid (iter))  // Loop while the iter is valid
    {
        n++;
        trs_inspectionStore.IterNext (ref iter);    // Next item!
    }
    Console.WriteLine (n);

I am wondering if there is a better way. I was hoping to find some Count property or something along those lines. Any ideas? Or am I already using the best method?

Upvotes: 1

Views: 192

Answers (1)

Brian J Hoskins
Brian J Hoskins

Reputation: 559

I found the answer to this problem myself:

To count the number of top-level rows in a TreeStore, use the NChildren() method of a model. This method is generally used to count the number of children of a row, and you pass it an 'Iter' object as a parameter. But if, instead, you call it with no Iter (zero parameters) it returns a count of the number of top level rows:

int count = Model.IterNChildren();

Upvotes: 1

Related Questions