Skip to content

Listbox Associative Category

tavlarides edited this page Feb 28, 2020 · 3 revisions

Minimum supported: Nana 1.4

Listbox is a sequence container, but it provides functions to allow the categories can be accessed through Key-Value pairs.

#include <nana/gui.hpp>
#include <nana/gui/widgets/listbox.hpp>

int main()
{
	using namespace nana;

	form fm;
	listbox list{ fm };

	//Layout
	fm.div("margin=10 list");
	fm["list"] << list;
	fm.collocate();

	//Create a column
	list.append_header("column");

	//Access associative categories and append an item for each category
	list.assoc(2015).push_back("item 2015");
	list.assoc(2016).push_back("item 2016");

	fm.show();
	exec();
}

The listbox::assoc() function is used to access a category through specified key, it performs an insertion if such key does not already exist. In above example, it created 2 categories with assoc() function.

assoc_at() is another function to access a category through specified key. What the difference with assoc() is to throw an std::out_of_range if such key does not already exist, instead of insertion.

assoc_erase() is used to remove a category which is associated with the specified key. If the key does not exist, no category will be removed.

Ordered associative category

Insertion of category can be ordered depending on keys. assoc_ordered() is to enable the ordered insertion, but it does not sort the existing categories. The ordered insertion requires that all the types of keys are a same type.

list.assoc(2015).push_back("item 2015");
list.assoc(2016).push_back("item 2016");

//Enable ordered insertion
list.assoc_ordered(true);

//A new category is inserted in front of category 2015.
list.assoc(2014).push_back("item 2014");

//The ordered insertion gets disable if a new key is a different type.
list.assoc(2017.0).push_back("item 2017.0");

list.assoc_ordered(true); //returns false, because there is a different key type.