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

📄 zhquerystu_infodlg.cpp

📁 是我大学事情开发的一个很经典的案例
💻 CPP
字号:
// zhquerystu_infoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "pmz.h"
#include "pmzDlg.h"
#include "zhquerystu_infoDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// zhquerystu_infoDlg dialog


zhquerystu_infoDlg::zhquerystu_infoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(zhquerystu_infoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(zhquerystu_infoDlg)
	m_sname = _T("");
	m_splace = _T("");
	m_sclass = _T("");
	m_scheckname = FALSE;
	m_schecksex = FALSE;
	m_scheckplace = FALSE;
	m_scheckclass = FALSE;
	//}}AFX_DATA_INIT
    m_pdb=new CDatabase;                        //yang
	m_pdb->Open( NULL, FALSE, FALSE,mydb); 
}

zhquerystu_infoDlg::~zhquerystu_infoDlg()
{
	m_pdb->Close();
	delete m_pdb;
}

BOOL zhquerystu_infoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	CListCtrl* m_pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
 	DWORD dwStyle= GetWindowLong(m_pList->m_hWnd, GWL_STYLE); 
    SetWindowLong( m_pList->m_hWnd, GWL_STYLE, dwStyle | LVS_REPORT);
 	
 	//设置ListCtrl可以整行选择和网格条纹
 	DWORD styles = m_pList->GetExtendedStyle();
 	m_pList->SetExtendedStyle(styles|LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
 	m_pList->SetBkColor(RGB(0xFF, 0xFF, 0xE0));
  	m_pList->SetTextBkColor(RGB(0xFF, 0xFF, 0xE0));


    //设置列的题头
	m_pList->InsertColumn(1,"学号",LVCFMT_CENTER,80);
	m_pList->InsertColumn(2,"姓名",LVCFMT_CENTER,120);
	m_pList->InsertColumn(3,"性别",LVCFMT_CENTER,80);
	m_pList->InsertColumn(4,"年龄",LVCFMT_CENTER,80);	
	m_pList->InsertColumn(5,"生源地",LVCFMT_CENTER,180);
	m_pList->InsertColumn(6,"班级",LVCFMT_CENTER,180);
    m_pList->InsertColumn(7,"所属院系",LVCFMT_CENTER,180);

	m_ssex.AddString("男");
	m_ssex.AddString("女");
	m_ssex.SetCurSel(0);
	return true;
}
void zhquerystu_infoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(zhquerystu_infoDlg)
	DDX_Control(pDX, IDC_LIST1, m_pList);
	DDX_Control(pDX, IDC_COMBO1, m_ssex);
	DDX_Text(pDX, IDC_EDIT1, m_sname);
	DDX_Text(pDX, IDC_EDIT3, m_splace);
	DDX_Text(pDX, IDC_EDIT4, m_sclass);
	DDX_Check(pDX, IDC_CHECK1, m_scheckname);
	DDX_Check(pDX, IDC_CHECK2, m_schecksex);
	DDX_Check(pDX, IDC_CHECK3, m_scheckplace);
	DDX_Check(pDX, IDC_CHECK4, m_scheckclass);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(zhquerystu_infoDlg, CDialog)
	//{{AFX_MSG_MAP(zhquerystu_infoDlg)
	ON_BN_CLICKED(IDC_BUTTON1, Ondelete)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// zhquerystu_infoDlg message handlers

void zhquerystu_infoDlg::OnOK() 
{
   UpdateData(TRUE);
   CListCtrl* m_pList = (CListCtrl*)GetDlgItem(IDC_LIST1);
   m_pList->DeleteAllItems();
   CString str="SELECT * FROM S_info ",str1,str11,str12,str13;
   int j=0;
   if(m_scheckclass==1||m_scheckname==1||m_schecksex==1||m_scheckplace==1)
   {
       if(m_scheckname==1)
	   {  
	      j++;
	      m_sname.TrimLeft();
	      m_sname.TrimRight();
	      if(m_sname.GetLength()<1) 
		  {
				AfxMessageBox("学生姓名不能为空!");
				return;
		  }
	      else 
		  {
			  str1=m_sname;
			  if(j>1)
			  {
			    str=str+ "and S_name='"+str1+"'";
			  }
		      else
			  {
		         str=str+"where S_name='"+str1+"'";
			  }	  
		  }
	   }
       if(m_schecksex==1)
	   { 
	       j++;
	       int index=m_ssex.GetCurSel();
	       m_ssex.GetLBText(index,str11);
	       if(str11=="")
		   {
		      AfxMessageBox("请选择性别!");
		      return;
		   }
	       else 
		   {	
		      if(j>1)
			  {
		 	    str=str+"and S_sex='"+str11+"'";
			  }
		      else
			  {
			    str=str+"where S_sex='"+str11+"'";			 
			  }
		   }
	   }
   
      if(m_scheckplace==1)
	  {
	      j++;
	      m_splace.TrimLeft();
	      m_splace.TrimRight();
	      if(m_splace.GetLength()<1) 
		  {
		    AfxMessageBox("学生地址不能为空!");
		    return;
		  }
	      else 
		  {
		    str12=m_splace;
		    if(j>1)
			{
			   str=str+ "and S_place='"+str12+"'";
			}
		    else
			{
		      str1=m_splace;
		      str=str+"where S_place='"+str12+"'";
			}
		  }

	  }
      if(m_scheckclass==1)
	  {
	     j++;
	     m_sclass.TrimLeft();
	     m_sclass.TrimRight();
	     if(m_sclass.GetLength()<1) 
		 {
		      AfxMessageBox("学生班级不能为空!");
		      return;
		 }
	     else 
		 {
		   str13=m_sclass;
		   if(j>1)
		   {
			   str=str+ "and S_class='"+str13+"'";
		   }
		   else
		   {
		      str1=m_sclass;
		      str=str+"where S_class='"+str13+"'";
		   }
		 }

	  }
     CString str3,s_no,s_name,s_sex,s_age,s_place,s_class,s_department_no,s_department;
   
     m_sprecordset=new S_infodb(m_pdb);
	 m_sprecordset->Open(S_infodb::snapshot,str);
     int i=0;
	 if(m_sprecordset->IsEOF())
	 {
      
	 }
	 else
	 {
	   m_sprecordset->MoveFirst();
	   while(!m_sprecordset->IsEOF())
	   {
		
    	s_no=m_sprecordset->m_S_no;
		s_name=m_sprecordset->m_S_name;
		s_sex=m_sprecordset->m_S_sex;
		s_age=m_sprecordset->m_S_age;
		s_place=m_sprecordset->m_S_place;
		s_class=m_sprecordset->m_S_class;
		s_department_no=m_sprecordset->m_department_no;
		
		str3="SELECT * FROM department_info where department_no='"+s_department_no+"'";
		m_dprecordset=new department_infodb(m_pdb);
	    m_dprecordset->Open(department_infodb::snapshot,str3);
		s_department=m_dprecordset->m_department_name;
		m_pList->InsertItem(i,s_no);
		m_pList->SetItemText(i,1,s_name);
		m_pList->SetItemText(i,2,s_sex);
		m_pList->SetItemText(i,3,s_age);
		m_pList->SetItemText(i,4,s_place);
		m_pList->SetItemText(i,5,s_class);
		m_pList->SetItemText(i,6,s_department);
		
		m_sprecordset->MoveNext();
		i++;
	   } 
	 }

	   delete m_sprecordset;
  }
  else 
  {
	AfxMessageBox("请选择组合查询条件!");
	return;
  }

}

void zhquerystu_infoDlg::Ondelete() 
{
   int j;
   j=m_pList.GetSelectionMark();
   if(j<0)
   {
	  AfxMessageBox("请在列表框点击选中要删除的记录!");
	  return;
   }
   else
   {
	  CString str;
	  str=m_pList.GetItemText(j,0);
	  if(str=="")
	  {
		 AfxMessageBox("列表框没有记录!");
	     return;
	  }
	  if(AfxMessageBox("这将删除所有与此院系有关的信息,继续吗?",MB_YESNO)==IDYES)
	  {	   
	   CString str1="delete from S_info where S_no= '"+str+"'";
       m_pdb->ExecuteSQL(str1);
	   m_pList.DeleteItem(j);
	   AfxMessageBox("删除成功!");
	  }
	  else return;	
   }
}

⌨️ 快捷键说明

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