📄 studentview.cpp
字号:
// StudentView.cpp : implementation of the CStudentView class
//
#include "stdafx.h"
#include "Student.h"
#include "StudentSet.h"
#include "StudentDoc.h"
#include "StudentView.h"
#include "FindDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CStudentView
IMPLEMENT_DYNCREATE(CStudentView, CRecordView)
BEGIN_MESSAGE_MAP(CStudentView, CRecordView)
//{{AFX_MSG_MAP(CStudentView)
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_BN_CLICKED(IDC_DELETE, OnDelete)
ON_BN_CLICKED(IDC_MODIFY, OnModify)
ON_BN_CLICKED(IDC_FIND, OnFind)
//}}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()
/////////////////////////////////////////////////////////////////////////////
// CStudentView construction/destruction
CStudentView::CStudentView()
: CRecordView(CStudentView::IDD)
{
//{{AFX_DATA_INIT(CStudentView)
m_pSet = NULL;
//}}AFX_DATA_INIT
// TODO: add construction code here
}
CStudentView::~CStudentView()
{
}
void CStudentView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CStudentView)
DDX_FieldText(pDX, IDC_ADDRESS, m_pSet->m_Address, m_pSet);
DDX_FieldText(pDX, IDC_AGE, m_pSet->m_Age, m_pSet);
DDX_FieldText(pDX, IDC_CLASSID, m_pSet->m_ClassID, m_pSet);
DDX_FieldText(pDX, IDC_NAME, m_pSet->m_Name, m_pSet);
DDX_FieldText(pDX, IDC_STUDENTID, m_pSet->m_StudentID, m_pSet);
//}}AFX_DATA_MAP
}
BOOL CStudentView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CRecordView::PreCreateWindow(cs);
}
void CStudentView::OnInitialUpdate()
{
m_pSet = &GetDocument()->m_studentSet;
CRecordView::OnInitialUpdate();
GetParentFrame()->RecalcLayout();
ResizeParentToFit();
}
/////////////////////////////////////////////////////////////////////////////
// CStudentView printing
BOOL CStudentView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CStudentView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CStudentView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CStudentView diagnostics
#ifdef _DEBUG
void CStudentView::AssertValid() const
{
CRecordView::AssertValid();
}
void CStudentView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
CStudentDoc* CStudentView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CStudentDoc)));
return (CStudentDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CStudentView database support
CRecordset* CStudentView::OnGetRecordset()
{
return m_pSet;
}
/////////////////////////////////////////////////////////////////////////////
// CStudentView message handlers
void CStudentView::OnAdd()
{
// TODO: Add your control notification handler code here
// 如果不能打开记录集,则返回
if(!m_pSet->IsOpen()) return ;
if(!m_pSet->CanAppend()) return ; // 没有设置任何字段的值。
m_pSet->AddNew();//增加新记录。
UpdateData(TRUE);//保存新增加的记录。
if(m_pSet->CanUpdate()) m_pSet->Update();
m_pSet->Requery();//刷新记录集。
m_pSet->MoveLast();//移动到新记录。
//更新视图。
UpdateData(FALSE);
}
void CStudentView::OnDelete()
{
// TODO: Add your control notification handler code here
//TODO:Add your command handler code here
//确定是否真的删除记录。
if(MessageBox("真要删除此记录吗?","删除记录?",
MB_YESNO | MB_ICONQUESTION) == IDYES)
{
//删除当前记录。
m_pSet->Delete();
//移动到上一个记录。
m_pSet->MovePrev();
if(m_pSet->IsBOF()) m_pSet->MoveLast();
if(m_pSet->IsEOF()) m_pSet->SetFieldNull(NULL);
//更新视图。
UpdateData(FALSE);
}
}
void CStudentView::OnModify()
{
// TODO: Add your control notification handler code here
m_pSet->Edit();// 允许编辑当前记录。
if(m_pSet->CanUpdate())
{
m_pSet->Update();
}
}
void CStudentView::OnFind()
{
// TODO: Add your control notification handler code here
CDBVariant varBookmark; //用于存放标签的变量。
BOOL lFound = FALSE; //使用该变量标识我们是否
//已经找到了符合条件的记录。
//得到对话条的文本框中的字符串。
CFindDlg dlg;
if(dlg.DoModal() != IDOK) return;
if(m_pSet->CanBookmark())
m_pSet->GetBookmark(varBookmark);
//移动到记录集中第一条记录
m_pSet->MoveFirst();
//搜索记录集中的记录,判断是否有复合条件的记录。
while(!m_pSet->IsEOF() ^ lFound)
{
//检查字段的值是否满足条件。
if(m_pSet->m_Name == dlg.m_sName)
lFound = TRUE;
//移动到下一个记录
else
m_pSet->MoveNext();
}
if(!lFound)
{
//未发现相应记录则显示错误信息。
MessageBox("未发现该记录!","数据库错误",
MB_OK | MB_ICONERROR) ;
if(m_pSet->CanBookmark())
//如果数据库支持书签的话
//则移动到到原来的记录。
m_pSet->SetBookmark(varBookmark);
else
//否则,移动到第一条记录。
m_pSet->MoveFirst();
}
else
{
//显示符合查找条件的记录。
UpdateData(FALSE);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -