📄 deptdlg.cpp
字号:
// DeptDlg.cpp : implementation file
//
#include "stdafx.h"
#include "tel2006.h"
#include "DeptDlg.h"
#include "Dept2Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
#define VIEW_COLCOUNT 2
static char s_view_colname[VIEW_COLCOUNT][10] = {"部门编号", "部门名称"};
static int s_view_colwidth[VIEW_COLCOUNT] = {120,120};
/////////////////////////////////////////////////////////////////////////////
// CDeptDlg dialog
CDeptDlg::CDeptDlg(CWnd* pParent /*=NULL*/)
: CDialog(CDeptDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CDeptDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
//m_manager = new CDataManager();
}
void CDeptDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CDeptDlg)
DDX_Control(pDX, IDC_LIST, m_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CDeptDlg, CDialog)
//{{AFX_MSG_MAP(CDeptDlg)
ON_BN_CLICKED(IDC_ADD_BUTTON, OnAddButton)
ON_BN_CLICKED(IDC_MODIFY_BUTTON, OnModifyButton)
ON_BN_CLICKED(IDC_DEL_BUTTON, OnDelButton)
ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CDeptDlg message handlers
void CDeptDlg::OnAddButton()
{
// TODO: Add your control notification handler code here
CDept2Dlg dlg;
if ( IDCANCEL == dlg.DoModal() ) return;
if(!JudgeDeptID(dlg.GetDeptID()))
{
AfxMessageBox("编号错误!");
return;
}
if(dlg.GetDeptName().GetLength() == 0)
{
AfxMessageBox("名称不能为空");
return;
}
InsertData(dlg.GetDeptID(),dlg.GetDeptName());
AddList(dlg.GetDeptID(),dlg.GetDeptName());
}
void CDeptDlg::OnModifyButton()
{
// TODO: Add your control notification handler code here
CDept2Dlg dlg;
int n= m_list.GetSelectionMark();
dlg.SetDeptID(m_list.GetItemText(n,0));
dlg.SetDeptName(m_list.GetItemText(n,1));
if ( IDCANCEL == dlg.DoModal() ) return;
if(dlg.GetDeptID().GetLength() != 2)
{
AfxMessageBox("编号必须为2位");
return;
}
if(dlg.GetDeptName().GetLength() == 0)
{
AfxMessageBox("名称不能为空");
return;
}
ModifyData(m_list.GetItemText(n,0),dlg.GetDeptID(),dlg.GetDeptName());
ModifyList(dlg.GetDeptID(),dlg.GetDeptName());
}
void CDeptDlg::OnDelButton()
{
int n = m_list.GetSelectionMark();
DeleteData(m_list.GetItemText(n,0));
m_list.DeleteItem(n);
}
void CDeptDlg::OnExitButton()
{
// TODO: Add your control notification handler code here
m_AdoConn.ExitConnect();
//delete(m_manager);
CDialog::OnCancel();
}
BOOL CDeptDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
InitList();
m_AdoConn.OnInitADOConn(".");
ShowList();
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
/*******************************
* InitList
*
* Remark: 初始化List控件
******************************/
void CDeptDlg::InitList()
{
LV_COLUMN lvC;
int i;
//ListView_SetImageList(m_list.m_hWnd, ImageList_Create(1, 16, ILC_COLOR, 1, 1), LVSIL_SMALL);
ListView_SetExtendedListViewStyle(m_list.m_hWnd, LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
lvC.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
lvC.fmt = LVCFMT_LEFT;
for (i = 0; i < VIEW_COLCOUNT; i++)
{
lvC.pszText = s_view_colname[i];
lvC.cx = s_view_colwidth[i];
lvC.iSubItem = i;
if (ListView_InsertColumn(m_list.m_hWnd, i, &lvC) == -1) return;
}
ListView_SetItemCount(m_list.m_hWnd, VIEW_COLCOUNT);
//m_list.SetSelectionMark(1);
}
/***********************************************
* 函数名: CPhoneDlg::ShowList
*
* 功 能: 显示
*
* 返回值: void
***********************************************/
void CDeptDlg::ShowList()
{
_variant_t var;
CString strCount("");
//ADOConn m_AdoConn;
//m_AdoConn.OnInitADOConn(m_strDataBase);
//设置SELECT语句
CString strSQL;
strSQL.Format("select * from R_TelDept");
_bstr_t vSQL =strSQL;
_RecordsetPtr m_pRecordset;
m_pRecordset = m_AdoConn.GetRecordSet(vSQL);
int n = 0;
m_list.DeleteAllItems();
m_list.Invalidate(TRUE);
while(!m_pRecordset->adoEOF)
{
m_list.InsertItem(n,"");
var = m_pRecordset->GetCollect("T_DeptID");
if(var.vt != VT_NULL)
m_list.SetItemText(n,0,(LPCSTR)_bstr_t(var));
var = m_pRecordset->GetCollect("T_DeptName");
if(var.vt != VT_NULL)
m_list.SetItemText(n,1,(LPCSTR)_bstr_t(var));
n++;
m_pRecordset->MoveNext();
}
//m_AdoConn.ExitConnect();
}
/*************************************
* AddList
*
* Parameters: structData // 结构体
*
* Remark: 添加入ListView中,显示
************************************/
void CDeptDlg::AddList(CString strDeptID,CString strDeptName)
{
int Count = m_list.GetItemCount();
m_list.InsertItem(Count,"");
m_list.SetItemText(Count,0,strDeptID);
m_list.SetItemText(Count,1,strDeptName);
}
void CDeptDlg::ModifyList(CString strDeptID,CString strDeptName)
{
int n = m_list.GetSelectionMark();
m_list.SetItemText(n,0,strDeptID);
m_list.SetItemText(n,1,strDeptName);
}
BOOL CDeptDlg::JudgeDeptID(CString strDeptID)
{
if(strDeptID.GetLength()!=2)
return false;
_variant_t var;
//设置SELECT语句
CString strSQL;
strSQL.Format( "SELECT T_DeptID FROM R_TelDept WHERE T_DeptID = '%s'",strDeptID);
_bstr_t vSQL =strSQL;
_RecordsetPtr pRecordset;
pRecordset = m_AdoConn.GetRecordSet(vSQL);
BOOL bRet;
if (pRecordset->adoEOF != VARIANT_TRUE)
{
bRet = false;
}
else
{
bRet = true;
}
return bRet;
}
void CDeptDlg::InsertData(CString strDeptID,CString strDeptName)
{
CString strSQL("");
try
{
strSQL.Format ("INSERT INTO R_TelDept(T_DeptID,T_DeptName)\
VALUES('%s','%s')", strDeptID,strDeptName);
_bstr_t vSQL =strSQL;
m_AdoConn.ExecuteSQL(vSQL);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("自定义错误信息3:%s",e.ErrorMessage());
AfxMessageBox(errormessage);//显示错误信息
}
}
void CDeptDlg::DeleteData(CString strDeptID)
{
CString strSQL("");
try
{
strSQL.Format("Delete from R_TelDept where T_DeptID = '%s'",strDeptID);
_bstr_t vSQL =strSQL;
m_AdoConn.ExecuteSQL(vSQL);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("自定义错误信息2:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
void CDeptDlg::ModifyData(CString strOldDeptID,CString strDeptID,CString strDeptName)
{
CString strSQL("");
try
{
strSQL.Format("update R_TelDept SET T_DeptID = '%s',T_DeptName= '%s' WHERE T_DeptID ='%s'",\
strDeptID,strDeptName,strOldDeptID);
_bstr_t vSQL =strSQL;
m_AdoConn.ExecuteSQL(vSQL);
}
catch (_com_error e)
{
CString errormessage;
errormessage.Format("自定义错误信息3:%s",e.ErrorMessage());
AfxMessageBox(errormessage);///显示错误信息
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -