A listbox is a window which contains a list of items. Each item appears on as separate line. Items may be selected by pointing at the line they appear on and clicking the left button. Click here for a list of the functions for TListBox.
The grocery problem is an ideal candidate for a list box solution. The document consists of a list of grocery items and at this point there is no way to edit the list of items. If we display the items in a list box, then the user can select an item to edit and we can alter the values for that item. The user should also be able to delete an item by selecting it in the list box and choosing a delete command.
The first step in creating this program with a TListBox as its main window. To do this select TListBox from the client/view class pull-down listbox in the AppExpert Applications Generation Options dialog. This will create project with a TListBox as a child of the application window.
As in earlier examples, once you have a project you can add menus and functions using the Resource Workshop and ClassExpert. This project is not a data base where there is an interaction between items, so I am storing each item as a string. This allows us to use some of the built-in I/O functions and reduces the complexity of the problem. In later examples we will create an underlying structure that is shown on the screen. Here the structure is what is stored in the list box.
The function that adds an item to the list box illustrates many of the methods needed to maintain this type of program. Data will be entered using a dialog box constructed in the resource editor. The class corresponding to this dialog box (NewGroceryItem) uses a transfer buffer to retrieve information about the new grocery item. This data is used to create a grocery item and that item provides the string that will represent it in the list box. This data is inserted at the end of the list using AddString. Notice that in this project the class ***Window is replaced by ***ListBox, so changes can be made directly in the list box.
The dialog box for NewGroceryItem looks like this. |
void Exam5aListBox::cmAddItem ()
{
// INSERT>> Your code here.
NewGroceryItemXfer tb;
strcpy(tb._cost,"0.00");
strcpy(tb._id,"");
strcpy(tb._inStock,"0");
strcpy(tb._name,"");
if (NewGroceryItem(&tb,this).Execute()==IDOK)
{
double cost = atof(tb._cost);
int howMany = atoi(tb._inStock);
GroceryItem g(tb._name,tb._id,cost,howMany);
char buff[255];
strstream out(buff,255,ios::out);
g.print(out);
AddString(buff);
}
}
The edit command is very similar to the new item with the exception of the fact that we set the fields of the NewGroceryItem with the values in the current item. If we want an item such as the id number to remain fixed, we could use a different dialog box with the fixed fields represented by static text of type TStatic. Since we are replacing a given item we must delete that item with DeleteString and then insert at the same spot using InsertString.
void Exam5aListBox::cmEditItem ()
{
// INSERT>> Your code here.
int selection=GetSelIndex();
if (selection >= 0) // have selected something
{
char buff[255]; // retrieve the string here
GetSelString(buff,255);
// Get the parts of this string and put them in a listbox
NewGroceryItemXfer tb;
istrstream in(buff,255); //use a string stream to get values
in >> tb._name; // both name and id have no spaces inside
in >> tb._id;
in >> tb._cost;
in >> tb._inStock;
if (NewGroceryItem(&tb,this).Execute()==IDOK)
{
double cost = atof(tb._cost);
int howMany = atoi(tb._inStock);
GroceryItem g(tb._name,tb._id,cost,howMany);
char buff[255];
strstream out(buff,255,ios::out);
g.print(out);
DeleteString(selection);
InsertString(buff,selection);
}
}
}
For a copy of the complete program click here.
List Boxes are very useful for presenting choices to a user when the choices are volitile and cannot be predicted at the time a program is created. They, in effect, become dynamic menus. An example of such a use can be seen by clicking here.