0052: MVC V – A ComboBox with Integers

This time around, we’re going to change things up in two ways:

  • the ListStore will have two columns, but only one will be visible in the ComboBox, and
  • we’ll be displaying integers instead of text in the ComboBox.

A Two-column ListStore

Results of this example:
Current example output
Current example output
Current example terminal output
Current example terminal output (click for enlarged view)

I talked about the two-column ListStore in the introduction to this series and touched on the requirements for ListStores in general last time, but here’s a brief review…

A ListStore needs:

  • data to stuff into rows, and
  • an array of data types, one for each column.

So what we did last time, we just do it twice to get two columns. And what’s the second column for if we don’t see it in the ComboBox? Whatever extra data you may want to have associated with the items that do show. Here’s the DayListStore class:

class DayListStore : ListStore
	string[] days = ["Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
	int[] dayNumbers = [1, 2, 3, 4, 5, 6, 7];
	GType[] columnTypes = [GType.STRING, GType.INT];
	TreeIter treeIter;
	int dayColumn = 0, numberColumn = 1;

		string day;
		int number;
		foreach(ulong i; 0..days.length)
			day = days[i];
			number = lettersInDays[i];
			treeIter = createIter();
			setValue(treeIter, dayColumn, day);
			setValue(treeIter, numberColumn, number);

	} // this()

} // class DayListStore

We have three arrays in the initialization section:

  • days - our string data,
  • dayNumbers – the integer data, and
  • columnTypes – an array of GType data types.

The constructor is pretty much the same as before except that in the foreach() loop we extract a bit of data from each of the two data arrays and make the call to setValue() twice, once for each column.

The DayComboBox

In the initialization section of the DayComboBox, we have:

class DayComboBox : ComboBox
	bool entryOn = false;
	DayListStore _dayListStore;
	CellRendererText cellRendererText;
	int visibleColumn = 1;
	int activeItem = 0;

A couple of things I’ll point out here…


Even though the data in our ListStore is integers, we’re using a CellRendererText. We follow through in the initialization section where we declare it:

CellRendererText cellRendererText;

And in these three lines from the constructor where we instantiate it, pack it into the ComboBox, and deal with the attributes:

cellRendererText = new CellRendererText();
packStart(cellRendererText, false);
addAttribute(cellRendererText, "text", visibleColumn);


In the initialization section, the visibleColumn variable is set to 1, not 0 as it was before. This is where the decision is made as to which column is shown in the ComboBox.

And if you look at those lines from the constructor, you’ll see that addAttribute() is where the visible column is set.

The rest of the constructor is the same and the callback is almost identical except that it calls an extra function to write to the terminal. Because there’s nothing remarkable about it, I’ll leave you to explore it on your own if you’re so inclined.


And that’s how to use integers in a ComboBox. Join us next time for a peek into the mysteries of ComboBoxes with images.

Comments? Questions? Observations?

Did we miss a tidbit of information that would make this post even more informative? Let's talk about it in the comments.

You can also subscribe via RSS so you won't miss anything. Thank you very much for dropping by.

© Copyright 2021 Ron Tarrant