When you created the simple VisualAge application, you used an IListBox* part to display the items in a to-do list. The items you put in the IListBox* part were text strings created by entering text in an entry field and selecting a push button. In this case, the text strings displayed in the list box are just text strings.
Besides viewing a list of text strings, you can also use a list box to view a collection of nonvisual objects. The following list boxes that can display objects:
By connecting a collection of objects, such as an IVSequence* part, to one of these list boxes, you can view a collection of objects instead of simple text strings.
The procedure outlined in this section modifies the original To-Do List application so that the list box contains objects rather than text strings. In this case, text strings are again displayed in the list box, but are used to show the names of the objects in the sequence. When you select an item in the list box after completing this procedure, you are really selecting an object in the sequence, not the text string that represents the object.
The modified To-Do List application differs from the original application in that it uses the following parts:
The following prerequisites must be met to reflect a sequence of objects in a list box:
| Attribute name | toDoItemName |
| Attribute type | IString |
When you specify the .hpv and .cpv files, VisualAge uses them for the default feature code of the toDoItemName attribute. Specify these files by doing the following:
Switch to the User Code tab with Required include files selected, enter <istring.h> in the bottom pane.
VisualAge generates the code for the ToDoItem part and stores it in files named ToDoItem.hpp and ToDoItem.cpp. These files contain the appropriate include statements for the ToDoItem.hpv and ToDoItem.cpv files. VisualAge also creates a header file for the declarations (ToDoItem.hv) and a resource include file for the class (ToDoItem.rci).
You need a list box that can display objects instead of just text strings. Therefore, you must replace the IListBox* part with an ICollectionViewListBox* part, and modify the ICollectionViewListBox* part to specify the type of objects it can display.
The Composition Editor displays a message advising you that it will delete the connections as well as the list box if you continue. You want to delete the connections because you need different connections for this example.
A list box part that displays the items in a collection appears beneath the second static text part.
The list box can now accept only ToDoItem* objects.
You need a Factory part to create the objects displayed in the list box. You must also specify the type of objects the Factory part can create.
The object factory can now create only ToDoItem objects.
You need to use a Variable and IVSequence* part to sequentially order the objects that the Factory part creates, and you must specify the type of objects the sequence can contain.
Next, place the IVSequence part.
The sequence can now accept only ToDoItem objects.
The following table outlines the connections that this example
requires to add objects to and remove objects from the list box.
| Connect Item... | Feature... | To Item... | Feature... |
|---|---|---|---|
| Add push button (1) | buttonClickEvent | ItemFactory | new |
| ItemFactory (2) | toDoItemName | entry field | text |
| ItemFactory (3) | newEvent | ItemList | addAsLast |
| previous connection | element | ItemVariable | this |
| ItemFactory | newEvent | ItemVariable | this |
| ItemList (4) | this | list box | items |
| Remove push button (5) | buttonClickEvent | ItemList | removeAtPosition |
| previous connection (6) | position | list box | selectedCollectionPosition |
1. This connection causes the object factory to create a new ToDoItem object whenever a user selects Add.
2. This connection identifies text in the entry field as the name of ToDoItem objects created by the object factory. Notice that the connection line is violet, the color of a parameter connection, instead of cyan, which is the color of an attribute-to-attribute connection. The connection line is violet because the text attribute of the entry field supplies a value for the ToDoItemName attribute only when the object factory creates a new object, just as if it were satisfying a parameter of the new action.
3. This connection causes each new ToDoItem object to be added as the last object in the sequence.
4. This connection causes the list box to display all of the ToDoItem objects that the sequence contains.
5. This connection causes the object at a specified position in the sequence to be removed. This connection is incomplete because the removeAtPosition parameter must be satisfied.
6. This connection provides the position of the selected item in the collection of objects, which is the required parameter the previous connection needs to remove the object.
The next step is Building and Executing Your To-Do List Application.
