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

📄 lendinfodlg.cpp

📁 < Visual C+++SQL Server数据库开发与实例>>一书中的实例核心源程序提供了几个案例的编程源代码。
💻 CPP
字号:
// LendInfoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DeviceDBS.h"
#include "LendInfoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CLendInfoDlg dialog


CLendInfoDlg::CLendInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CLendInfoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CLendInfoDlg)
	m_strSelectedCode = _T("");
	m_strSelectedName = _T("");
	//}}AFX_DATA_INIT
}


void CLendInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CLendInfoDlg)
	DDX_Control(pDX, IDC_LIST_LEND, m_listLend);
	DDX_Control(pDX, IDC_COMBO_NAME, m_comboName);
	DDX_Control(pDX, IDC_COMBO_CODE, m_comboCode);
	DDX_CBString(pDX, IDC_COMBO_CODE, m_strSelectedCode);
	DDX_CBString(pDX, IDC_COMBO_NAME, m_strSelectedName);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CLendInfoDlg, CDialog)
	//{{AFX_MSG_MAP(CLendInfoDlg)
	ON_BN_CLICKED(IDC_BTN_SHOW, OnBtnShow)
	ON_CBN_CLOSEUP(IDC_COMBO_NAME, OnCloseupComboName)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CLendInfoDlg message handlers

void CLendInfoDlg::OnBtnShow() 
{
	// TODO: Add your control notification handler code here
	//从界面控件中获取信息更新到控件变量中.
	if(!UpdateData())
		return;
	if(m_strSelectedName.IsEmpty()){
		MessageBox("请选择设备名称");
		return;
	}
	if(m_strSelectedCode.IsEmpty()){
		MessageBox("请选择设备编号");
		return;
	}
	m_listLend.DeleteAllItems();
	TRY{
		CRecordset rs(m_pDB);
		CString sql;
		//如果有提示用户,返回界面.
		sql.Format("Select * from device_lend_info_tab "
			"where device_code = '%s'",m_strSelectedCode);
		rs.Open(CRecordset::snapshot, sql);
		while (!rs.IsEOF()) {
			CString borrower,brDate,rtDate;
			int lendID = 0;
			CDBVariant var;
			rs.GetFieldValue((short)0, var, SQL_C_SLONG);
			if (var.m_dwType != DBVT_NULL)	
				lendID = var.m_iVal;
			var.Clear();
			//获取设备名称字段值
			rs.GetFieldValue(2, borrower);
			rs.GetFieldValue(3, brDate);
			rs.GetFieldValue(4, rtDate);
			//向借出列表信息控件添加信息
			InsertLendInfoItem(lendID,borrower,brDate,rtDate);
			rs.MoveNext();	
		}
		rs.Close();
	}
	CATCH(CDBException,ex)
	{
		AfxMessageBox (ex->m_strError);
		AfxMessageBox (ex->m_strStateNativeOrigin);		
	}
	AND_CATCH(CException,e)
	{
		TCHAR szError[100];
		e->GetErrorMessage(szError,100);
		AfxMessageBox (szError);
	}
	END_CATCH				
}

BOOL CLendInfoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	//设置列表框控件扩展风格
	DWORD dwExStyle = LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES |
		LVS_EX_HEADERDRAGDROP | LVS_EX_ONECLICKACTIVATE | LVS_EX_UNDERLINEHOT;
	m_listLend.SetExtendedStyle(dwExStyle);
	//初始化借出信息列表框控件
	m_listLend.InsertColumn(0,"借出ID",LVCFMT_CENTER,80);
	m_listLend.InsertColumn(1,"借出人",LVCFMT_CENTER,80);
	m_listLend.InsertColumn(2,"设备借出时间",LVCFMT_CENTER,140);
	m_listLend.InsertColumn(3,"设备归还时间",LVCFMT_CENTER,140);
	//更新设备名称和设备编号组合框的数据
	RefreshComboNameData();
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CLendInfoDlg::RefreshComboNameData()
{
	m_comboName.ResetContent();
	TRY{
		CRecordset rs(m_pDB);
		//打开所有的设备名称记录.
		rs.Open(CRecordset::dynaset, "select distinct device_name from device_info_tab");
		while (!rs.IsEOF()) {
			CString strName;
			//获取设备名称字段值
			rs.GetFieldValue((short)0, strName);
			//向设备名称组合框添加所有设备名称.
			m_comboName.AddString(strName);
			rs.MoveNext();	
		}

		rs.Close();
		m_comboName.SetCurSel(1);
		//更新设备组合框的数据
		CString strSelected;
		m_comboName.GetLBText(0,strSelected);
		CString sql;
		sql.Format("select device_code from device_info_tab "
			"where device_name = '%s'",strSelected);
		rs.Open(CRecordset::dynaset,sql);
		m_comboCode.ResetContent();
		while (!rs.IsEOF()) {
			CString strCode;
			//获取设备编号字段值
			rs.GetFieldValue((short)0, strCode);
			//向设备编号组合框添加名称为前面选择的设备名称的所有设备编号.
			m_comboCode.AddString(strCode);
			rs.MoveNext();	
		}
		m_comboCode.SetCurSel(0);
	}
	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 CLendInfoDlg::OnCloseupComboName() 
{
	// TODO: Add your control notification handler code here
	TRY{
		CRecordset rs(m_pDB);
		//打开所有的设备信息记录.
		CString sql;
		CString strSelected;
		//获取当前选择项目
		int nIndex = m_comboName.GetCurSel();
		//如果没有选择,退出
		if(nIndex == -1)
			return;
		//获取当前选择的设备名称
		m_comboName.GetLBText(nIndex,strSelected);
		sql.Format("select device_code from device_info_tab "
			"where device_name = '%s'",strSelected);
		rs.Open(CRecordset::dynaset,sql);
		m_comboCode.ResetContent();
		while (!rs.IsEOF()) {
			CString strCode;
			//获取设备编号字段值
			rs.GetFieldValue((short)0, strCode);
			//向设备编号组合框添加名称为前面选择的设备名称的所有设备编号.
			m_comboCode.AddString(strCode);
			rs.MoveNext();	
		}
		m_comboCode.SetCurSel(0);
	}
	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 CLendInfoDlg::InsertLendInfoItem(int id,CString borrower,CString lendDate,CString returnDate)
{
	//获取当前的纪录条数.
	int nIndex = m_listLend.GetItemCount();
	LV_ITEM lvItem;
	lvItem.mask = LVIF_TEXT ;	
	lvItem.iItem = nIndex;				//行数
	lvItem.iSubItem = 0;
	CString temp ;
	temp.Format("%d",id);
	lvItem.pszText = (char*)(LPCTSTR)temp;		//第一列
	//在最后一行插入记录值.
	m_listLend.InsertItem(&lvItem);	
	//设置该行的其他列的值.
	m_listLend.SetItemText(nIndex,1,borrower);	
	m_listLend.SetItemText(nIndex,2,lendDate);	
	m_listLend.SetItemText(nIndex,3,returnDate);	
}

⌨️ 快捷键说明

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