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 / step4 / sectform.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  248 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 "AddForm.h"
  11. #include "SectForm.h"
  12. #include "MainFrm.h"
  13.  
  14. #ifdef _DEBUG
  15. #define new DEBUG_NEW
  16. #undef THIS_FILE
  17. static char THIS_FILE[] = __FILE__;
  18. #endif
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CSectionForm
  22.  
  23.  
  24. IMPLEMENT_DYNCREATE(CSectionForm, CAddForm)
  25.  
  26. BEGIN_MESSAGE_MAP(CSectionForm, CAddForm)
  27.  
  28.     //{{AFX_MSG_MAP(CSectionForm)
  29.     ON_CBN_SELENDOK(IDC_COURSELIST, OnSelendokCourselist)
  30.     ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
  31.     ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
  32.     //}}AFX_MSG_MAP
  33.     // Standard printing commands
  34.     ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
  35.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
  36.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
  37. END_MESSAGE_MAP()
  38.  
  39. /////////////////////////////////////////////////////////////////////////////
  40. // CSectionForm construction/destruction
  41.  
  42. CSectionForm::CSectionForm()
  43.     : CAddForm(CSectionForm::IDD)
  44. {
  45.     //{{AFX_DATA_INIT(CSectionForm)
  46.     m_pSet = NULL;
  47.     //}}AFX_DATA_INIT
  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.     CEnrollDoc* pDoc = GetDocument();
  81.     m_pSet = &pDoc->m_sectionSet;
  82.     m_pSet->m_pDatabase = pDoc->GetDatabase();
  83.     if (!m_pSet->m_pDatabase->IsOpen())
  84.         return;
  85.  
  86.     // Fill the combo box with all of the courses
  87.  
  88.     pDoc->m_courseSet.m_strSort = "CourseID";
  89.     if (pDoc->m_courseSet.m_pDatabase == NULL)
  90.         pDoc->m_courseSet.m_pDatabase = pDoc->GetDatabase();
  91.     if (!pDoc->m_courseSet.Open())
  92.         return;
  93.  
  94.     // Filter, parameterize and sort the course recordset
  95.     m_pSet->m_strFilter = "CourseID = ?";
  96.     m_pSet->m_strCourseIDParam = pDoc->m_courseSet.m_CourseID;
  97.     m_pSet->m_strSort = "SectionNo";
  98.  
  99.     CRecordView::OnInitialUpdate();
  100.  
  101.     m_ctlCourseList.ResetContent();
  102.     if (pDoc->m_courseSet.IsOpen())
  103.     {
  104.         while (pDoc->m_courseSet.IsEOF() != TRUE)
  105.         {
  106.             m_ctlCourseList.AddString(
  107.                 pDoc->m_courseSet.m_CourseID);
  108.             pDoc->m_courseSet.MoveNext();
  109.         }
  110.     }
  111.     m_ctlCourseList.SetCurSel(0);
  112. }
  113.  
  114. void CSectionForm::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
  115. {
  116.     BOOL bReselectCombo = FALSE;
  117.     int nIndex;
  118.     CUpdateHint* pUpdateHint;
  119.     if (lHint != 0)
  120.     {
  121.         pUpdateHint = (CUpdateHint*)pHint;
  122.         ASSERT(pUpdateHint->IsKindOf(RUNTIME_CLASS(CUpdateHint)));
  123.         switch (lHint)
  124.         {
  125.             case HINT_ADD_COURSE:
  126.                 m_ctlCourseList.AddString(pUpdateHint->m_strCourse);
  127.                 bReselectCombo = TRUE;
  128.                 break;
  129.             case HINT_DELETE_COURSE:
  130.                 nIndex = m_ctlCourseList.FindStringExact(0,
  131.                     pUpdateHint->m_strCourse);
  132.                 ASSERT(nIndex != CB_ERR);
  133.                 m_ctlCourseList.DeleteString(nIndex);
  134.                 bReselectCombo = TRUE;
  135.                 break;
  136.         }
  137.     }
  138.     if (bReselectCombo)
  139.     {
  140.         nIndex = m_ctlCourseList.FindStringExact(0,
  141.             m_pSet->m_strCourseIDParam);
  142.         if (nIndex == CB_ERR)
  143.             return;
  144.         m_ctlCourseList.SetCurSel(nIndex);
  145.     }
  146.     CRecordView::OnUpdate(pSender, lHint, pHint);
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150. // CSectionForm printing
  151.  
  152. BOOL CSectionForm::OnPreparePrinting(CPrintInfo* pInfo)
  153. {
  154.     // default preparation
  155.     return DoPreparePrinting(pInfo);
  156. }
  157.  
  158. void CSectionForm::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  159. {
  160.     // TODO: add extra initialization before printing
  161. }
  162.  
  163. void CSectionForm::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  164. {
  165.     // TODO: add cleanup after printing
  166. }
  167.  
  168. /////////////////////////////////////////////////////////////////////////////
  169. // CSectionForm diagnostics
  170.  
  171. #ifdef _DEBUG
  172. void CSectionForm::AssertValid() const
  173. {
  174.     CRecordView::AssertValid();
  175. }
  176.  
  177. void CSectionForm::Dump(CDumpContext& dc) const
  178. {
  179.     CRecordView::Dump(dc);
  180. }
  181.  
  182. CEnrollDoc* CSectionForm::GetDocument() // non-debug version is inline
  183. {
  184.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CEnrollDoc)));
  185.     return (CEnrollDoc*)m_pDocument;
  186. }
  187. #endif //_DEBUG
  188.  
  189. /////////////////////////////////////////////////////////////////////////////
  190. // CSectionForm database support
  191. CRecordset* CSectionForm::OnGetRecordset()
  192. {
  193.     return m_pSet;
  194. }
  195.  
  196.  
  197. /////////////////////////////////////////////////////////////////////////////
  198. // CSectionForm message handlers
  199.  
  200.  
  201. void CSectionForm::OnSelendokCourselist()
  202. {
  203.     if (!m_pSet->IsOpen())
  204.         return;
  205.     m_ctlCourseList.GetLBText(m_ctlCourseList.GetCurSel(),
  206.         m_pSet->m_strCourseIDParam);
  207.     if (!m_bAddMode)
  208.     {
  209.         m_pSet->Requery();
  210.         if (m_pSet->IsEOF())
  211.         {
  212.             m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
  213.             m_pSet->m_CourseID = m_pSet->m_strCourseIDParam;
  214.         }
  215.         UpdateData(FALSE);
  216.     }
  217. }
  218.  
  219. void CSectionForm::OnRecordAdd()
  220. {
  221.     // If already in add mode, then complete previous new record
  222.     if (m_bAddMode)
  223.         OnMove(ID_RECORD_FIRST);
  224.  
  225.     CString strCurrentCourse = m_pSet->m_CourseID;
  226.     m_pSet->AddNew();
  227.     m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
  228.     m_pSet->m_CourseID = strCurrentCourse;
  229.     m_bAddMode = TRUE;
  230.     m_ctlSection.SetReadOnly(FALSE);
  231.     UpdateData(FALSE);
  232. }
  233.  
  234.  
  235. void CSectionForm::OnRecordRefresh()
  236. {
  237.     if (m_bAddMode == TRUE)
  238.         m_ctlSection.SetReadOnly(TRUE);
  239.     CAddForm::RecordRefresh();
  240.  
  241. }
  242. BOOL CSectionForm::OnMove(UINT nIDMoveCommand)
  243. {
  244.     if (CAddForm::OnMove(nIDMoveCommand))
  245.         m_ctlSection.SetReadOnly(TRUE);
  246.     return TRUE;
  247. }
  248.