home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / tutorial / enroll / step3 / sectform.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  6.1 KB  |  264 lines

  1. // SectForm.cpp : implementation of the CSectionForm class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "Enroll.h"
  6.  
  7. #include "SectSet.h"
  8. #include "CoursSet.h"
  9. #include "EnrolDoc.h"
  10. #include "SectForm.h"
  11.  
  12. #ifdef _DEBUG
  13. #define new DEBUG_NEW
  14. #undef THIS_FILE
  15. static char THIS_FILE[] = __FILE__;
  16. #endif
  17.  
  18. /////////////////////////////////////////////////////////////////////////////
  19. // CSectionForm
  20.  
  21. IMPLEMENT_DYNCREATE(CSectionForm, CRecordView)
  22.  
  23. BEGIN_MESSAGE_MAP(CSectionForm, CRecordView)
  24.  
  25.  
  26.     //{{AFX_MSG_MAP(CSectionForm)
  27.     ON_CBN_SELENDOK(IDC_COURSELIST, OnSelendokCourselist)
  28.     ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  29.     ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
  30.     ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
  31.     //}}AFX_MSG_MAP
  32.     // Standard printing commands
  33.     ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
  34.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
  35.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
  36. END_MESSAGE_MAP()
  37.  
  38. /////////////////////////////////////////////////////////////////////////////
  39. // CSectionForm construction/destruction
  40.  
  41. CSectionForm::CSectionForm()
  42.     : CRecordView(CSectionForm::IDD)
  43. {
  44.     //{{AFX_DATA_INIT(CSectionForm)
  45.     m_pSet = NULL;
  46.     //}}AFX_DATA_INIT
  47.     m_bAddMode = FALSE;
  48.  
  49. }
  50.  
  51. CSectionForm::~CSectionForm()
  52. {
  53. }
  54.  
  55. void CSectionForm::DoDataExchange(CDataExchange* pDX)
  56. {
  57.     CRecordView::DoDataExchange(pDX);
  58.     //{{AFX_DATA_MAP(CSectionForm)
  59.     DDX_Control(pDX, IDC_COURSELIST, m_ctlCourseList);
  60.     DDX_FieldText(pDX, IDC_SECTION, m_pSet->m_SectionNo, m_pSet);
  61.     DDX_Control(pDX, IDC_SECTION, m_ctlSection);
  62.     DDX_FieldText(pDX, IDC_INSTRUCTOR, m_pSet->m_InstructorID, m_pSet);
  63.     DDX_FieldText(pDX, IDC_ROOM, m_pSet->m_RoomNo, m_pSet);
  64.     DDX_FieldText(pDX, IDC_SCHEDULE, m_pSet->m_Schedule, m_pSet);
  65.     DDX_FieldText(pDX, IDC_CAPACITY, m_pSet->m_Capacity, m_pSet);
  66.     DDX_FieldCBString(pDX, IDC_COURSELIST, m_pSet->m_CourseID, m_pSet);
  67.     //}}AFX_DATA_MAP
  68. }
  69.  
  70. BOOL CSectionForm::PreCreateWindow(CREATESTRUCT& cs)
  71. {
  72.     // TODO: Modify the Window class or styles here by modifying
  73.     //  the CREATESTRUCT cs
  74.  
  75.     return CRecordView::PreCreateWindow(cs);
  76. }
  77.  
  78. void CSectionForm::OnInitialUpdate()
  79. {
  80.     m_pSet = &GetDocument()->m_sectionSet;
  81.  
  82.     // Fill the combo box with all of the courses
  83.  
  84.     CEnrollDoc* pDoc = GetDocument();
  85.     pDoc->m_courseSet.m_strSort = "CourseID";
  86.     if (!pDoc->m_courseSet.Open())
  87.         return;
  88.  
  89.     // Filter, parameterize and sort the course recordset
  90.     m_pSet->m_strFilter = "CourseID = ?";
  91.     m_pSet->m_strCourseIDParam = pDoc->m_courseSet.m_CourseID;
  92.     m_pSet->m_strSort = "SectionNo";
  93.     m_pSet->m_pDatabase = pDoc->m_courseSet.m_pDatabase;
  94.  
  95.     CRecordView::OnInitialUpdate();
  96.  
  97.     m_ctlCourseList.ResetContent();
  98.     if (pDoc->m_courseSet.IsOpen())
  99.     {
  100.         while (!pDoc->m_courseSet.IsEOF())
  101.         {
  102.             m_ctlCourseList.AddString(
  103.                 pDoc->m_courseSet.m_CourseID);
  104.             pDoc->m_courseSet.MoveNext();
  105.         }
  106.     }
  107.     m_ctlCourseList.SetCurSel(0);
  108. }
  109.  
  110. /////////////////////////////////////////////////////////////////////////////
  111. // CSectionForm printing
  112.  
  113. BOOL CSectionForm::OnPreparePrinting(CPrintInfo* pInfo)
  114. {
  115.     // default preparation
  116.     return DoPreparePrinting(pInfo);
  117. }
  118.  
  119. void CSectionForm::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  120. {
  121.     // TODO: add extra initialization before printing
  122. }
  123.  
  124. void CSectionForm::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  125. {
  126.     // TODO: add cleanup after printing
  127. }
  128.  
  129. /////////////////////////////////////////////////////////////////////////////
  130. // CSectionForm diagnostics
  131.  
  132. #ifdef _DEBUG
  133. void CSectionForm::AssertValid() const
  134. {
  135.     CRecordView::AssertValid();
  136. }
  137.  
  138. void CSectionForm::Dump(CDumpContext& dc) const
  139. {
  140.     CRecordView::Dump(dc);
  141. }
  142.  
  143. CEnrollDoc* CSectionForm::GetDocument() // non-debug version is inline
  144. {
  145.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEnrollDoc)));
  146.     return (CEnrollDoc*)m_pDocument;
  147. }
  148. #endif //_DEBUG
  149.  
  150. /////////////////////////////////////////////////////////////////////////////
  151. // CSectionForm database support
  152. CRecordset* CSectionForm::OnGetRecordset()
  153. {
  154.     return m_pSet;
  155. }
  156.  
  157.  
  158. /////////////////////////////////////////////////////////////////////////////
  159. // CSectionForm message handlers
  160.  
  161.  
  162. void CSectionForm::OnSelendokCourselist()
  163. {
  164.     if (!m_pSet->IsOpen())
  165.         return;
  166.     m_ctlCourseList.GetLBText(m_ctlCourseList.GetCurSel(),
  167.         m_pSet->m_strCourseIDParam);
  168.     if (!m_bAddMode)
  169.     {
  170.         m_pSet->Requery();
  171.         if (m_pSet->IsEOF())
  172.         {
  173.             m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
  174.             m_pSet->m_CourseID = m_pSet->m_strCourseIDParam;
  175.         }
  176.         UpdateData(FALSE);
  177.     }
  178. }
  179.  
  180. void CSectionForm::OnRecordAdd()
  181. {
  182.     // If already in add mode, then complete previous new record
  183.     if (m_bAddMode)
  184.         OnMove(ID_RECORD_FIRST);
  185.  
  186.     CString strCurrentCourse = m_pSet->m_CourseID;
  187.     m_pSet->AddNew();
  188.     m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
  189.     m_pSet->m_CourseID = strCurrentCourse;
  190.     m_bAddMode = TRUE;
  191.     m_ctlSection.SetReadOnly(FALSE);
  192.     UpdateData(FALSE);
  193. }
  194.  
  195. void CSectionForm::OnRecordDelete()
  196. {
  197.     TRY
  198.     {
  199.         m_pSet->Delete();
  200.     }
  201.     CATCH(CDBException, e)
  202.     {
  203.         AfxMessageBox(e->m_strError);
  204.         return;
  205.     }
  206.     END_CATCH
  207.  
  208.     // Move to the next record after the one just deleted
  209.         m_pSet->MoveNext();
  210.  
  211.     // If we moved off the end of file, then move back to last record
  212.     if (m_pSet->IsEOF())
  213.         m_pSet->MoveLast();
  214.  
  215.     // If the recordset is now empty, then clear the fields
  216.     // left over from the deleted record
  217.     if (m_pSet->IsBOF())
  218.         m_pSet->SetFieldNull(NULL);
  219.     UpdateData(FALSE);
  220. }
  221.  
  222. void CSectionForm::OnRecordRefresh()
  223. {
  224.     if (m_bAddMode == TRUE)
  225.     {
  226.         m_pSet->Move(AFX_MOVE_REFRESH);
  227.         m_ctlSection.SetReadOnly(TRUE);
  228.         m_bAddMode = FALSE;
  229.     }
  230.     // Copy fields from recordset to form, thus
  231.     // overwriting any changes user may have made
  232.     // on the form
  233.     UpdateData(FALSE);
  234.  
  235. }
  236. BOOL CSectionForm::OnMove(UINT nIDMoveCommand)
  237. {
  238.     if (m_bAddMode)
  239.     {
  240.         if (!UpdateData())
  241.             return FALSE;
  242.         TRY
  243.         {
  244.             m_pSet->Update();
  245.         }
  246.         CATCH(CDBException, e)
  247.         {
  248.             AfxMessageBox(e->m_strError);
  249.             return FALSE;
  250.         }
  251.         END_CATCH
  252.  
  253.         m_pSet->Requery();
  254.         UpdateData(FALSE);
  255.         m_ctlSection.SetReadOnly(TRUE);
  256.         m_bAddMode = FALSE;
  257.         return TRUE;
  258.     }
  259.     else
  260.     {
  261.         return CRecordView::OnMove(nIDMoveCommand);
  262.     }
  263. }
  264.