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

📄 studentdlg.cpp

📁 小型学生管理系统
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void CStudentDlg::OnButtonModify() 
{
	// TODO: Add your control notification handler code here
	int iItemSelected = m_lstStudent.GetSelectionMark();
	if(iItemSelected<0)
	{
		CString strOnDeleteNoChoice,strDeleteTitle;
		strOnDeleteNoChoice.LoadString(IDS_MODIFY_NO_CHOICE);
		strDeleteTitle.LoadString(IDS_MSGBOX_MODIFY_TITLE);
		MessageBox(strOnDeleteNoChoice,strDeleteTitle,MB_ICONINFORMATION|MB_OK);
		return;
	}

	Student m_UpdateBefore;
	char strTmp[41];
	CStudentChangeDlg dlg(m_lstStudent,iItemSelected);
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{			
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
		m_lstStudent.GetItemText(iItemSelected,0,strTmp,10);
		m_UpdateBefore.m_Stu_No = strTmp;
		m_lstStudent.GetItemText(iItemSelected,1,strTmp,30);
		m_UpdateBefore.m_Stu_Name = strTmp;
		m_lstStudent.GetItemText(iItemSelected,2,strTmp,40);
		m_UpdateBefore.m_Stu_Email = strTmp;
		m_lstStudent.GetItemText(iItemSelected,3,strTmp,40);
		m_UpdateBefore.m_Stu_Home = strTmp;
		if(!UpdateList(UPDATE_BEFORE,&dlg.m_StuOnRefresh,&m_UpdateBefore))
		{
			CString strErr;
			strErr.LoadString(IDS_ERR_UPDARE);
			MessageBox(_T(strErr),"ERROR",MB_OK|MB_ICONERROR);
			return;
		}
		m_lstStudent.SetItemText(iItemSelected,0,dlg.m_StuOnRefresh.m_Stu_No);
		m_lstStudent.SetItemText(iItemSelected,1,dlg.m_StuOnRefresh.m_Stu_Name);
		m_lstStudent.SetItemText(iItemSelected,2,dlg.m_StuOnRefresh.m_Stu_Email);
		m_lstStudent.SetItemText(iItemSelected,3,dlg.m_StuOnRefresh.m_Stu_Home);
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	
	// TODO: Add your control notification handler code here	
	if(m_lstStudent.GetItemCount()<1){
		m_ButtonModify.EnableWindow(FALSE);
		m_ButtonDelete.EnableWindow(FALSE);
	}
}




void CStudentDlg::OnClickStudentList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if(m_lstStudent.GetSelectedCount()>0){
		if(m_lstStudent.GetSelectedCount()<2)
			m_ButtonModify.EnableWindow(TRUE);
		else
			m_ButtonModify.EnableWindow(FALSE);
		m_ButtonDelete.EnableWindow(TRUE);	
	}
	else
	{
		m_ButtonDelete.EnableWindow(FALSE);	
		m_ButtonModify.EnableWindow(FALSE);
	}
	*pResult = 0;
}

void CStudentDlg::OnRclickStudentList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if(m_lstStudent.GetSelectedCount()>0){
		if(m_lstStudent.GetSelectedCount()<2)
			m_ButtonModify.EnableWindow(TRUE);
		else
			m_ButtonModify.EnableWindow(FALSE);
		m_ButtonDelete.EnableWindow(TRUE);	
	}
	else
	{
		m_ButtonDelete.EnableWindow(FALSE);	
		m_ButtonModify.EnableWindow(FALSE);
	}
	*pResult = 0;
}

void CStudentDlg::OnKeydownStudentList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	if(m_lstStudent.GetSelectedCount()>0){
		if(m_lstStudent.GetSelectedCount()<2)
			m_ButtonModify.EnableWindow(TRUE);
		else
			m_ButtonModify.EnableWindow(FALSE);
		m_ButtonDelete.EnableWindow(TRUE);	
	}
	else
	{
		m_ButtonDelete.EnableWindow(FALSE);	
		m_ButtonModify.EnableWindow(FALSE);
	}
	
	*pResult = 0;
}

void CStudentDlg::OnSetfocusStudentList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if(m_lstStudent.GetSelectedCount()>0){
		if(m_lstStudent.GetSelectedCount()<2)
			m_ButtonModify.EnableWindow(TRUE);
		else
			m_ButtonModify.EnableWindow(FALSE);
		m_ButtonDelete.EnableWindow(TRUE);	
	}
	else
	{
		m_ButtonDelete.EnableWindow(FALSE);	
		m_ButtonModify.EnableWindow(FALSE);
	}
	*pResult = 0;
}

void CStudentDlg::OnKillfocusStudentList(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	if(m_lstStudent.GetSelectedCount()<1){
		m_ButtonModify.EnableWindow(FALSE);
		m_ButtonDelete.EnableWindow(FALSE);
	}
	*pResult = 0;
}


BOOL CAboutDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CString strAboutTitle;
	strAboutTitle.LoadString(IDS_ABOUT_TITLE);
	SetWindowText(strAboutTitle);
	return true;
}

void CStudentDlg::OnButtonRefersh() //重新从数据库读取数据
{
	// TODO: Add your control notification handler code here
	UpdateDatabase();//更新数据库

	m_lstStudent.DeleteAllItems();//删除原来列表中的所有数据
	LVITEM Item;
	int i;
	CString str;
	m_pReadSet->Requery();//更新读取数据库指针
	if(!m_pReadSet->IsEOF()||!m_pReadSet->IsBOF())m_pReadSet->MoveFirst();
	for(i=0;!m_pReadSet->IsEOF();m_pReadSet->MoveNext(),i++){
		//重新将数据库的数据插入列表
		Item.mask = LVIF_IMAGE|LVIF_TEXT;
		Item.iItem = i;
		Item.iSubItem = 0;
		Item.iImage = i;
		str = m_pReadSet->m_Stu.m_Stu_No;
		Item.pszText = str.GetBuffer(str.GetLength());
		m_lstStudent.InsertItem(&Item);
		m_lstStudent.SetItemText(i,1,m_pReadSet->m_Stu.m_Stu_Name);
		m_lstStudent.SetItemText(i,2,m_pReadSet->m_Stu.m_Stu_Email);
		m_lstStudent.SetItemText(i,3,m_pReadSet->m_Stu.m_Stu_Home);
	}
	SetWindowLong(m_lstStudent.m_hWnd,GWL_STYLE,WS_VISIBLE|WS_CHILD|
				WS_BORDER|LVS_REPORT|LVS_EDITLABELS);
}

void CStudentDlg::UpdateDatabase()
{
	CStudentLinkTag tmpStu;	
	CString strTableName;
	strTableName.LoadString(IDS_TABLE_NAME);
	for(;!m_Stu_List.IsEmpty();m_Stu_List.RemoveHead())
	{
		tmpStu = m_Stu_List.GetHead();
		CString sql;
		CStudentLinkTag tmpStu2;
		switch(tmpStu.enmStuState)
		{
		case ADD_STU:
			sql.Format("INSERT INTO %s VALUES ('%s','%s','%s','%s');",
				strTableName,tmpStu.m_Stu.m_Stu_No,tmpStu.m_Stu.m_Stu_Name,tmpStu.m_Stu.m_Stu_Email,tmpStu.m_Stu.m_Stu_Home);
			m_dbStudent.Execute(sql);
			break;
		case DELETE_STU:
			sql.Format("DELETE FROM %s WHERE 学号 = '%s'",strTableName,tmpStu.m_Stu.m_Stu_No);
			m_dbStudent.Execute(sql);
			break;
		case UPDATE_BEFORE:
			m_Stu_List.RemoveHead();
			tmpStu2 = m_Stu_List.GetHead();
			if(tmpStu2.enmStuState != UPDATE_AFTER){
				AfxMessageBox("error");
			}
			sql.Format("UPDATE %s SET 学号 = '%s',姓名 = '%s',E_Mail地址 = '%s',籍贯 = '%s' WHERE 学号 = '%s' ",
				strTableName,tmpStu2.m_Stu.m_Stu_No,tmpStu2.m_Stu.m_Stu_Name,tmpStu2.m_Stu.m_Stu_Email,tmpStu2.m_Stu.m_Stu_Home,tmpStu.m_Stu.m_Stu_No);
			m_dbStudent.Execute(sql);
			break;
		default:
			AfxMessageBox("error");
			break;
		}
	}
	m_ButtonModify.EnableWindow(FALSE);
	m_ButtonDelete.EnableWindow(FALSE);
}

BOOL CStudentDlg::UpdateList(StuListSatate enmLS,Student *m_StuOnRefresh,Student *sUpdateBefore /* = NULL */)
//更新链表操作
{
	CStudentLinkTag m_tmpSL;
	StuListSatate enmtmpSLS;
	POSITION pstmp;
	POSITION pUpBefore;
	m_tmpSL.enmStuState = enmLS;
	if(sUpdateBefore == NULL)
		m_tmpSL.m_Stu = *m_StuOnRefresh;
	else
		m_tmpSL.m_Stu = *sUpdateBefore;
	switch(enmLS)
	{
	case ADD_STU://如果是添加元组
		if(pstmp = m_Stu_List.Find(m_tmpSL))//如果链表中存在相同学号的元组
		{
			enmtmpSLS = m_Stu_List.GetAt(pstmp).enmStuState;//获得该元组的状态
			if (enmtmpSLS == DELETE_STU)//如果欲添加的元组与前不久删除的元组同学号
			{	//则将该元组改为更新前状态,而刚刚添加的元组改为更新后状态
				m_tmpSL.enmStuState = UPDATE_AFTER;
				m_Stu_List.InsertAfter(pstmp,m_tmpSL);
				m_tmpSL = m_Stu_List.GetAt(pstmp);
				m_tmpSL.enmStuState = UPDATE_BEFORE;
				m_Stu_List.SetAt(pstmp,m_tmpSL);
				return TRUE;
			}
			else if (enmtmpSLS == UPDATE_BEFORE)//如果欲添加的元组与更新前的某一元组同学号
				//则将此次添加作为上次更新的结果,原来的更新结果作为新添加的元组
			{
				m_tmpSL.enmStuState = UPDATE_AFTER;
				m_Stu_List.InsertAfter(pstmp,m_tmpSL);
				m_Stu_List.GetNext(pstmp);//pstmp++
				m_Stu_List.GetNext(pstmp);//pstmp++
				m_tmpSL = m_Stu_List.GetAt(pstmp);
				m_tmpSL.enmStuState = ADD_STU;
				m_Stu_List.SetAt(pstmp,m_tmpSL);
				return TRUE;
			}
			return FALSE;//否则返回添加出错
		}
		else //if(pstmp = m_Stu_List.Find(m_tmpSL))
			m_Stu_List.AddTail(m_tmpSL);//否则直接插入链表尾
		return TRUE;
	case DELETE_STU: //删除元组
		if(pstmp = m_Stu_List.Find(m_tmpSL))//如果链表中存在相同学号的元组
		{
			enmtmpSLS = m_Stu_List.GetAt(pstmp).enmStuState;
			if (enmtmpSLS == ADD_STU)
			{
				m_Stu_List.RemoveAt(pstmp);
				return TRUE;
			}
			else if (enmtmpSLS == UPDATE_AFTER)
			{
				m_Stu_List.RemoveAt(pstmp);
				m_Stu_List.GetPrev(pstmp);//pstmp--
				m_tmpSL = m_Stu_List.GetAt(pstmp);
				m_tmpSL.enmStuState = DELETE_STU;
				m_Stu_List.SetAt(pstmp,m_tmpSL);
				return TRUE;
			}
			else if (enmtmpSLS == UPDATE_BEFORE)
			{
				m_Stu_List.GetNext(pstmp);//pstmp++
				CStudentLinkTag m_tmpSL2 = m_tmpSL;
				m_tmpSL = m_Stu_List.GetAt(pstmp);
				if(m_tmpSL.enmStuState != UPDATE_AFTER || m_tmpSL != m_tmpSL2)
				{
					AfxMessageBox("ERROR");
					return FALSE;
				}

				m_Stu_List.RemoveAt(pstmp);
				m_tmpSL.enmStuState = DELETE_STU;
				m_Stu_List.GetPrev(pstmp);//pstmp--
				m_Stu_List.SetAt(pstmp,m_tmpSL);
				return TRUE;
			}
			return FALSE;
		}
		m_Stu_List.AddTail(m_tmpSL);
		return TRUE;
	case UPDATE_BEFORE:		
		if(pstmp = m_Stu_List.Find(m_tmpSL))
		{//如果链表中存在与修改前相同的数据项
			enmtmpSLS = m_Stu_List.GetAt(pstmp).enmStuState;
			if (enmtmpSLS == UPDATE_BEFORE)
			{
				CStudentLinkTag m_tmpSL2 = m_tmpSL;
				m_Stu_List.GetNext(pstmp);//pstmp++
				m_tmpSL = m_Stu_List.GetAt(pstmp);
				if(m_tmpSL.enmStuState!=UPDATE_AFTER || m_tmpSL != m_tmpSL2)
				{
					AfxMessageBox("ERROR");
					return FALSE;
				}

				m_tmpSL.m_Stu = *m_StuOnRefresh;

				pUpBefore = pstmp;//记录原有删除记录
				m_Stu_List.GetPrev(pstmp);//pstmp--
				m_Stu_List.RemoveAt(pUpBefore);//删除原有的修改记录
				pUpBefore = pstmp;//记录欲删除的记录
				goto UP_AF;
			}
			else if (enmtmpSLS == UPDATE_AFTER)
			{
				m_tmpSL.m_Stu = *m_StuOnRefresh;
				pUpBefore = pstmp;//记录原有删除记录
				m_Stu_List.GetPrev(pstmp);//pstmp--
				m_Stu_List.RemoveAt(pUpBefore);//删除原有的修改记录
				pUpBefore = pstmp;//记录欲删除的记录
				goto UP_AF;
			}
			else if(enmtmpSLS == ADD_STU)
			{
				m_tmpSL.m_Stu = *m_StuOnRefresh;
				m_Stu_List.RemoveAt(pstmp);//删除原来添加的项目
				pUpBefore = NULL;
				goto UP_AF;
			}
			return FALSE;
		}
		m_Stu_List.AddTail(m_tmpSL);
		pUpBefore = m_Stu_List.GetTailPosition();
		m_tmpSL.m_Stu = *m_StuOnRefresh;

UP_AF:	if(pUpBefore != NULL && m_Stu_List.GetAt(pUpBefore) == m_tmpSL)
		{
			m_tmpSL.enmStuState = UPDATE_AFTER;
			m_Stu_List.InsertAfter(pUpBefore,m_tmpSL);//如果修改没有改变学号,直接插入修改后的项
			return TRUE;
		}
		if(pstmp = m_Stu_List.Find(m_tmpSL))
		{//如果链表中存在于修改后数据相同的项
			enmtmpSLS = m_Stu_List.GetAt(pstmp).enmStuState;
			if (enmtmpSLS == DELETE_STU)
			{
				m_tmpSL.enmStuState = UPDATE_AFTER;
				m_Stu_List.InsertAfter(pstmp,m_tmpSL);
				m_tmpSL = m_Stu_List.GetAt(pstmp);
				m_tmpSL.enmStuState = UPDATE_BEFORE;
				m_Stu_List.SetAt(pstmp,m_tmpSL);
				if(pUpBefore != NULL)//将原来的修改改为删除
				{
					m_tmpSL = m_Stu_List.GetAt(pUpBefore);
					m_tmpSL.enmStuState = DELETE_STU;
					m_Stu_List.SetAt(pUpBefore,m_tmpSL);
				}
				//else
					//否则无需添加新的项目
				return TRUE;
			}
			else if (enmtmpSLS == UPDATE_BEFORE)//交换修改
			{
				m_tmpSL.enmStuState = UPDATE_AFTER;
				m_Stu_List.GetNext(pstmp);//pstmp++
				CStudentLinkTag m_tmpSLT = m_Stu_List.GetAt(pstmp);//保存原来的修改
				m_Stu_List.SetAt(pstmp,m_tmpSL);//写入新的修改
				if(pUpBefore == NULL)
				{
					m_tmpSL.enmStuState = ADD_STU;//将原来的修改作为新元素插入
					m_Stu_List.AddTail(m_tmpSL);
				}
				else
				{
					m_Stu_List.InsertAfter(pUpBefore,m_tmpSLT);//插入新的修改作为添加元素
				}
				return TRUE;
			}
			return FALSE;//返回出错
		}
		else//if(pstmp = m_Stu_List.Find(m_tmpSL))
		{//否则
			if (pUpBefore == NULL)
			{
				m_tmpSL.enmStuState = ADD_STU;
				m_Stu_List.AddTail(m_tmpSL);//插入为新元素
			}
			else
			{
				m_tmpSL.enmStuState = UPDATE_AFTER;
				m_Stu_List.InsertAfter(pUpBefore,m_tmpSL);//直接插入修改
			}
			return TRUE;
		}
	default: return FALSE;
	}
}

⌨️ 快捷键说明

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