📄 coursedlg.cpp
字号:
// CourseDlg.cpp : implementation file
//
#include "stdafx.h"
#include "FPSys.h"
#include "CourseDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCourseDlg dialog
extern CDatabase m_db;
extern int OpenFrm;
extern int FromNameToID(CString tabName,CString attr,CString name); //通过名字获得编号
extern CString FromIDToName(CString tabName,CString attr,int name);
extern CString Weedday[7];
CString strDay[7]={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
CString strClassroom; //用于去时传递数据,也用于回来时接受数据
CString strCourse; //作用同 m_strClassroom 变量
CString strSchedule; //时间段
CString TheWeekday; //星期几
BOOL bHaveMod;
CCourseDlg::CCourseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCourseDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCourseDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CCourseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCourseDlg)
DDX_Control(pDX, IDC_COMBO_CLASS, m_cClass);
DDX_Control(pDX, IDC_COMBO_DEPART, m_cDepart);
DDX_Control(pDX, IDC_MSFLEXGRID1, m_cMsCourse);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCourseDlg, CDialog)
//{{AFX_MSG_MAP(CCourseDlg)
ON_CBN_SELCHANGE(IDC_COMBO_DEPART, OnSelchangeComboDepart)
ON_CBN_SELCHANGE(IDC_COMBO_CLASS, OnSelchangeComboClass)
ON_BN_CLICKED(IDC_BTN_DELCOURSE, OnBtnDelcourse)
ON_BN_CLICKED(IDC_BTN_MOD, OnBtnMod)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCourseDlg message handlers
BEGIN_EVENTSINK_MAP(CCourseDlg, CDialog)
//{{AFX_EVENTSINK_MAP(CCourseDlg)
ON_EVENT(CCourseDlg, IDC_MSFLEXGRID1, -600 /* Click */, OnClickMsflexgrid1, VTS_NONE)
//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()
void CCourseDlg::OnSelchangeComboDepart()
{
// TODO: Add your control notification handler code here
m_cClass.Clear();
int iCount=m_cClass.GetCount();
for(int i=0;i<iCount;i++)
{
m_cClass.DeleteString(0); //每删除一个,下一个就又变为第一个
}
CString departName;
int departId;
m_cDepart.GetLBText(m_cDepart.GetCurSel(),departName);
CString sql;
if(strcmp(departName,"所有系别")==0)
sql.Format("select *from tab_Class");
else
{
departId=FromNameToID("tab_Depart","DepartName",departName);
sql.Format("select *from tab_Class where DepartID=%d",departId);
}
TRY{
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,sql);
CString className;
while(!rs.IsEOF())
{
rs.GetFieldValue(1,className);
m_cClass.AddString(className);
rs.MoveNext();
}
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
void CCourseDlg::OnSelchangeComboClass()
{
// TODO: Add your control notification handler code here
CString ClassName;
m_cClass.GetLBText(m_cClass.GetCurSel(),ClassName);
m_cMsCourse.Clear();
InitListCtrl();
FromClassNameToCourseTab(ClassName);
}
void CCourseDlg::OnClickMsflexgrid1()
{
// TODO: Add your control notification handler code here
//获得当前行当前列,用于回来时信息的定位
m_iRow=m_cMsCourse.GetRow(); //获得第几行,即知道是星期几
m_iCol=m_cMsCourse.GetCol(); //获得第几列,即知道是在哪个时间段
//在这里获得当前课程,课室因为要访问数据库,可在修改按钮的时个获得
strCourse=m_cMsCourse.GetTextMatrix(1,2); //获得课程名
long row,col;
row=m_cMsCourse.GetRow();
col=m_cMsCourse.GetCol();
if(-1==m_cClass.GetCurSel())
return;
// GetDlgItem(IDC_BTN_MOD)->EnableWindow(TRUE);
int ClassId;
CString strCourse,ClassName;
//由班级名称得到班级ID,由班级ID及星期,时段,得到课室ID,由课室ID得到课室名称
m_cClass.GetLBText(m_cClass.GetCurSel(),ClassName);
ClassId=FromNameToID("tab_Class","ClassName",ClassName);
CString sql;
sql.Format("select ClassroomID from tab_Coursetab where ClassID=%d and ScheduleID=%d"
" and TheWeekday=%d",ClassId,col,row);
TRY{
CString Classroom;
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,sql);
if(!rs.IsEOF())
{
int ClassroomId;
CDBVariant var;
rs.GetFieldValue((short)0,var,SQL_C_SLONG);
if(var.m_dwType!=DBVT_NULL)
ClassroomId=var.m_iVal;
var.Clear();
Classroom=FromIDToName("tab_Classroom","ClassroomID",ClassroomId);
}
strCourse.Format("[%s,%s]:%s\r\n[上 课 地 点]:%s",strDay[row-1],
m_cMsCourse.GetTextArray(col),m_cMsCourse.GetTextMatrix(row,col),Classroom);
GetDlgItem(IDC_STATIC_ALL)->SetWindowText(strCourse);
UpdateData(FALSE);
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
void CCourseDlg::OnBtnDelcourse()
{
// TODO: Add your control notification handler code here
//要删除比较找到三个条件,即:星期,时间段ID,课室ID
//现在由 m_iRow 可知道星期,由 m_iCol 可知道时间段 ID,所以主要要求班级ID
int ClassId;
CString ClassName;
m_cClass.GetLBText(m_cClass.GetCurSel(),ClassName);
ClassId=FromNameToID("tab_Class","ClassName",ClassName);
TRY{
CString sql;
sql.Format("delete from tab_Coursetab where ClassID=%d and ScheduleID=%d and TheWeekday=%d",
ClassId,m_iCol,m_iRow);
TRACE(sql);
m_db.ExecuteSQL(sql);
OnSelchangeComboClass();
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
void CCourseDlg::OnBtnMod()
{
// TODO: Add your control notification handler code here
if(-1==m_cClass.GetCurSel())
{
MessageBox("请选择班级!","提示");
return;
}
if(strcmp(strCourse,"")!=0)
{
int iCourseId,iClassId;
iCourseId=FromNameToID("tab_Course","CourseName",strCourse);
CString sql;
sql.Format("select ClassroomID from tab_Coursetab where CourseID=%d",iCourseId);
TRY{
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,sql);
if(!rs.IsEOF())
{
CDBVariant var;
rs.GetFieldValue((short)0,var,SQL_C_SLONG);
if(var.m_dwType!=DBVT_NULL)
iClassId=var.m_lVal;
var.Clear();
strClassroom=FromIDToName("tab_Classroom","ClassroomID",iClassId);
}
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
CEditCourseDlg dlg;
dlg.DoModal();
if(bHaveMod)
{
m_cMsCourse.SetTextMatrix(m_iRow,m_iCol,strCourse);
//下面获得各个的名字,并把他们存入数据库中
CString tmpClassName,tmpSchedule,tmpClassroom,tmpCourse;
int tmpWeekday;
m_cClass.GetLBText(m_cClass.GetCurSel(),tmpClassName);
tmpSchedule=m_cMsCourse.GetTextArray(m_iCol);
tmpWeekday=m_iRow;
tmpClassroom=strClassroom;
tmpCourse=strCourse;
InsertToDB(tmpClassName,tmpSchedule,tmpWeekday,tmpClassroom,tmpCourse);
}
}
BOOL CCourseDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
InitListCtrl();
InitCtlrData();
if(OpenFrm==2)
{
int i=0;
while(i<m_cClass.GetCount())
{
CString tmp;
m_cClass.GetLBText(i,tmp);
if(strcmp(tmpClassName,"所有班级")==0 || strcmp(tmpClassName,tmp)==0)
break;
i++;
}
m_cClass.SetCurSel(i);
OnSelchangeComboClass();
}
// GetDlgItem(IDC_BTN_MOD)->EnableWindow(false);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CCourseDlg::InitListCtrl()
{
long nNewValue;
// m_cMsCourse.SetColWidth(0,1000);
/* m_cMsCourse.SetColWidth(2,nNewValue);
m_cMsCourse.SetColWidth(3,1000);
m_cMsCourse.SetColWidth(4,nNewValue);
m_cMsCourse.SetColWidth(5,nNewValue);*/
nNewValue=500;
m_cMsCourse.SetRowHeight(1,nNewValue);
m_cMsCourse.SetRowHeight(2,nNewValue);
m_cMsCourse.SetRowHeight(3,nNewValue);
m_cMsCourse.SetRowHeight(4,nNewValue);
m_cMsCourse.SetRowHeight(5,nNewValue);
m_cMsCourse.SetRowHeight(6,nNewValue);
m_cMsCourse.SetRowHeight(7,nNewValue);
TRY{
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,"select *from tab_Schedule order by ScheduleID");
while(!rs.IsEOF())
rs.MoveNext();
int iCount=rs.GetRecordCount();
/* CString temp;
temp.Format("%d",i);
MessageBox(temp);
return;*/
m_cMsCourse.SetCols(iCount+1);
int i=1;
CString name;
rs.MoveFirst();
while(!rs.IsEOF())
{
int id;
CDBVariant var;
rs.GetFieldValue((short)0,var,SQL_C_SLONG);
if(var.m_dwType!=DBVT_NULL)
id=var.m_iVal;
var.Clear();
rs.GetFieldValue(1,name);
m_cMsCourse.SetTextArray(i,name);
m_cMsCourse.SetColWidth(i,7800/iCount);
i++;
rs.MoveNext();
}
m_cMsCourse.SetCellAlignment(4);
m_cMsCourse.SetTextArray(i,"星期一");
m_cMsCourse.SetTextArray(2*i,"星期二");
m_cMsCourse.SetTextArray(3*i,"星期三");
m_cMsCourse.SetTextArray(4*i,"星期四");
m_cMsCourse.SetTextArray(5*i,"星期五");
m_cMsCourse.SetTextArray(6*i,"星期六");
m_cMsCourse.SetTextArray(7*i,"星期日");
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
void CCourseDlg::FromClassNameToCourseTab(CString ClassName)
{
int ClassId;
ClassId=FromNameToID("tab_Class","ClassName",ClassName);
CString sql;
sql.Format("select *from tab_Coursetab where ClassID=%d",ClassId);
TRY{
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,sql);
if(rs.IsEOF())
{
m_cMsCourse.Clear();
InitListCtrl();
return;
}
while(!rs.IsEOF())
{
int iScheduleId,iWeekday,iClassroomId,iCourseId;
CDBVariant var;
rs.GetFieldValue((short)1,var,SQL_C_SLONG);
if(var.m_dwType!=DBVT_NULL)
iScheduleId=var.m_iVal;
var.Clear();
rs.GetFieldValue(2,var,SQL_C_SLONG);
if(var.m_dwType!=DBVT_NULL)
iWeekday=var.m_iVal;
var.Clear();
rs.GetFieldValue(4,var,SQL_C_SLONG);
if(var.m_dwType!=DBVT_NULL)
iCourseId=var.m_iVal;
var.Clear();
CString sCourseName;
sCourseName=FromIDToName("tab_Course","CourseID",iCourseId);
m_cMsCourse.SetTextMatrix(iWeekday,iScheduleId,sCourseName);
rs.MoveNext();
// InsertMsCourse(iWeekday,iScheduleId,sCourseName);
}
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
void CCourseDlg::InsertToDB(CString ClassName, CString Schedule, int Weekday, CString Classroom, CString Course)
{
int ClassId,ScheduleId,ClassroomId,CourseId;
ClassId=FromNameToID("tab_Class","ClassName",ClassName);
ScheduleId=FromNameToID("tab_Schedule","Schedule",Schedule);
ClassroomId=FromNameToID("tab_Classroom","ClassroomName",Classroom);
CourseId=FromNameToID("tab_Course","CourseName",Course);
TRY{
CString sql;
sql.Format("select *from tab_Coursetab where ClassID=%d and ScheduleID=%d and TheWeekday=%d",
ClassId,ScheduleId,Weekday);
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,sql);
if(!rs.IsEOF())
return;
sql.Format("Insert into tab_Coursetab(ClassID,ScheduleID,TheWeekday,ClassroomID,CourseID)"
" values(%d,%d,%d,%d,%d)",ClassId,ScheduleId,Weekday,ClassroomId,CourseId);
TRACE(sql);
m_db.ExecuteSQL(sql);
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
void CCourseDlg::InitCtlrData()
{
m_cDepart.AddString("所有系别");
TRY{
CString sql;
sql.Format("select *from tab_Depart");
CRecordset rs(&m_db);
rs.Open(CRecordset::dynaset,sql);
while(!rs.IsEOF())
{
CString departName;
rs.GetFieldValue(1,departName);
m_cDepart.AddString(departName);
rs.MoveNext();
}
m_cDepart.SetCurSel(0);
rs.Close();
sql.Format("select *from tab_Class");
rs.Open(CRecordset::dynaset,sql);
while(!rs.IsEOF())
{
CString className;
rs.GetFieldValue(1,className);
m_cClass.AddString(className);
rs.MoveNext();
}
}
CATCH(CDBException,ex)
{
AfxMessageBox(ex->m_strError);
AfxMessageBox(ex->m_strStateNativeOrigin);
}
AND_CATCH(CMemoryException,pEx)
{
pEx->ReportError();
AfxMessageBox("memory exception");
}
AND_CATCH(CException,e)
{
TCHAR szError[100];
e->GetErrorMessage(szError,100);
AfxMessageBox(szError);
}
END_CATCH
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -