📄 moreview.cpp
字号:
// MoreView.cpp : implementation file
//
#include "stdafx.h"
#include "TestApp.h"
#include "MoreView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMoreView
IMPLEMENT_DYNCREATE(CMoreView, CRecordView)
CMoreView::CMoreView()
: CRecordView(CMoreView::IDD)
{
//{{AFX_DATA_INIT(CMoreView)
m_pSet = NULL;
m_recnews = _T("");
m_recno=0; //初始化当前记录号
m_reccount=0; //初始化记录总数
//}}AFX_DATA_INIT
}
CMoreView::~CMoreView()
{ //启用显示多选题数据管理窗口菜单命令
((CTestAppApp*)AfxGetApp())->m_MoreViews=0;
if (m_pSet)
delete m_pSet;//删除访问More表的记录集指针
if (m_pSetC)
delete m_pSetC;//删除访问Chapter表的记录集指针
}
void CMoreView::DoDataExchange(CDataExchange* pDX)
{
CRecordView::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMoreView)
DDX_Control(pDX, IDC_COMBO_CHAPTER, m_chaptername);
DDX_Control(pDX, IDC_CHECKD, m_checkd);
DDX_Control(pDX, IDC_CHECKB, m_checkb);
DDX_Control(pDX, IDC_CHECKC, m_checkc);
DDX_Control(pDX, IDC_CHECKA, m_checka);
DDX_Control(pDX, IDC_BUTTON_MOVEPREV, m_moveprev);
DDX_Control(pDX, IDC_BUTTON_MOVENEXT, m_movenext);
DDX_Control(pDX, IDC_BUTTON_MOVELAST, m_movelast);
DDX_Control(pDX, IDC_BUTTON_MOVEFIRST, m_movefirst);
DDX_Text(pDX, IDC_EDIT_RECNEWS, m_recnews);
DDX_FieldText(pDX, IDC_EDIT_CONTENT, m_pSet->m_content, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_OPTION1, m_pSet->m_option1, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_OPTION2, m_pSet->m_option2, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_OPTION3, m_pSet->m_option3, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_OPTION4, m_pSet->m_option4, m_pSet);
DDX_FieldText(pDX, IDC_EDIT_SCORE, m_pSet->m_score, m_pSet);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMoreView, CRecordView)
//{{AFX_MSG_MAP(CMoreView)
ON_BN_CLICKED(IDC_SAVERECORD, OnSaverecord)
ON_BN_CLICKED(IDC_DELETERECORD, OnDeleterecord)
ON_BN_CLICKED(IDC_ADDNEWRECORD, OnAddnewrecord)
ON_BN_CLICKED(IDC_BUTTON_MOVEFIRST, OnButtonMovefirst)
ON_BN_CLICKED(IDC_BUTTON_MOVELAST, OnButtonMovelast)
ON_BN_CLICKED(IDC_BUTTON_MOVENEXT, OnButtonMovenext)
ON_BN_CLICKED(IDC_BUTTON_MOVEPREV, OnButtonMoveprev)
ON_CBN_SELENDOK(IDC_COMBO_CHAPTER, OnSelendokComboChapter)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMoreView diagnostics
#ifdef _DEBUG
void CMoreView::AssertValid() const
{
CRecordView::AssertValid();
}
void CMoreView::Dump(CDumpContext& dc) const
{
CRecordView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMoreView message handlers
CRecordset* CMoreView::OnGetRecordset()
{
if (m_pSet != NULL)
return m_pSet;
m_pSet = new CMoreSet(NULL);
m_pSet->Open();
m_pSetC = new CChapterSet(NULL);//创建访问Chapter表的记录集指针
m_pSetC->Open();//打开访问Chapter表的记录集
return m_pSet;
}
CMoreSet* CMoreView::GetRecordset()
{
CMoreSet* pData = (CMoreSet*) OnGetRecordset();
ASSERT(pData == NULL || pData->IsKindOf(RUNTIME_CLASS(CMoreSet)));
return pData;
}
void CMoreView::OnInitialUpdate()
{
BeginWaitCursor();
GetRecordset();
CRecordView::OnInitialUpdate();
GetDocument()->SetTitle("多选题数据管理");//设置文档标题
GetParent()->SetWindowText("多选题数据管理");//设置子窗口标题
if (!m_pSet->IsOpen())
m_pSet->Open();
//创建章节名称下拉列表
if (!m_pSetC->IsOpen())
m_pSetC->Open();
while(!m_pSetC->IsEOF())
{
if(CB_ERR==m_chaptername.FindString(-1,m_pSetC->m_name))
m_chaptername.AddString(m_pSetC->m_name);//不重复,加入列表
m_pSetC->MoveNext();
}
m_pSetC->MoveFirst();
//设置默认筛选条件及章节名称
CString str;
str.Format("%d",m_pSetC->m_id);
m_pSet->m_strFilter="chapterid=" + str;
m_pSet->Requery();
m_chaptername.SelectString(-1,m_pSetC->m_name);
if (!m_pSet->IsEOF())
{ //若记录集不为空,则遍历记录集,
//使GetRecordCount函数返回准确的记录数
while(!m_pSet->IsEOF())
m_pSet->MoveNext();
m_reccount=m_pSet->GetRecordCount();
m_pSet->MoveFirst();
m_recno=1;
}
ShowData();//显示当前记录
EndWaitCursor();
}
void CMoreView::ShowData()
{
if (m_pSet->IsOpen())
{
if (m_recno>1)
{ //当前记录不是第一条记录,启用向前和第一条记录按钮
m_movefirst.EnableWindow(true);
m_moveprev.EnableWindow(true);
}
else
{ //当前记录是第一条记录,则禁用向前和第一条记录按钮
m_movefirst.EnableWindow(false);
m_moveprev.EnableWindow(false);
}
if (m_recno==m_reccount)
{ //当前记录是最后一条记录,禁用向后和最后一条记录按钮
m_movenext.EnableWindow(false);
m_movelast.EnableWindow(false);
}
else
{ if(m_recno>=0)
{ //若当前记录不是最后一条记录,则启用向后和最后一条记录按钮
m_movenext.EnableWindow(true);
m_movelast.EnableWindow(true);
}
}
CString str;
str=m_pSet->m_answer;
if ('1'==str.GetAt(0))
m_checka.SetCheck(1);
else
m_checka.SetCheck(0);
if ('1'==str.GetAt(1))
m_checkb.SetCheck(1);
else
m_checkb.SetCheck(0);
if ('1'==str.GetAt(2))
m_checkc.SetCheck(1);
else
m_checkc.SetCheck(0);
if ('1'==str.GetAt(3))
m_checkd.SetCheck(1);
else
m_checkd.SetCheck(0);
//显示当前记录号和记录总数
str.Format("%d/%d",m_recno,m_reccount);
m_recnews=str; //设置记录信息成员变量
UpdateData(false); //将成员变量数据传递到窗口
}
}
void CMoreView::OnSaverecord()
{
try
{
if (m_recno>0)
{ //若有当前记录,则执行保存操作
m_pSet->Edit();//使当前记录进入编辑状态
UpdateData(true);//用窗体数据更新记录集绑定字段
//根据章节名称确定章节编号
CString str;
m_chaptername.GetWindowText(str);
m_pSetC->m_strFilter="name='" + str + "'";
m_pSetC->Requery();
m_pSet->m_chapterid=m_pSetC->m_id;//设置chapterid字段值
if (m_checka.GetCheck()==1)
str="1";
else
str="0";
if (m_checkb.GetCheck()==1)
str+="1";
else
str+="0";
if (m_checkc.GetCheck()==1)
str+="1";
else
str+="0";
if (m_checkd.GetCheck()==1)
str+="1";
else
str+="0";
m_pSet->m_answer=str;
if (m_pSet->CanUpdate()) {
m_pSet->Update();//将记录集更新返回数据库
}
AfxMessageBox("成功保存数据!");
}
}
catch(CDBException* e) {
m_pSet->CancelUpdate();//撤销引起错误的操作
AfxMessageBox(e->m_strError);//显示错误信息
e->Delete();//删除错误对象
return;
}
}
void CMoreView::OnDeleterecord()
{
if (m_recno>0)
{ //若有当前记录,则执行删除操作
try {
m_pSet->Delete();//删除当前记录
}
catch(CDBException* e) {
m_pSet->CancelUpdate();//撤销引起错误的操作
//如果删除操作发生错误,则显示错误信息
AfxMessageBox(e->m_strError);//显示错误信息
e->Delete();//删除错误对象
m_pSet->MoveFirst(); //使第一条记录成为当前记录
m_recno=1;
ShowData();//刷新窗口
return;
}
if (m_recno==m_reccount) {
//如果删除的是最后一条记录,则使前一条记录成为当前记录
m_pSet->MovePrev();
m_recno--;
}
else {
m_pSet->MoveNext();//使下一条记录成为当前记录
}
m_reccount--;
ShowData();//显示新的当前记录数据
AfxMessageBox("成功删除记录!");
}
}
void CMoreView::OnAddnewrecord()
{
try{
m_pSet->AddNew();
UpdateData(true);//用窗体数据更新记录集绑定字段
//根据章节名称确定章节编号
CString str;
m_chaptername.GetWindowText(str);
m_pSetC->m_strFilter="name='" + str + "'";
m_pSetC->Requery();
m_pSet->m_chapterid=m_pSetC->m_id;//设置chapterid字段值
if (m_checka.GetCheck()==1)
str="1";
else
str="0";
if (m_checkb.GetCheck()==1)
str+="1";
else
str+="0";
if (m_checkc.GetCheck()==1)
str+="1";
else
str+="0";
if (m_checkd.GetCheck()==1)
str+="1";
else
str+="0";
m_pSet->m_answer=str;
if (m_pSet->CanUpdate()) {
m_pSet->Update();//将记录集更新返回数据库
m_reccount++;
}
m_recno=m_reccount;
m_pSet->MoveLast();
ShowData();
AfxMessageBox("成功添加记录!");
}
catch(CDBException* e) {
m_pSet->CancelUpdate();//撤销引起错误的操作
AfxMessageBox(e->m_strError);//显示错误信息
e->Delete();//删除错误对象
return;
}
}
void CMoreView::OnButtonMovefirst()
{ //使第一个记录成为当前记录
if(m_recno>1)
{
m_pSet->MoveFirst();
m_recno=1;
ShowData(); //显示当前记录数据
}
}
void CMoreView::OnButtonMovelast()
{ //使最后一个记录成为当前记录
if(m_recno<m_reccount)
{
m_pSet->MoveLast();
m_recno=m_reccount;
ShowData(); //显示当前记录数据
}
}
void CMoreView::OnButtonMovenext()
{ //使下一个记录成为当前记录
if (m_recno<m_reccount)
{
m_pSet->MoveNext();
m_recno++;
ShowData();//显示当前记录数据
}
}
void CMoreView::OnButtonMoveprev()
{ //使前一个记录成为当前记录
if (m_recno>1)
{
m_pSet->MovePrev();
m_recno--;
ShowData();//显示当前记录数据
}
}
void CMoreView::OnSelendokComboChapter()
{ //使用选定章节的编号筛选判断题记录集
CString str;
m_chaptername.GetWindowText(str);//获得章节名称
m_pSetC->m_strFilter="name='" + str + "'";//设置章节记录集筛选条件
m_pSetC->Requery();//刷新记录集
str.Format("%d",m_pSetC->m_id);//获得章节编号
m_pSet->m_strFilter="chapterid=" + str ;//用获得章节编号筛选判断题记录集
m_pSet->Requery();//刷新记录集
if (!m_pSet->IsEOF() && !m_pSet->IsBOF())
{ //若记录集不为空,则遍历记录集,
//使GetRecordCount函数返回准确的记录数
while(!m_pSet->IsEOF())
m_pSet->MoveNext();
m_reccount=m_pSet->GetRecordCount();
m_pSet->MoveFirst();
m_recno=1;
}else{
m_reccount=0;
m_recno=0;
}
ShowData();//显示当前记录数据
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -