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

📄 scoredlg.cpp

📁 VC大作业
💻 CPP
字号:
// ScoreDlg.cpp : implementation file
//

#include "stdafx.h"
#include "member.h"
#include "ScoreDlg.h"
#include "ScoreReSet.h"
#include "MemberView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CScoreDlg dialog


CScoreDlg::CScoreDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CScoreDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CScoreDlg)
	m_CourseID = _T("");
	m_CourseName = _T("");
	m_CourseScore = 0;
	m_Score = 0;
	//}}AFX_DATA_INIT
}


void CScoreDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CScoreDlg)
	DDX_Control(pDX, IDC_SCORE_ADD, m_ScoreAdd);
	DDX_Control(pDX, IDC_LIST_SCORE, m_list);
	DDX_Text(pDX, IDC_COURSE_ID, m_CourseID);
	DDX_Text(pDX, IDC_COURSE_NAME, m_CourseName);
	DDX_Text(pDX, IDC_COURSE_SCORE, m_CourseScore);
	DDX_Text(pDX, IDC_SCORE, m_Score);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CScoreDlg, CDialog)
	//{{AFX_MSG_MAP(CScoreDlg)
	ON_BN_CLICKED(IDC_SCORE_ADD, OnScoreAdd)
	ON_BN_CLICKED(IDC_SCORE_EDIT, OnScoreEdit)
	ON_NOTIFY(NM_CLICK, IDC_LIST_SCORE, OnClickListScore)
	ON_BN_CLICKED(IDC_SCORE_DEL, OnScoreDel)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CScoreDlg message handlers

void CScoreDlg::ListScore()
{

	try
	{
	//删除CList的所有内容
	m_list.DeleteAllItems();
	if(m_ScoreReSet.IsOpen())
		m_ScoreReSet.Close();
	//打开记录集
    m_ScoreReSet.Open(CRecordset::snapshot,m_strPosition,CRecordset::none);
   //将记录集显示到 CList中
	if(m_ScoreReSet.IsOpen()&&!m_ScoreReSet.IsEOF())
	{
		for(int i=0;i<m_ScoreReSet.GetRecordCount();i++)
		{
		CString temp;
		m_ScoreReSet.GetFieldValue("MemID",temp);
		m_list.InsertItem(i,temp);
		m_ScoreReSet.GetFieldValue("CourseID",temp);
		m_list.SetItemText(i,1,temp);
        m_ScoreReSet.GetFieldValue("CourseName",temp);
		m_list.SetItemText(i,2,temp);
		m_ScoreReSet.GetFieldValue("CourseScore",temp);
		m_list.SetItemText(i,3,temp);
		m_ScoreReSet.GetFieldValue("Score",temp);
		m_list.SetItemText(i,4,temp);
        m_ScoreReSet.MoveNext(); 
		}
	}
	else
	{
		AfxMessageBox("无成绩记录!");
		return;
	}
	   m_ScoreReSet.Close();
	}
	//关闭数据库
	
	catch(CDBException *e)
	{
		e->Delete();
		return;
	}

}

BOOL CScoreDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	//设置表格风格
	m_list.SetExtendedStyle(m_list.GetExtendedStyle()|LVS_EX_GRIDLINES|LVS_EX_FULLROWSELECT);   

    //设置表头
	m_list.InsertColumn(0,"学号",LVCFMT_LEFT,80);
    m_list.InsertColumn(1,"课程编号",LVCFMT_LEFT,80);
    m_list.InsertColumn(2,"课程名称",LVCFMT_LEFT,90);
	m_list.InsertColumn(3,"学分",LVCFMT_LEFT,80);
	m_list.InsertColumn(4,"成绩",LVCFMT_LEFT,80);
	ListScore();
	return TRUE;  
}

void CScoreDlg::AddPosition(CString str)
{
	m_strPosition=str;

}

void CScoreDlg::OnScoreAdd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CScoreReSet m_ScoreReSet;
	try
	{
		if(m_ScoreReSet.IsOpen())
			m_ScoreReSet.Close();
		if(m_CourseID.GetLength()<=0||m_CourseName.GetLength()<=0)
		{
		AfxMessageBox("课程编号、名称不能为空");
	    	return;
		}
		CString strSQL;
		strSQL.Format("%s and CourseID='%s'",m_strPosition,m_CourseID);
	//	AfxMessageBox(strSQL);
		m_ScoreReSet.Open(CRecordset::snapshot,strSQL,CRecordset::none);
        if(!m_ScoreReSet.IsEOF())
		{
			m_ScoreReSet.Close();
			AfxMessageBox("成绩已存在!");
			return;
		}
		int n=m_strPosition.Find("MemID=");
		int m=m_strPosition.GetLength();
		CString MemID1=m_strPosition.Right(m-n-7);
		int i=MemID1.GetLength();
		CString MemID=MemID1.Left(i-1);
		m_ScoreReSet.AddNew();
		m_ScoreReSet.m_MemID=MemID;
		m_ScoreReSet.m_CourseID=m_CourseID;
		m_ScoreReSet.m_CourseName=m_CourseName;
		m_ScoreReSet.m_CourseScore=m_CourseScore;
		m_ScoreReSet.m_Score=m_Score;
		if(m_ScoreReSet.CanUpdate())
			m_ScoreReSet.Update();
	    m_ScoreReSet.Close();
	    AfxMessageBox("添加成功!");
	}
	catch(CDBException *e)
	{
		e->Delete();
		return;
	}
    //刷新全部信息
	ListScore();	
}

void CScoreDlg::OnScoreEdit() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CScoreReSet m_ScoreReSet;
	try
	{
		if(m_ScoreReSet.IsOpen())
			m_ScoreReSet.Close();
		if(m_CourseName.GetLength()<=0)
		{
	    	AfxMessageBox("课程编号、名称不能为空");
	    	return;
		}
		CString strSQL;
		strSQL.Format("%s and CourseID='%s'",m_strPosition,m_CourseID);
	//	AfxMessageBox(strSQL);
		m_ScoreReSet.Open(CRecordset::snapshot,strSQL,CRecordset::none);
        if(m_ScoreReSet.IsOpen()&&!m_ScoreReSet.IsEOF())
		{	
			//设置编辑当前记录
			m_ScoreReSet.Edit();
			//开始编辑该条记录的内容
			m_ScoreReSet.m_CourseName=m_CourseName;
	    	m_ScoreReSet.m_CourseScore=m_CourseScore;
	    	m_ScoreReSet.m_Score=m_Score;
		    if(m_ScoreReSet.CanUpdate())
			{
				m_ScoreReSet.Update();			
			}
			//更新完毕,关闭数据库
			m_ScoreReSet.Close();
			AfxMessageBox("修改成功!");
		}
		else
		{	
			//关闭数据库
			m_ScoreReSet.Close();
			//提示用户
			AfxMessageBox("该课程不存在,无法修改!");
			return;
		}
	}
	catch(CDBException *e)
	{
		e->Delete();
		return;
	}
    //刷新全部信息
	ListScore();	
}

void CScoreDlg::ScoreShow()
{
	int row=m_list.GetSelectionMark();
	CString position=m_list.GetItemText(row,1);
//	AfxMessageBox(position);
	try
	{
		if(m_ScoreReSet.IsOpen())
			m_ScoreReSet.Close();
		CString strSQL;
		strSQL.Format("%s and CourseID='%s'",m_strPosition,position);
		m_ScoreReSet.Open(CRecordset::snapshot,strSQL,CRecordset::none);
		m_CourseID=m_ScoreReSet.m_CourseID;
		m_CourseName=m_ScoreReSet.m_CourseName;
		m_CourseScore=m_ScoreReSet.m_CourseScore;
		m_Score=m_ScoreReSet.m_Score;
		UpdateData(FALSE);
	}
	catch(CDBException *e)
	{
		e->Delete();
		return;
	}
}

void CScoreDlg::OnClickListScore(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	ScoreShow();
	*pResult = 0;
}

void CScoreDlg::OnScoreDel() 
{
	// TODO: Add your control notification handler code here
	int row=m_list.GetSelectionMark();
	CString position=m_list.GetItemText(row,1);
	if(position=="")
	{
		AfxMessageBox("请先选择一条信息!");
	    return;
	}
   if(MessageBox("确定删除这条记录?","删除确认",MB_YESNO|MB_ICONQUESTION)==IDYES)
   {
    	try
		{
		if(m_ScoreReSet.IsOpen())
			m_ScoreReSet.Close();
		CString strSQL;
		strSQL.Format("%s and CourseID='%s'",m_strPosition,position);
		m_ScoreReSet.Open(CRecordset::snapshot,strSQL,CRecordset::none);
		m_ScoreReSet.Delete();
		}
	   catch(CDBException *e)
	   {
		e->Delete();
		return;
	   }

   }
   ListScore();
   ScoreShow();
}

⌨️ 快捷键说明

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