📄 citydlg.cpp
字号:
// CityDlg.cpp : implementation file
//
#include "stdafx.h"
#include "Ex_CtrlSDI.h"
#include "CityDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CCityDlg dialog
CCityDlg::CCityDlg(CWnd* pParent /*=NULL*/)
: CDialog(CCityDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CCityDlg)
m_strCity = _T("");
m_strZip = _T("");
//}}AFX_DATA_INIT
}
void CCityDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CCityDlg)
DDX_Control(pDX, IDC_LIST1, m_ListBox);
DDX_Text(pDX, IDC_EDIT_CITY, m_strCity);
DDV_MaxChars(pDX, m_strCity, 40);
DDX_Text(pDX, IDC_EDIT_ZIP, m_strZip);
DDV_MaxChars(pDX, m_strZip, 6);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CCityDlg, CDialog)
//{{AFX_MSG_MAP(CCityDlg)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_CHANGE, OnButtonChange)
ON_LBN_SELCHANGE(IDC_LIST1, OnSelchangeList1)
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CCityDlg message handlers
BOOL CCityDlg::IsValidate()
{
UpdateData();
m_strCity.TrimLeft();
if ( m_strCity.IsEmpty() )
{
MessageBox ( "城市名输入无效!" );
return FALSE;
}
m_strZip.TrimLeft();
if ( m_strZip.IsEmpty() )
{
MessageBox ( "邮政编码输入无效!" );
return FALSE;
}
return TRUE;
}
void CCityDlg::OnButtonAdd()
{
if ( !IsValidate() ) return;
int nIndex = m_ListBox.FindStringExact (-1, m_strCity);
if (nIndex != LB_ERR) {
MessageBox("该城市已添加");
return;
}
nIndex = m_ListBox.AddString(m_strCity);
m_ListBox.SetItemDataPtr(nIndex, new CString(m_strZip));
}
void CCityDlg::OnButtonChange()
{
if ( !IsValidate() ) return;
int nIndex = m_ListBox.FindStringExact (-1, m_strCity);
if (nIndex != LB_ERR) {
delete (CString *)m_ListBox.GetItemDataPtr(nIndex);
m_ListBox.SetItemDataPtr(nIndex, new CString(m_strZip));
}
}
void CCityDlg::OnSelchangeList1()
{
int nIndex = m_ListBox.GetCurSel ();
if (nIndex != LB_ERR) {
m_ListBox.GetText(nIndex, m_strCity);
m_strZip = * (CString *)m_ListBox.GetItemDataPtr(nIndex);
UpdateData(FALSE);
}
}
void CCityDlg::OnDestroy()
{
for (int nIndex = m_ListBox.GetCount()-1; nIndex>=0; nIndex--)
{
// 删除所有与列表项相关联的CString数据,并释放内存
delete (CString *)m_ListBox.GetItemDataPtr(nIndex);
}
CDialog::OnDestroy();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -