Solution to The Delete Problem in Our Course Example

Add Delete Information to ProfArray

Alter the ProfArray from a typedef to a class declaration. This allows you to store information about the deleted item and to add functions to handle various aspects of the delete operation.

typedef RcPointer ProfPtr;
class ProfArray :public Array
{
public:
	ProfArray(){}
	void deleteItemAt(int where);
	ProfPtr getDeletedProf(){return _oldValue;}
	int getEditNumber(){return _editNumber;}
	void undoDelete(){insertItemAt(_oldValue,_editNumber);}
private:
	ProfPtr _oldValue;
	int _editNumber;
};

typedef RcPointer ProfListPtr;

Add Message to Main Window

First go to the resource editor and view the identifiers. Next add the identifier WM_DELETE_PROFESSOR to the list of idenifiers.
Add the declaration of the deleteProf function (message handler) to the class definition. Make sure to put the definition inside the comments for RSP_TBL. Notice than any message handler must have return type LRESULT and parameters WMPARAM and LPARAM. We won't use these parameters.
//{{Week1_98ListBoxRSP_TBL_BEGIN}}
protected:

    void EvPaint ();
    LRESULT deleteProf (WPARAM,LPARAM);
//{{Week1_98ListBoxRSP_TBL_END}}
Associate the handler with the message in the response table found in the cpp file.
DEFINE_RESPONSE_TABLE1(Week1_98ListBox, TListBox)
//{{Week1_98ListBoxRSP_TBL_BEGIN}}
	 EV_WM_PAINT,
	 EV_MESSAGE(WM_DELETE_PROFESSOR, deleteProf),
//{{Week1_98ListBoxRSP_TBL_END}}
END_RESPONSE_TABLE;
Add the handler in the cpp file.
LRESULT Week1_98ListBox::deleteProf (WPARAM,LPARAM)
{
     _courseWind->processDelete();
    Invalidate();
    return 1;
}

Add The New Function to The Course Window

Once we have deleted the professor, and asked the course window to process this delete, we need to ask what the course window will do.

Course windows response
void CourseWindow::processDelete()
{
    // This function is called from Week1_98ListBox in response
    // to a WM_DELETE_PROFESSOR message.
    // First find all courses taught by deleted prof then alter each in turn

    // prepare a course dialog box for use in the main loop
    CourseDialogXfer tb;
    CourseList undoList = _courses->copy();
    ArrayIterator next(*_professors);
    while(next)
    {
        char buff[255];
        next()->name(buff);
        tb._prof.AddString(buff);
        next++;
    }
    Course test("",_professors->getDeletedProf());
    int which=_courses->findNextCourseWithProf(test,0);

    while (which >=0)
    {
         tb._prof.Select(0);
         (*_courses)[which].number(tb._number);
        if(CourseDialog(&tb,this).Execute()==IDOK)
         {
             Course c(tb._number,(*_professors)[tb._prof.GetSelIndex()]);
             (*_courses)[which]= c;
             PostMessage(WM_PAINT,0,0);
             which = _courses->findNextCourseWithProf(test,++which);
        }
       else
       {
            _courses = new CourseList(undoList);
            _professors->undoDelete();
            return;
       }
       Invalidate();
    }
}

The function findNextCourseWithProf looks for the next occurence of the professor as an instructor for a course. Notice that which is incremented before the call to this function by using the prefix ++ (++which).

Back to Week 3