⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sectionform.cpp

📁 VC6数据库开发指南
💻 CPP
字号:
// SectionForm.cpp : implementation of the CSectionForm class
//

#include "stdafx.h"
#include "StdInfo.h"

#include "SectionSet.h"
#include "StdInfoDoc.h"
#include "SectionForm.h"
#include "CourseSet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CSectionForm

IMPLEMENT_DYNCREATE(CSectionForm, CRecordView)

BEGIN_MESSAGE_MAP(CSectionForm, CRecordView)
	//{{AFX_MSG_MAP(CSectionForm)
	ON_CBN_SELENDOK(IDC_COURSELIST, OnSelendokCourselist)
	ON_COMMAND(ID_RECORD_ADD, OnRecordAdd)
	ON_COMMAND(ID_RECORD_DELETE, OnRecordDelete)
	ON_COMMAND(ID_RECORD_REFRESH, OnRecordRefresh)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CRecordView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CRecordView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSectionForm construction/destruction

CSectionForm::CSectionForm()
	: CRecordView(CSectionForm::IDD)
{
	//{{AFX_DATA_INIT(CSectionForm)
	m_pSet = NULL;
	//}}AFX_DATA_INIT
	// TODO: add construction code here
	m_bAddMode = FALSE;
}

CSectionForm::~CSectionForm()
{
}

void CSectionForm::DoDataExchange(CDataExchange* pDX)
{
	CRecordView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSectionForm)
	DDX_Control(pDX, IDC_SECTION, m_ctlSection);
	DDX_Control(pDX, IDC_COURSELIST, m_ctlCourseList);
	DDX_FieldText(pDX, IDC_CAPACITY, m_pSet->m_Capacity, m_pSet);
	DDX_FieldText(pDX, IDC_INSTRUCTOR, m_pSet->m_InstructorID, m_pSet);
	DDX_FieldText(pDX, IDC_ROOM, m_pSet->m_RoomNo, m_pSet);
	DDX_FieldText(pDX, IDC_SCHEDULE, m_pSet->m_Schedule, m_pSet);
	DDX_FieldText(pDX, IDC_SECTION, m_pSet->m_SectionID, m_pSet);
	//}}AFX_DATA_MAP
}

BOOL CSectionForm::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CRecordView::PreCreateWindow(cs);
}

void CSectionForm::OnInitialUpdate()
{
	m_pSet = &GetDocument()->m_sectionSet;
		m_pSet = &GetDocument()->m_sectionSet;

	// Fill the combo box with all of the courses

	CStdInfoDoc* pDoc = GetDocument();
	pDoc->m_courseSet.m_strSort = "课号";
	if (!pDoc->m_courseSet.Open())
		return;

	// Filter, parameterize and sort the course recordset
	m_pSet->m_strFilter = "课号 = ?";
	m_pSet->m_strCourseIDParam = pDoc->m_courseSet.m_CourseID;
	m_pSet->m_strSort = "班号";
	m_pSet->m_pDatabase = pDoc->m_courseSet.m_pDatabase;

	CRecordView::OnInitialUpdate();

	m_ctlCourseList.ResetContent();
	if (pDoc->m_courseSet.IsOpen())
	{
		while (!pDoc->m_courseSet.IsEOF())
		{
			m_ctlCourseList.AddString(
				pDoc->m_courseSet.m_CourseID);
			pDoc->m_courseSet.MoveNext();
		}
	}
	m_ctlCourseList.SetCurSel(0);
}

/////////////////////////////////////////////////////////////////////////////
// CSectionForm printing

BOOL CSectionForm::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CSectionForm::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CSectionForm::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CSectionForm diagnostics

#ifdef _DEBUG
void CSectionForm::AssertValid() const
{
	CRecordView::AssertValid();
}

void CSectionForm::Dump(CDumpContext& dc) const
{
	CRecordView::Dump(dc);
}

CStdInfoDoc* CSectionForm::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStdInfoDoc)));
	return (CStdInfoDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CSectionForm database support
CRecordset* CSectionForm::OnGetRecordset()
{
	return m_pSet;
}


/////////////////////////////////////////////////////////////////////////////
// CSectionForm message handlers

void CSectionForm::OnSelendokCourselist() 
{
	// TODO: Add your control notification handler code here
	if (!m_pSet->IsOpen())
		return;
	m_ctlCourseList.GetLBText(m_ctlCourseList.GetCurSel(),
		m_pSet->m_strCourseIDParam);
	m_pSet->Requery();
	if (m_pSet->IsEOF())
	{
		m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
		m_pSet->m_CourseID = m_pSet->m_strCourseIDParam;
	}
	UpdateData(FALSE);
}

BOOL CSectionForm::OnMove(UINT nIDMoveCommand) 
{
	// TODO: Add your specialized code here and/or call the base class
	if (m_bAddMode)
	{
		if (!UpdateData())
			return FALSE;
		TRY
		{
			m_pSet->Update();
		}
		CATCH(CDBException, e)
		{
			AfxMessageBox(e->m_strError);
			return FALSE;
		}
		END_CATCH

		m_pSet->Requery();
		UpdateData(FALSE);
		m_ctlSection.SetReadOnly(TRUE);
		m_bAddMode = FALSE;
		return TRUE;
	}
	else
	{
		return CRecordView::OnMove(nIDMoveCommand);
	}
}

void CSectionForm::OnRecordAdd() 
{
	// 如果依然处于编辑状态,则完成以前的新记录的添加
	if (m_bAddMode)
		OnMove(ID_RECORD_FIRST);

	CString strCurrentCourse = m_pSet->m_CourseID;
	m_pSet->AddNew();
	m_pSet->SetFieldNull(&(m_pSet->m_CourseID), FALSE);
	m_pSet->m_CourseID = strCurrentCourse;
	m_bAddMode = TRUE;
	m_ctlSection.SetReadOnly(FALSE);
	UpdateData(FALSE);
}

void CSectionForm::OnRecordDelete() 
{
	// TODO: Add your command handler code here
	TRY
	{
		m_pSet->Delete();
	}
	CATCH(CDBException, e)
	{
		AfxMessageBox(e->m_strError);
		return;
	}
	END_CATCH

	// 删除后,移动到下一个记录
		m_pSet->MoveNext();

	// 如果已经移动到最后一个记录,就移回到第一个记录
	if (m_pSet->IsEOF())
		m_pSet->MoveLast();

	// 如果记录集是空集,则清空被删除的记录的字段
	if (m_pSet->IsBOF())
		m_pSet->SetFieldNull(NULL);
	UpdateData(FALSE);	
}

void CSectionForm::OnRecordRefresh() 
{
	// TODO: Add your command handler code here
	if (m_bAddMode == TRUE)
	{
		m_pSet->Move(AFX_MOVE_REFRESH);
		m_ctlSection.SetReadOnly(TRUE);
		m_bAddMode = FALSE;
	}
	// 从记录集中将字段值拷贝到视图中,
	// 覆盖任何用户对记录的任何修改
	UpdateData(FALSE);	
}

void CSectionForm::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint)
{
	BOOL bReselectCombo = FALSE;
	int nIndex;
	CUpdateHint* pUpdateHint;
	if (lHint != 0)
	{
		pUpdateHint = (CUpdateHint*)pHint;
		ASSERT(pUpdateHint->IsKindOf(RUNTIME_CLASS(CUpdateHint)));
		switch (lHint)
		{
			case HINT_ADD_COURSE:
				m_ctlCourseList.AddString(pUpdateHint->m_strCourse);
				bReselectCombo = TRUE;
				break;
			case HINT_DELETE_COURSE:
				nIndex = m_ctlCourseList.FindStringExact(0,
					pUpdateHint->m_strCourse);
				ASSERT(nIndex != CB_ERR);
				m_ctlCourseList.DeleteString(nIndex);
				bReselectCombo = TRUE;
				break;
		}
	}
	if (bReselectCombo)
	{
		nIndex = m_ctlCourseList.FindStringExact(0,
			m_pSet->m_strCourseIDParam);
		if (nIndex == CB_ERR)
			return;
		m_ctlCourseList.SetCurSel(nIndex);
	}
	CRecordView::OnUpdate(pSender, lHint, pHint);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -