In our first windows version of the grocery list problem, we used the TInputDialog to retrieve information needed to create a new GroceryItem. This was very clumsy and users would find this type of thing annoying. In addition they would not be able to change values if they discover mistakes after viewing all of the information. We will replace the TInputDialogs with one dialog that will collect all of the information about the new item.
The design of this dialog is very straight forward. Simply ask what information must be entered to create a new grocery item. We need to get the name of the item, its id, unit cost and number of items. Each of these is well-suited to text input, so we might sketch the following version of the dialog. The following is a crude sketch of this dialog.
After deciding on the design of the box, we can use the resource workshop to create a new dialog. Notice that in this case each of the fields that have to be filled in can be text fields The edit box is added using the text edit box tool.
After you have added the NewGroceryDialog class, and created the instance variables for each text edit box, you will find the following transfer buffer information in the dialog box header.
//{{TDialog = NewGroceryItem}}
struct NewGroceryItemXfer {
//{{NewGroceryItemXFER_DATA}}
char _cost[ 255 ];
char _id[ 255 ];
char _inStock[ 255 ];
char _name[ 255 ];
//{{NewGroceryItemXFER_DATA_END}}
};
Now add the new constructor
NewGroceryItem (NewGroceryItemXfer *tb,TWindow* parent, TResId resId = IDD_NEW_GROCERY_ITEM, TModule* module = 0);to the header and cpp file.
The cost and number in stock are numerical items. You can stop the user from entering non-numerical characters by adding a validators to each of these text edit boxes. At this point all you have left to do is add the code to extract the values from each of the buffers and create the new grocery item. Here is the code for this function.
void Exam5Window::cmAddItem ()
{
// INSERT>> Your code here.
NewGroceryItemXfer tb;
tb._name[0]='\0';
tb._id[0]= '\0';
strcpy(tb._inStock,"0");
strcpy(tb._cost,"0.00");
if (NewGroceryItem(&tb,this).Execute()==IDOK)
{
double cost = atof(tb._cost);
int howMany = atoi(tb._inStock);
GroceryItem g(tb._name,tb._id,cost,howMany);
groceries.addItem(g);
}
}