Name

ComboBox — drop-down list selection (optionally editable)

Synopsis

ComboBox ( string label ,
  list items );
 

Parameters

string label

Options

editable

the user can enter any value.

Optional Arguments

list items

the items contained in the combo box

Description

A combo box is a combination of a selection box and an input field. It gives the user a one-out-of-many choice from a list of items. Each item has a ( mandatory ) label and an ( optional ) id. When the 'editable' option is set, the user can also freely enter any value. By default, the user can only select an item already present in the list.

The items are very much like SelectionBox items: They can have an (optional) ID, they have a mandatory text to be displayed and an optional boolean parameter indicating the selected state. Only one of the items may have this parameter set to "true"; this will be the default selection on startup.

[Note]Note

You can and should set a keyboard shortcut within the label. When the user presses the hotkey, the combo box will get the keyboard focus.

Usage

 	`ComboBox( `id( `pizza ), "select your Pizza:", [ "Margarita", `item( `id( `na ), "Napoli" ) ] )

Examples

          {
    UI::OpenDialog(
	       `VBox( 
		     `ComboBox( "Select your Pizza:",
				[
				 "Napoli",
				 "Funghi",
				 "Salami"
				] ),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    UI::CloseDialog();
}

        
          // Create a combo box with three entries.
// All entries have IDs to identify them independent of locale
// (The texts might have to be translated!).
// Entry "Funghi" will be selected by default.
{
    UI::OpenDialog(
	       `VBox(
		     `ComboBox(`id(`pizza),
				    "Select your Pizza:",
				    [
				     `item(`id(`nap), "Napoli"       ),
				     `item(`id(`fun), "Funghi", true ),
				     `item(`id(`sal), "Salami"       )
				    ] ),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();

    any items = UI::QueryWidget(`pizza, `Items);
    y2debug( "Items:\n%1", items );

    // Get the input from the selection box.
    //
    // Notice: The return value of UI::UserInput() does NOT return this value!
    // Rather, it returns the ID of the widget (normally the PushButton)
    // that caused UI::UserInput() to return.
    any pizza=UI::QueryWidget(`id(`pizza), `Value);
    y2milestone( "Selected %1", pizza );
    
    // Close the dialog.
    // Remember to read values from the dialog's widgets BEFORE closing it!
    UI::CloseDialog();

    // Evaluate selection

    string toppings = "nothing";

    if      ( pizza == `nap )	toppings = "Tomatoes, Cheese";
    else if ( pizza == `fun )	toppings = "Tomatoes, Cheese, Mushrooms";
    else if ( pizza == `sal )	toppings = "Tomatoes, Cheese, Sausage";
    
    // Pop up a new dialog to echo the selection.
    UI::OpenDialog(
	       `VBox(
		     `Label("You will get a pizza with:"),
		     `Label(toppings),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    UI::CloseDialog();
}

        
          // Create a combo box with three entries.
// All entries have IDs to identify them independent of locale
// (The texts might have to be translated!).
// Entry "Funghi" will be selected by default.
{
    UI::OpenDialog(
	       `VBox(
		     `ComboBox(`id(`pizza), `opt(`editable),
				    "Select your Pizza:",
				    [
				     `item(`id(`nap), "Napoli"       ),
				     `item(`id(`fun), "Funghi", true ),
				     `item(`id(`sal), "Salami"       )
				    ] ),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();

    // Get the input from the selection box.
    //
    // Notice: The return value of UI::UserInput() does NOT return this value!
    // Rather, it returns the ID of the widget (normally the PushButton)
    // that caused UI::UserInput() to return.
    any pizza=UI::QueryWidget(`id(`pizza), `Value);
    y2milestone( "Selected %1", pizza );
    
    // Close the dialog.
    // Remember to read values from the dialog's widgets BEFORE closing it!
    UI::CloseDialog();

    // Evaluate selection

    string toppings = "nothing";

    if      ( pizza == `nap )	toppings = "Tomatoes, Cheese";
    else if ( pizza == `fun )	toppings = "Tomatoes, Cheese, Mushrooms";
    else if ( pizza == `sal )	toppings = "Tomatoes, Cheese, Sausage";
    
    // Pop up a new dialog to echo the selection.
    UI::OpenDialog(
	       `VBox(
		     `Label("You will get a pizza with:"),
		     `Label(toppings),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    UI::CloseDialog();
}

        
          // Create an editable combo box with restricted input character set.
{
    UI::OpenDialog(
	       `VBox(
		     `ComboBox(`id(`addr), `opt(`editable),
				    "Enter hex address:",
				    [
				     "0cff",
				     "8080",
				     "D0C0",
				     "ffff"
				    ] ),
		     `PushButton("&OK")
		     )
	       );
    // Set the valid input characters.
    UI::ChangeWidget(`id(`addr), `ValidChars, "0123456789abcdefABCDEF" );

    
    // Wait for user input.
    UI::UserInput();


    // Get the input from the selection box.
    //
    // Notice: The return value of UI::UserInput() does NOT return this value!
    // Rather, it returns the ID of the widget (normally the PushButton)
    // that caused UI::UserInput() to return.
    any val=UI::QueryWidget(`id(`addr), `Value);
    y2milestone( "Selected %1", val );

    // Close the dialog.
    // Remember to read values from the dialog's widgets BEFORE closing it!
    UI::CloseDialog();

    
    // Pop up a new dialog to echo the input.
    UI::OpenDialog(
	       `VBox(
		     `Label("You entered:"),
		     `Label(val),
		     `PushButton("&OK")
		     )
	       );
    UI::UserInput();
    UI::CloseDialog();
}

        
          // Example showing how to replace SelectionBox items
{

    list pizza_list =
	[
	 "Pizza Napoli",
	 "Pizza Funghi",
	 "Pizza Salami",
	 "Pizza Hawaii"
	 ];

    list pasta_list =
	[
	 "Spaghetti",
	 "Rigatoni",
	 "Tortellini"
	 ];

    UI::OpenDialog( `VBox(
			  `ComboBox(`id(`menu), "Daily &Specials:", pizza_list ),
			  `HBox(
				`PushButton(`id(`pizza), "Pi&zza" ),
				`PushButton(`id(`pasta), "&Pasta" )
				),
			  `PushButton(`id(`ok), "&OK" )
			  )
		    );

    symbol button = nil;

    do
    {
	button = (symbol) UI::UserInput();

	if ( button == `pizza )		UI::ChangeWidget(`menu, `Items, pizza_list );
	if ( button == `pasta )		UI::ChangeWidget(`menu, `Items, pasta_list );
	
    } while ( button != `ok );

    string order = (string) UI::QueryWidget(`menu, `Value );
    UI::CloseDialog();


    //
    // Show the result
    //
    
    UI::OpenDialog(`VBox(
			 `Label( sformat( "Your order: %1", order ) ),
			 `PushButton(`opt(`default), "&OK" )
			 )
		   );
    UI::UserInput();
    UI::CloseDialog();
}

        
          {

UI::OpenDialog(
    `VBox( 
	`ComboBox( `id( `input ), `opt( `editable ),"Combo Box",
		[ "pizza", "pasta", "pronta" ] ),
	`IntField( `id ( `field ), "Limit characters to...", -1, 100, -1 ),
	`PushButton( `id ( `butt ),"limit input" ),
	`PushButton( `id ( `exitButton ), "Exit" )
	)
);

any ret = nil;

ret = UI::UserInput();

while ( ret != `exitButton ) {
    integer chars = (integer) UI::QueryWidget(`id(`field), `Value);
    UI::ChangeWidget( `input, `InputMaxLength, chars );
    ret = UI::UserInput();
}

UI::CloseDialog();


}