Thread Rating:
  • 478 Vote(s) - 2.93 Average
  • 1
  • 2
  • 3
  • 4
  • 5
User Interface Basics4:Populating Control List Data
06-15-2011, 03:58 PM, (This post was last modified: 06-16-2011, 10:41 AM by kino.)
#1
User Interface Basics4:Populating Control List Data
Allowing a user to select one or more items from a list is a common Web design pattern. A ListBox permits the programmer to display a number of items, allow the user to make a selection, and then proceed accordingly. ListBoxes are ideal controls for presenting a list of choices to the user---whether a few items or a large number.


In Curl, a ListBox is used to select multiple items. Each selection can be displayed as either text or graphics. The choices are displayed in single vertical column. Zero, one, or more of the choices can be selected by the user.

Code:
{curl 6.0, 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}


{value
   let my-list-box:ListBox =
       {ListBox
           style = "checkbutton",
           height = 1in,
           "Pizza",
           "Cheeseburger",
           "Spaghetti",
           "Lobster",
           "Barbeque ribs",
           "Beef stew",
           "Fried chicken"
       }

   {spaced-vbox
       {italic{bold Select one or more of your favorite foods:}},
       my-list-box
   }
}

Notice that scrollbars will automatically appear if the ListBox height is greater than the specified height. If the height is not specified, the ListBox will display all selections. Also, the style of the displayed items has been set to checkbutton which provides a small box to show what is selected. When an item is selected, a "check" graphic will appear in the box. The style can also be set to standard, in which it looks and behaves like a traditional list control.



If the requirement is that a maximum of one item can be selected from the list, you can set ListBox.selection-policy to "single" instead of the default policy of "multiple". You may also want to consider using ComboBox, DropdownList or RadioButton classes.



Creating List Items from Accessed Data
It is common that the selectable items are dynamically retrieved at runtime from an external source such as a database. We can query the data and temporarily store it using an array, hashtable, recordset, or other method of storage. The append or insert methods are used to add new items to the ListBox.


Items appended to a ListBox appear in the order in which they were added. For our example, we will store our list data in an array and then append each item in the array to the ListBox.

Code:
{curl 6.0, 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}


{value
   let my-list-box:ListBox =
       {ListBox
           style = "checkbutton",
           height = 1in
       }


   let food-array:{Array-of String} =
       {{Array-of String}
           "Pizza",
           "Cheeseburger",
           "Spaghetti",
           "Lobster",
           "Barbeque ribs",
           "Beef stew",
           "Fried chicken"
       }


   {for food:String in food-array do
       {my-list-box.append food}
   }

   {spaced-vbox
       {italic{bold Select one or more of your favorite foods:}},
       my-list-box
   }
}
Another method of gathering items from an array is to use splice. The splice macro returns the elements of the array as a list of arguments as shown below.

Code:
{curl 6.0, 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}

{value
   let food-array:{Array-of String} =
       {{Array-of String}
           "Pizza",
           "Cheeseburger",
           "Spaghetti",
           "Lobster",
           "Barbeque ribs",
           "Beef stew",
           "Fried chicken"
       }
   let my-list-box:ListBox =
       {ListBox
           style = "checkbutton",
           height = 1in,
           {splice food-array}
       }
  
   {spaced-vbox
       {italic{bold Select one or more of your favorite foods:}},
       my-list-box
   }
}
Tip: Sometimes list items need to be added after the actual control has been created. ListModelControl provides methods for manipulating items such as append, remove, clear, delete and find.


Getting Selected Values
Once the user has made their selections, you will need to gather the information for display or further processing. The user selected items in a ListBox are stored as an array in the value property. We can loop through the items of the array to obtain the selected values.


In our example below, the selected values are gathered and displayed in a VBox.

Code:
{curl 6.0, 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}


{value

   let my-list-box:ListBox =
       {ListBox
           style = "checkbutton",
           height = 1in
       }

   let food-array:{Array-of String} =
       {{Array-of String}
           "Pizza",
           "Cheeseburger",
           "Spaghetti",
           "Lobster",
           "Barbeque ribs",
           "Beef stew",
           "Fried chicken"
       }


   {for food:String in food-array do
       {my-list-box.append food}
   }

   let selection-button:CommandButton =  
       {CommandButton label = "Press for selections",
           {on Action at box:CommandButton do
               let selections:VBox = {VBox spacing=2pt}
               {for val in my-list-box.value do
                   {selections.add val}
               }

               {popup-message
                   {HBox
                       {text You have selected:   },
                       {value selections}
                   }
               }
           }
       }

   {spaced-vbox
       {italic{bold Select one or more of your favorite foods:}},
       my-list-box,
       selection-button
   }
}

For more information regarding List Controls, please refer to the following section in the Curl Documentation:Curl Developer's Guide > Graphical User Interface > List Controls



The next example takes input from a TextField and appends those values to a ListBox. The ListBox displays the current entries.

Code:
{curl 6.0, 7.0 applet}
{curl-file-attributes character-encoding = "utf8"}


{value
   let name-list:DefaultListModel = {DefaultListModel}
   let name-listbox:ListBox =
       {ListBox
           data-model = name-list
       }
  
   {VBox
       {bold Type items to add and press enter},
       {TextField
           {on ValueFinished at text:TextField do
               {name-list.append text.value}
           }
       },
       {bold Here are the current items in the {italic List}},
       name-listbox
   }
}

Notice that unset-value is used to clear the input value in the TextField when the user presses enter.




For more information regarding Text Controls, please refer to the following section in the Curl Documentation:Curl Developer's Guide > Graphical User Interface > Text Controls



Possibly Related Threads...
Thread Author Replies Views Last Post
  XML1:XML Data kino 3 9,750 10-21-2013, 12:48 PM
Last Post: Leta56
  Persistent Data1:Client-side Persistent Data kino 0 6,565 06-15-2011, 05:24 PM
Last Post: kino
  Files over HTTP4:Viewing RecordSet Data kino 0 4,859 06-15-2011, 04:46 PM
Last Post: kino
  Files over HTTP3:Connected Data kino 0 7,072 06-15-2011, 04:42 PM
Last Post: kino
  Files over HTTP2:Organizing Data using RecordSets kino 0 5,492 06-15-2011, 04:35 PM
Last Post: kino
  User Interface Basics7:Stretching Graphical Objects kino 0 4,781 06-15-2011, 04:12 PM
Last Post: kino
  User Interface Basics6:Object Layout kino 0 4,507 06-15-2011, 04:08 PM
Last Post: kino
  User Interface Basics5:Swapping UI Content kino 0 3,500 06-15-2011, 04:03 PM
Last Post: kino
  User Interface Basics3:Creating Dialogs kino 0 3,360 06-15-2011, 03:55 PM
Last Post: kino
  User Interface Basics2:Adding Pop-ups kino 0 3,718 06-15-2011, 03:52 PM
Last Post: kino
Forum Jump:


Users browsing this thread:
2 Guest(s)

MyBB SQL Error

MyBB has experienced an internal SQL error and cannot continue.

SQL Error:
1017 - Can't find file: 'mybb_threadviews' (errno: 2)
Query:
INSERT INTO mybb_threadviews (tid) VALUES('12')