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

📄 mydialog.cpp

📁 学习VC++时做得小练习
💻 CPP
字号:
// mydialog.cpp : implementation file
//

#include "stdafx.h"
#include "测试.h"
#include "mydialog.h"

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

/////////////////////////////////////////////////////////////////////////////
// mydialog dialog


mydialog::mydialog(CWnd* pParent /*=NULL*/)
	: CDialog(mydialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(mydialog)
	m_nbase = 0;
	m_nchinese = 0;
	m_nenglish = 0;
	m_nmath = 0;
	m_strname = _T("");
	//}}AFX_DATA_INIT
}


void mydialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(mydialog)
	DDX_Control(pDX, IDC_LISTSTATE, m_ListBx);
	DDX_Text(pDX, IDC_EDITbase, m_nbase);
	DDV_MinMaxDWord(pDX, m_nbase, 0, 100);
	DDX_Text(pDX, IDC_EDITchinese, m_nchinese);
	DDV_MinMaxDWord(pDX, m_nchinese, 0, 100);
	DDX_Text(pDX, IDC_EDITenglish, m_nenglish);
	DDV_MinMaxDWord(pDX, m_nenglish, 0, 100);
	DDX_Text(pDX, IDC_EDITmath, m_nmath);
	DDV_MinMaxDWord(pDX, m_nmath, 0, 100);
	DDX_Text(pDX, IDC_EDITname, m_strname);
	DDV_MaxChars(pDX, m_strname, 20);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(mydialog, CDialog)
	//{{AFX_MSG_MAP(mydialog)
	ON_BN_CLICKED(IDC_BUTNADD, OnButnadd)
	ON_BN_CLICKED(IDC_BUTNDEL, OnButndel)
	ON_LBN_SELCHANGE(IDC_LISTSTATE, OnSelchangeListstate)
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// mydialog message handlers

void mydialog::OnButnadd() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);  // 获得控件中的数据
	if(m_strname.IsEmpty())  //判断"姓名"名称是否为空
	{
		MessageBox("姓名不能为空!");
		return;
	}
	m_strname.TrimLeft();    //去掉m_strName左边的空格
	m_strname.TrimRight();   //去掉m_strName右边的空格
	if((m_ListBx.FindString(-1,m_strname))!=LB_ERR)
	{
		MessageBox("列表框中已有该项,不能在添加!");
		return;
	}
	int nIndex=m_ListBx.AddString(m_strname);  //向列表框添加姓名
	SStudent stStudent;                            //将国家数据项与新增的列表项关联起来
	stStudent.strname=m_strname;
	stStudent.nmath=m_nmath;
	stStudent.nenglish=m_nenglish;
	stStudent.nchinese=m_nchinese;
	stStudent.nbase=m_nbase;
	m_ListBx.SetItemDataPtr(nIndex,new SStudent(stStudent));  //建立关联

}

void mydialog::OnButndel() 
{
	// TODO: Add your control notification handler code here
	int nIndex=m_ListBx.GetCurSel();  //获得当前列表项的索引
	if(nIndex!=LB_ERR)
	{
		delete(SStudent *)m_ListBx.GetItemDataPtr(nIndex);  //释放关联数据所占的内存空间
		m_ListBx.DeleteString(nIndex);     //删除列表框当前选项
		m_strname=m_strname="";       //设置编辑框控件数据
		m_nmath=m_nenglish=m_nchinese=m_nbase=0;
		UpdateData(FALSE);              //在编辑框中显示数据
	}
	else
		MessageBox("没有选择列表项或列表框操作失败!");
	
}

void mydialog::OnSelchangeListstate() 
{
	// TODO: Add your control notification handler code here
	int nIndex=m_ListBx.GetCurSel();
	if(nIndex!=LB_ERR)
	{
		SStudent * pstStu=(SStudent * )m_ListBx.GetItemDataPtr(nIndex);  //获得关联数据
		m_strname=pstStu->strname;   //设置控件成员变量
		m_nmath=pstStu->nmath;
		m_nchinese=pstStu->nchinese;
		m_nenglish=pstStu->nenglish;
		m_nbase=pstStu->nbase;
		UpdateData(FALSE);   //显示数据
	}
}

void mydialog::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	int nIndex=m_ListBx.GetCurSel();
	if(nIndex!=LB_ERR)
	{
		SStudent * pstStu=(SStudent * )m_ListBx.GetItemDataPtr(nIndex);  //获得关联数据
		m_strname=pstStu->strname;   //设置控件成员变量
		m_nmath=pstStu->nmath;
		m_nchinese=pstStu->nchinese;
		m_nenglish=pstStu->nenglish;
		m_nbase=pstStu->nbase;
		UpdateData(FALSE);   //显示数据
	}
}

⌨️ 快捷键说明

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