Skip to content
James Bremner edited this page Apr 25, 2019 · 5 revisions

This example shows the usage of inputbox.

Inputbox

Prepare an image to show it on inputbox, and name it "inputbox_images.bmp". Inputbox images

Let's create the inputbox.

#include <nana/gui.hpp>
int main()
{
	using namespace nana;
	form fm;

	drawing{ fm }.draw([](paint::graphics& graph){
		graph.string({ 20, 20 }, "Click on the form to show inputbox");
	});

	fm.show();

	internationalization i18n;
	//Translate these 2 words into Chinese.
	i18n.set("NANA_BUTTON_OK", u8"确定");
	i18n.set("NANA_BUTTON_CANCEL", u8"取消");

	//Show an inputbox when the form is clicked.
	fm.events().click([&fm]
	{
		inputbox::text name("<bold blue>Name</>", "Nana C++ Library");	//The format text is also available, the second parameter can be given for default value.
		inputbox::text gender("Gender", std::vector<std::string>{ "Male", "Female" });
		inputbox::date birth("Date of birth");
		inputbox::real height("Height(cm)", 100, 1, 300, 1);
		inputbox::integer kids("Kids", 0, 0, 100, 1);

		inputbox inbox(fm, "Please input <bold>your personal information</>.", "Personal information");

		//Open the image file
		paint::image img("inputbox_images.bmp");

		//Use 'copy' to assign the image, these image objects refer to the same
		//image resource.
		inbox.image(img, true, { 380, 0, 40, 100 });
		inbox.image(img, false, { 420, 0, 40, 100 });
		inbox.image_v(img, true, { 0, 0, 380, 50 });
		inbox.image_v(img, false, { 0, 50, 380, 50 });

		//Sets a verifier
		inbox.verify([&name](window handle)
		{
			if (name.value().empty())
			{
				msgbox mb(handle, "Invalid input");
				mb << L"Name should not be empty, Please input your name.";
				mb.show();
				return false; //verification failed
			}
			return true; //verified successfully
		});

		//Call the show_modal() method to show the inputbox.
		//This method blocks the application execution until the inputbox gets closed.
                //To allow user to continue interacting with parent and other windows, use show() instead.
		//It returns true if the user clicks OK button, or false otherwise.
		if (inbox.show_modal(name, gender, birth, height, kids))
		{
			auto n = name.value();		//std::string in UTF-8
			auto g = gender.value();	//std::string in UTF-8
			auto b = birth.value();		//std::string in UTF-8
			auto year = birth.year();	//int
			auto month = birth.month(); //int
			auto day = birth.day();		//int
			auto h = height.value();	//double
			auto k = kids.value();		//int
		}
	});

	exec();
}

i18n

The inputbox shows 2 buttons in Chinese(OK is translated as "确定", which means "confirm" or "confirmed". Cancel is translated as "取消"). There are some other predefined translations that inputbox may use are:

NANA_BUTTON_OK OK button

NANA_BUTTON_CANCEL Cancel button

NANA_BUTTON_BROWSE Browse button

and

"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"