In Class Exercise 1.5

The Problem

The license program from the lab 3 has been embbed in a windows interface with a few additions. You will lean how to pass information between application objects and interface objects. You will also learn how to add non windows code files to a windows project. Get the base project by clicking here .

The Windows Changes

First create a class LicenseList as a public descendent of Array< LicensePtr>. This class has two functions of importance, print and read that make it easier to implement an interface. There is nothing unusual about this code it is very similar to the grocery problem.

void  LicenseList::read(istream &in)
{
       int size;
       in>>size;
       for (int i = 0;i< size;i++)
      {
         LicensePtr l;
         int type;
         in>>type;
         if  (type == License::CAR)
             l =  new  CarLicense;
         else
             l =  new  TruckLicense;
         l->read(in);
         insertItem(l);
     }
}

void  LicenseList::print(ostream &out)
{
   ArrayIterator next(*this);
   while(next)
   {
      next()->print(out);
      out<< endl;
      next++;
   }
  out<< '\0' ;  //needed since we are actually creating a String here
  
}

The main form WM_PAINT hander is very simple now.

 void __fastcall  TMainForm::FormPaint(TObject *Sender)
{
   char buff[32000];
   strstream out(buff,32000);
   _licenses.print(out);
   LicenseListBox->Items->Text = String(buff);
}
 //---------------------------------------------------------------------------  

Check the settings on the TabWidth of LicenseListBox in the Object Inspector, this allows you to use tabs to separate the entries in the print functions for the Licenses.

Create The Dialog Interface

Given the project, create a dialog to retrieve the license information. Check out the components used in dialog boxes . This dialog is given below. Notice that you will use radio buttons to retrieve the type of license.

The Dialog

Create a new dialog with ok and cancel button at the bottom. Then execute the following steps.
Select "GroupBox" from the Standard Component Pallet. Place this in your dialog.
In the object inspector change the Caption of the "GroupBox" to owner. Then in the object inspector change the font style by clicking on the font box and changing the color to red and the style to bold. Add a TEdit box inside this groupbox, change its name to "Name" (Owner is a predefined type), and its font back to black, normal. Finally delete the Text commonent value in the object inspector. Copy the GroupBox and after deselecting the GroupBox paste in the dialog. You should now have a second groupbox and edit box. Change the Caption of the GroupBox and the name of the textbox to License
Place radio button groupbox to the right of the two you have created. Name it LicenseType. Change its caption to License Type and its font to bold red. Next click on the Items value box to bring up the String List Editor. There you can type Car Licens and Truck License on separate lines and close the editor. You should now have two radio buttons in the panel. Before you move to the next step, set ItemIndex to 0 (the first type of license.
To display your dialog, include the dialog in the main form, Move LicenseDialog from the auto create list to the available list in the Forms tab of the Project/Options dialog, and change the constructor to look like this. After the test delete this series of steps from the constructor.
__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
        LicenseDialog = new TLicenseDialog(Owner);
        LicenseDialog->ShowModal();
        delete LicenseDialog;
}
Add the License cpp file to the project using the project window. Create an object of type LicensePtr in the private section of MainForm.

Coding the Program

Disabling and Enabling DialogBox Components

In this problem you want to enable the ok button only when the owner and license number are filled in, and in addition you want the number of wheels and the weight if you have a truck. Both the number of wheels and the weight must be numeric and only active when truck is checked. If the you want to restrict the input, you have filter the data inserted into the TEditBox in much the same way you did in the payroll problem .

The Menu Items

The Add, Delete and Edit functions are very similar to those presented in the Payroll problem. One exception is the radio buttons now are accessed by their index. On return from the dialogbox, you create the approriate type of License pointer using the following code:

  if  (LicenseDialog->LicenseType->ItemIndex == 0)
         l =  new  CarLicense(LicenseDialog->License->Text,LicenseDialog->Name->Text);
       else 
         l =  new  TruckLicense(LicenseDialog->License->Text,LicenseDialog->Name->Text,

Using The ListBox in The Main Form

The delete and edit functions require that you choose an item to edit. One way to do this is to use the Array find function to get the subscript of the item in question. In the edit problem you may get the data directly from the LicenseList and use the License dialog to change the data. In either the edit or delete, you delete the item in the list at the given position. In the edit, you insert the new item in the same position in the array. After completing this, call on Invalidate to redraw the listbox.

Completed Code

To get the completed code for this project click here .