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

📄 athletedlg.cpp

📁 本源码非常详细的实现了一个运动员信息管理系统 其中包括管理员
💻 CPP
字号:
// AthleteDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AthleteInfo.h"
#include "AthleteDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAthleteDlg dialog


CAthleteDlg::CAthleteDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAthleteDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAthleteDlg)
	//}}AFX_DATA_INIT
}


void CAthleteDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAthleteDlg)
	DDX_Control(pDX, ID_ATHLETE_OK, m_bConfirm);
	DDX_Control(pDX, IDC_LIST_ATHLETE, m_list);
	DDX_Control(pDX, IDC_ATHLETE_DEL, m_bDel);
	DDX_Control(pDX, IDC_ATHLETE_EDIT, m_bEdit);
	DDX_Control(pDX, IDC_ATHLETE_ADD, m_bAdd);
	DDX_Control(pDX, IDC_ATHLETE_PHONE, m_strPhone);
	DDX_Control(pDX, IDC_ATHLETE_NO, m_strNo);
	DDX_Control(pDX, IDC_ATHLETE_NAME, m_strName);
	DDX_Control(pDX, IDC_ATHLETE_COUNTRY, m_strCountry);
	DDX_Control(pDX, IDC_ATHLETE_AGE, m_strAge);
	DDX_Control(pDX, IDC_ATHLETE_ADDRESS, m_strAddress);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CAthleteDlg, CDialog)
	//{{AFX_MSG_MAP(CAthleteDlg)
	ON_NOTIFY(NM_CLICK, IDC_LIST_ATHLETE, OnClickListAthlete)
	ON_BN_CLICKED(IDC_ATHLETE_ADD, OnAthleteAdd)
	ON_BN_CLICKED(IDC_ATHLETE_EDIT, OnAthleteEdit)
	ON_BN_CLICKED(IDC_ATHLETE_DEL, OnAthleteDel)
	ON_BN_CLICKED(ID_ATHLETE_OK, OnAthleteOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAthleteDlg message handlers

void CAthleteDlg::RefreshList()
{
	m_list.DeleteAllItems();
	m_athleteSet.Open();
	m_athleteSet.m_strFilter="active_status='Y'";
	m_athleteSet.Requery();
	for(int i=0;i<m_athleteSet.GetRecordCount();i++){
		CString temp;
		m_athleteSet.GetFieldValue("athlete_id",temp);
		m_list.InsertItem(i,temp);
		m_athleteSet.GetFieldValue("athlete_no",temp);
		m_list.SetItemText(i,1,temp);
		m_athleteSet.GetFieldValue("athlete_name",temp);
		m_list.SetItemText(i,2,temp);
		m_athleteSet.GetFieldValue("athlete_sex",temp);
		m_list.SetItemText(i,3,temp);
		m_athleteSet.GetFieldValue("athlete_age",temp);
		m_list.SetItemText(i,4,temp);
		m_athleteSet.GetFieldValue("athlete_phone",temp);
		m_list.SetItemText(i,5,temp);
		m_athleteSet.GetFieldValue("athlete_localaddress",temp);
		m_list.SetItemText(i,6,temp);
		m_athleteSet.GetFieldValue("athlete_country",temp);
		m_list.SetItemText(i,7,temp);
		m_athleteSet.MoveNext();
	}
	m_athleteSet.Close();
}

void CAthleteDlg::ResetButton()
{
	m_strNo.EnableWindow(false);
	m_strName.EnableWindow(false);
	m_strAge.EnableWindow(false);
	m_strPhone.EnableWindow(false);
	m_strAddress.EnableWindow(false);
	m_strCountry.EnableWindow(false);
	femaleRadio->EnableWindow(false);
    m_bAdd.EnableWindow(true);
	m_bEdit.EnableWindow(true);
	m_bDel.EnableWindow(true);

}

BOOL CAthleteDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	//初始化里CList 控件的和header
    m_list.InsertColumn(0,"记录号");
	m_list.InsertColumn(1,"编号");
    m_list.InsertColumn(2,"姓名");
    m_list.InsertColumn(3,"性别");
	m_list.InsertColumn(4,"年龄");
    m_list.InsertColumn(5,"电话");
    m_list.InsertColumn(6,"地址");
	m_list.InsertColumn(7,"国家");
	//设置header的宽度
    RECT rectList;
	m_list.GetWindowRect(&rectList);
	int wid=rectList.right-rectList.left-4;
	for(int i=0;i<8;i++)
		m_list.SetColumnWidth(i,wid/8);
    m_list.SetExtendedStyle(LVS_EX_FULLROWSELECT);
	//调用refreshList()初始化CList中的数据
	RefreshList();
	//为年龄的下拉列表添加item
	
	femaleRadio=(CButton*)this->GetDlgItem(IDC_RADIO_SEX_FEMALE);
    maleRadio=(CButton*)this->GetDlgItem(IDC_RADIO_SEX_MALE);
	femaleRadio->EnableWindow(false);
	maleRadio->EnableWindow(false);
    //调用ResetButton()方法,使按钮处于初始状态
	ResetButton();
	return TRUE;
}




void CAthleteDlg::OnClickListAthlete(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here
	int row=m_list.GetSelectionMark();
	CString s;
	//将第2列的值赋给临时字符串s中
	s=m_list.GetItemText(row,1);
	//相应的编辑框显示该值
	m_strNo.SetWindowText(s);
    s=m_list.GetItemText(row,2);
	m_strName.SetWindowText(s);
    s=m_list.GetItemText(row,3);
    //设置单选按钮选中状态
	if(s=="女")
	{
		femaleRadio->SetCheck(true);
		maleRadio->SetCheck(false);
	}
    else
	{
		femaleRadio->SetCheck(false);
		maleRadio->SetCheck(true);
	}
    //设置年龄的下拉列表值
	s=m_list.GetItemText(row,4);
    //int i=m_strAge.FindString(0,s);
    //m_strAge.SelectString(i,s);
	m_strAge.SetWindowText(s);
	s=m_list.GetItemText(row,5);
	m_strPhone.SetWindowText(s);
	s=m_list.GetItemText(row,6);
	m_strAddress.SetWindowText(s);
	s=m_list.GetItemText(row,7);
	m_strCountry.SetWindowText(s);
    *pResult = 0;
}
	



void CAthleteDlg::OnAthleteAdd() 
{
	// TODO: Add your control notification handler code here
	m_strNo.EnableWindow(true);
 	m_strNo.SetWindowText("");
 	m_strName.EnableWindow(true);
    m_strName.SetWindowText("");
 	m_strAge.EnableWindow(true);
 	m_strAge.SetWindowText("");
 	m_strPhone.EnableWindow(true);
 	m_strAddress.EnableWindow(true);
 	m_strCountry.EnableWindow(true);
 	femaleRadio->EnableWindow(true);
 	maleRadio->EnableWindow(true);
 	//打开记录集
    m_athleteSet.Open();
 	//添加新记录
 	m_athleteSet.AddNew();
 	//设置点击确定按钮后,其他按钮的状态
 	m_bConfirm.EnableWindow(true);
    m_bAdd.EnableWindow(false);
 	m_bEdit.EnableWindow(false);
 	m_bDel.EnableWindow(false);	
	
}

void CAthleteDlg::OnAthleteEdit() 
{
	// TODO: Add your control notification handler code here
	int row=m_list.GetSelectionMark();
	CString s,strSQL;
	//将当前行的第一列的值赋给s
	s=m_list.GetItemText(row,0);
	if(s=="")//如果s为空,则表示没有选中行
	{
		MessageBox("请选择一行!");
	}
	else//如果有选中一行
	{
		//定义查询条件,相当于where
		strSQL.Format("athlete_id=%s",s);
		//打开记录集
		m_athleteSet.Open();
		//设置查询条件
		m_athleteSet.m_strFilter=strSQL;
		//执行查询
		m_athleteSet.Requery();
		//游标指定到该行所在的记录上,执行修改
		m_athleteSet.Edit();
		//设置点击修改按钮后其他按钮的状态
		m_bConfirm.EnableWindow(true);
		m_bAdd.EnableWindow(false);
		m_bEdit.EnableWindow(false);
		m_bDel.EnableWindow(false);
		m_strName.EnableWindow(true);
		femaleRadio->EnableWindow(true);
		maleRadio->EnableWindow(true);
		m_strNo.EnableWindow(true);
		m_strAge.EnableWindow(true);
		m_strPhone.EnableWindow(true);
		m_strAddress.EnableWindow(true);
		m_strCountry.EnableWindow(true);

	}
	
}

void CAthleteDlg::OnAthleteDel() 
{
	// TODO: Add your control notification handler code here
	int row=m_list.GetSelectionMark();
	CString s,strSQL;
	//将当前行的第一列的值赋给s
	s=m_list.GetItemText(row,0);
	if(s=="")//如果s为空,则表示没有选中行
	{
		MessageBox("请选择一行!");
	}
	else
	{
		//弹出窗口询问是否删除数据
		if(MessageBox("确定要删除记录吗?","删除询问",MB_OKCANCEL|MB_ICONQUESTION)==IDOK)
		{
			//查询所选行所在的记录
			strSQL.Format("athlete_id=%s",s);
			m_athleteSet.Open();
			m_athleteSet.m_strFilter=strSQL;
			m_athleteSet.Requery();
			m_athleteSet.Edit();
			//设置active_status="N";
			m_athleteSet.m_active_status="N";
			//更新数据库
			m_athleteSet.Update();
			//关闭数据库
			m_athleteSet.Close();
			//更新CList数据
			RefreshList();
		}
	}
	
}

void CAthleteDlg::OnAthleteOk() 
{
	// TODO: Add your control notification handler code here
	CString no,name,phone,address,strSex,country;
	//得到输入编辑框的值
	m_strNo.GetWindowText(no);
	m_strName.GetWindowText(name);
	m_strPhone.GetWindowText(phone);
	m_strAddress.GetWindowText(address);
	m_strCountry.GetWindowText(country);
	//将CString类型转化为int型
	int age;
	CString s;
    m_strAge.GetWindowText(s);
	age=atoi(s);
	//取得单选按纽的状态
	int sex=this->GetCheckedRadioButton(IDC_RADIO_SEX_MALE,IDC_RADIO_SEX_FEMALE);
	if(sex==IDC_RADIO_SEX_FEMALE)
		strSex="女";
	else
		strSex="男";
	if(name=="")//如果运动员姓名为空
	{
		MessageBox("请输入教师姓名");
	}
	else
	{
		//设置记录添加或编辑后的值
        m_athleteSet.m_athlete_no=no;
		m_athleteSet.m_athlete_name=name;
		m_athleteSet.m_athlete_sex=strSex;
		m_athleteSet.m_athlete_age=age;
		m_athleteSet.m_athlete_phone=phone;
        //m_athleteSet.m_athlete_address=address;
		m_athleteSet.m_athlete_localaddress=address;
		m_athleteSet.m_athlete_country=country;
		m_athleteSet.m_active_status="Y";
		//更新记录集
		m_athleteSet.Update();
		//关闭记录集
		m_athleteSet.Close();
		//更新列表数据
		RefreshList();
		//按钮恢复初始状态
		ResetButton();
	}
	
}

void CAthleteDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
	CDialog::OnCancel();
}

⌨️ 快捷键说明

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