📄 socialisepersondlg.cpp
字号:
void CSocialisePersonDlg::OnBnClickedSocialisePersonAddBt()
{
// TODO: 在此添加控件通知处理程序代码
Add();
}
void CSocialisePersonDlg::Update ()
{
CString s;
POSITION pos = m_cPersonListCtrl.GetFirstSelectedItemPosition();
if (pos == NULL)
{
MessageBox(L"你没有选择要修改的个人\n请在列表中选择要修改的个人!",L"提示",MB_ICONEXCLAMATION);
return ;//没有选择表项则不用做下面的事情
}
else
{
if(!theApp.m_db.CanTransact ())
{
//虽然不支持事务处理的数据库很少,但是我强硬地不允许这种情况出现
MessageBox(L"\n系统不支持事务处理,修改不成功!\n",L"错误", MB_ICONSTOP);
return ;
}
int nItem = m_cPersonListCtrl.GetNextSelectedItem(pos);//列表中的项目序号也即数据库中的编号
//下面获取该表项的数据,用于在修改对话框中显示
int Id=_ttol(m_cPersonListCtrl.GetItemText (nItem,6));
if(m_pSet.IsOpen ())
m_pSet.Close ();
m_pSet.Open ();
m_pSet.m_strFilter .Empty ();
m_pSet.MoveFirst ();
while(!m_pSet.IsEOF ())
{
if(m_pSet.m_pId ==Id)//说明找到了相应的那个字段
{
m_iSocialisePerson_Id=m_pSet.m_pId ;
m_sSocialisePerson_Name=m_pSet.m_pName ;
m_sSocialisePerson_NickName=m_pSet.m_pNickName ;
m_sSocialisePerson_Sex=m_pSet.m_pSex ;
m_tSocialisePerson_Birthday=m_pSet.m_pBirthday;
m_sSocialisePerson_Phone=m_pSet.m_pPhone ;
m_sSocialisePerson_Fax=m_pSet.m_pFax ;
m_sSocialisePerson_Address=m_pSet.m_pAddress ;
m_sSocialisePerson_Code=m_pSet.m_pCode ;
m_sSocialisePerson_Email=m_pSet.m_pEmail ;
m_sSocialisePerson_MSN=m_pSet.m_pMSN ;
m_sSocialisePerson_Web=m_pSet.m_pWeb ;
m_sSocialisePerson_QQ=m_pSet.m_pQQ ;
m_sSocialisePerson_Group=m_pSet.m_pGroup ;
m_sSocialisePerson_Uint=m_pSet.m_pUint ;
m_sSocialisePerson_Remark=m_pSet.m_pRemark ;
break;
}
m_pSet.MoveNext ();
}
m_pSet.Close ();//记得要关闭!
//下面弹出修改对话框,初始化数据由上面部分提供
CSocialisePersonUpdateDlg dlg;
if(dlg.DoModal ()!=IDOK)
{
return ;//万一用户没有输入正确信息,则退出,不给修改
}
theApp.m_db.BeginTrans ();//开始事务处理
TRY
{
m_pSet.Open (AFX_DB_USE_DEFAULT_TYPE,L"Select * from Person order by pName");
m_pSet.m_strFilter.Empty ();
m_pSet.MoveFirst ();//刚开始移动到第一个位置
while(!m_pSet.IsEOF ())
{
if(m_pSet.m_pId ==m_iSocialisePerson_Id)
{
m_pSet.Edit ();
m_pSet.m_pName =dlg.m_sName ;
m_pSet.m_pNickName =dlg.m_sNickName ;
m_pSet.m_pSex =dlg.m_sSex ;
m_pSet.m_pBirthday =dlg.m_tBirthday ;
m_pSet.m_pPhone =dlg.m_sPhone ;
m_pSet.m_pFax =dlg.m_sFax ;
m_pSet.m_pAddress =dlg.m_sAddress ;
m_pSet.m_pCode =dlg.m_sCode ;
m_pSet.m_pEmail =dlg.m_sEmail ;
m_pSet.m_pMSN =dlg.m_sMSN ;
m_pSet.m_pWeb =dlg.m_sWeb ;
m_pSet.m_pQQ =dlg.m_sQQ ;
m_pSet.m_pGroup =dlg.m_sGroup ;
m_pSet.m_pUint =dlg.m_sUint ;
m_pSet.m_pRemark =dlg.m_sRemark ;
m_pSet.Update ();
m_pSet.Requery ();
m_pSet.MoveNext ();
break;
}
m_pSet.MoveNext ();
}
m_pSet.Close ();
theApp.m_db.CommitTrans ();//修改成功了
}
CATCH_ALL(e)
{
theApp.m_db.Rollback ();
MessageBox(L"\n事务处理出错,修改失败!\n",L"错误", MB_ICONSTOP);
return;
}
END_CATCH_ALL
//修改成功了,就要把新的在列表中显示出来,同时去掉旧的
//先去掉旧的
m_cPersonListCtrl.DeleteItem (nItem);
//再添加新的
m_cPersonListCtrl.InsertItem(nItem,dlg.m_sName ,0);
m_cPersonListCtrl.SetItemText (nItem,1,FormatDate(dlg.m_tBirthday) );
m_cPersonListCtrl.SetItemText (nItem,2,dlg.m_sPhone );
m_cPersonListCtrl.SetItemText (nItem,3,dlg.m_sQQ );
m_cPersonListCtrl.SetItemText (nItem,4,dlg.m_sMSN );
m_cPersonListCtrl.SetItemText (nItem,5,dlg.m_sEmail );
s.Format (L"%d",m_iSocialisePerson_Id );
m_cPersonListCtrl.SetItemText (nItem,6,s);
}
}
void CSocialisePersonDlg::OnBnClickedSocialisePersonUpdateBt()
{
// TODO: 在此添加控件通知处理程序代码
Update();
}
void CSocialisePersonDlg::Delete ()
{
POSITION pos = m_cPersonListCtrl.GetFirstSelectedItemPosition();
if (pos == NULL)
{
MessageBox(L"\n你没有选中任何一个个人,不可以删除!\n",L"提示",MB_ICONEXCLAMATION);
}
else
{
if(MessageBox(L"\n此操作将永久删除该项,确定吗?\n",L"删除提醒",MB_OKCANCEL|MB_ICONQUESTION|MB_DEFBUTTON2)==IDCANCEL)
return ;
while (pos)
{
int nItem = m_cPersonListCtrl.GetNextSelectedItem(pos);
CString str;
str="Select * from Person order by pName";
m_pSet.Open (AFX_DB_USE_DEFAULT_TYPE,str);
//获取该单词
int m_iId;
m_iId=_ttol(m_cPersonListCtrl.GetItemText (nItem,6));
m_pSet.m_strFilter.Empty ();
m_pSet.MoveFirst ();//刚开始移动到第一个位置
while(!m_pSet.IsEOF ())
{
if(m_iId ==m_pSet.m_pId )
{
m_pSet.Delete ();
break;
}
m_pSet.MoveNext ();
}
if(!m_pSet.IsDeleted ())
{
MessageBox(L"\n删除失败!\n",L"错误", MB_ICONSTOP);
return ;
}
m_pSet.MoveNext ();
//数据库删除成功后就删除列表了
m_cPersonListCtrl.DeleteItem (nItem);
m_pSet.Close ();
//现在在右边显示个数
m_iPersonAmount--;
CString s;
s.Format (L"%d",m_iPersonAmount);
SetDlgItemText(IDC_PERSON_AMOUNT_STATIC,s);//显示
}
}
}
void CSocialisePersonDlg::OnBnClickedSocialisePersonDeleteBt()
{
// TODO: 在此添加控件通知处理程序代码
Delete();
}
void CSocialisePersonDlg::Search ()
{
CString s;
int i=0;
CSocialisePersonSearchDlg dlg;
if(dlg.DoModal ()==IDOK)
{
BeginWaitCursor();
m_cPersonListCtrl.DeleteAllItems ();
if(m_pSet.IsOpen ())
m_pSet.Close ();
CString str;
str="Select * from Person where ";
switch(dlg.m_iItem )
{
case 0: str+="pName"; break;
case 1: str+="pNickName"; break;
case 2: str+="pBirthday"; break;
case 3: str+="pPhone"; break;
case 4: str+="pFax";break;
case 5: str+="pAddress"; break;
case 6: str+="pEmail"; break;
case 7: str+="pMSN"; break;
case 8: str+="pWeb"; break;
case 9: str+="pQQ"; break;
case 10: str+="pGroup";break;
case 11: str+="pUint"; break;
}
str+=" Like '%";
str+=dlg.m_sKeyWord ;
str+="%' order by pName";
m_pSet.Open (AFX_DB_USE_DEFAULT_TYPE,str);
m_pSet.m_strFilter .Empty ();
m_pSet.Requery ();
if(m_pSet.IsEOF ())
{
m_iPersonAmount=i;
s.Format (L"%d",m_iPersonAmount);
SetDlgItemText(IDC_PERSON_AMOUNT_STATIC,s);//显示
MessageBox(L"\n不存在这样的记录!\n",L"提示",MB_ICONEXCLAMATION);
m_pSet.Close ();
EndWaitCursor();
return ;
}
else
{
if(m_pSet.GetRecordCount ()==0)
{
m_iPersonAmount=i;
s.Format (L"%d",m_iPersonAmount);
SetDlgItemText(IDC_PERSON_AMOUNT_STATIC,s);//显示
MessageBox(L"\n不存在这样的记录!\n",L"提示",MB_ICONEXCLAMATION);
m_pSet.Close ();
EndWaitCursor();
return ;
}
m_pSet.MoveFirst ();
while (!m_pSet.IsEOF ())
{
m_cPersonListCtrl.InsertItem(i,(CString)m_pSet.m_pName ,0);
m_cPersonListCtrl.SetItemText (i,1,FormatDate(m_pSet.m_pBirthday));
m_cPersonListCtrl.SetItemText (i,2,(CString)m_pSet.m_pPhone );
m_cPersonListCtrl.SetItemText (i,3,(CString)m_pSet.m_pQQ );
m_cPersonListCtrl.SetItemText (i,4,(CString)m_pSet.m_pMSN );
m_cPersonListCtrl.SetItemText (i,5,(CString)m_pSet.m_pEmail );
s.Format (L"%d",m_pSet.m_pId );
m_cPersonListCtrl.SetItemText (i,6,s);
i++;
m_pSet.MoveNext ();
}
m_pSet.Close ();
//现在在右边显示个数
if(i==0)
{
m_iPersonAmount=i;
s.Format (L"%d",m_iPersonAmount);
SetDlgItemText(IDC_PERSON_AMOUNT_STATIC,s);//显示
MessageBox(L"\n不存在这样的记录!\n",L"提示",MB_ICONEXCLAMATION);
m_pSet.Close ();
EndWaitCursor();
return ;
}
m_iPersonAmount=i;
s.Format (L"%d",m_iPersonAmount);
SetDlgItemText(IDC_PERSON_AMOUNT_STATIC,s);//显示
}
EndWaitCursor();
}
}
void CSocialisePersonDlg::OnBnClickedSocialisePersonSearchBt()
{
// TODO: 在此添加控件通知处理程序代码
Search();
}
BOOL CSocialisePersonDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
SetTimer(1,100,NULL);
m_cPersonListCtrl.SetExtendedStyle (LVS_EX_FULLROWSELECT|LVS_EX_GRIDLINES);
m_cPersonListCtrl.InsertColumn (0,L"名称",LVCFMT_IMAGE|LVCFMT_LEFT);
m_cPersonListCtrl.SetColumnWidth (0,75);
m_cPersonListCtrl.InsertColumn (1,L"生日");
m_cPersonListCtrl.SetColumnWidth (1,75);
m_cPersonListCtrl.InsertColumn (2,L"电话");
m_cPersonListCtrl.SetColumnWidth (2,75);
m_cPersonListCtrl.InsertColumn (3,L"QQ");
m_cPersonListCtrl.SetColumnWidth (3,75);
m_cPersonListCtrl.InsertColumn (4,L"MSN");
m_cPersonListCtrl.SetColumnWidth (4,75);
m_cPersonListCtrl.InsertColumn (5,L"Email");
m_cPersonListCtrl.SetColumnWidth (5,125);
m_cPersonListCtrl.InsertColumn (6,L"主键");
m_cPersonListCtrl.SetColumnWidth (6,0);
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CSocialisePersonDlg::Print ()
{
if(m_cPersonListCtrl.GetItemCount()<= 0)
{
MessageBox(L"\n打印列表为空!\n",L"提示",MB_ICONEXCLAMATION);
return;
}
PRNINFO PrnInfo = {0};
PrnInfo.hListView = m_cPersonListCtrl.m_hWnd;
PrnInfo.hWnd = this->m_hWnd;
PrnInfo.IsPrint = FALSE;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -