EquipKind.cpp
上传用户:latoyin
上传日期:2017-10-19
资源大小:2882k
文件大小:6k
源码类别:

数据库系统

开发平台:

Visual C++

  1. // EquipKind.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Equipment.h"
  5. #include "EquipKind.h"
  6. #include "Equipment.h"
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12. extern CEquipmentApp theApp;
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CEquipKind dialog
  15. CEquipKind::CEquipKind(CWnd* pParent /*=NULL*/)
  16. : CDialog(CEquipKind::IDD, pParent)
  17. {
  18. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINTITLE);
  19. }
  20. bool CEquipKind::IsRepeated(CString str)
  21. {
  22. CString sql;
  23. sql.Format("select * from tb_kinds where kinds = '%s'",str);
  24. theApp.datamanage->Record1->raw_Close();
  25. theApp.datamanage->Record1->Open((_bstr_t)sql,theApp.datamanage->GetConn().GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
  26. if(theApp.datamanage->Record1->RecordCount>0)
  27. return true;
  28. return false;
  29. }
  30. void CEquipKind::DoDataExchange(CDataExchange* pDX)
  31. {
  32. CDialog::DoDataExchange(pDX);
  33. //{{AFX_DATA_MAP(CEquipKind)
  34. DDX_Control(pDX, IDC_EDIT1, kind);
  35. DDX_Control(pDX, IDC_DATAGRID, DBgrid1);
  36. //}}AFX_DATA_MAP
  37. }
  38. BEGIN_MESSAGE_MAP(CEquipKind, CDialog)
  39. //{{AFX_MSG_MAP(CEquipKind)
  40. ON_WM_SHOWWINDOW()
  41. ON_WM_PAINT()
  42. ON_COMMAND(ID_BUTTONADD, OnButtonadd)
  43. ON_COMMAND(ID_BUTTONREFRESH, OnButtonrefresh)
  44. ON_COMMAND(ID_BUTTONUPDATE, OnButtonupdate)
  45. ON_COMMAND(ID_BUTTONDELETE, OnButtondelete)
  46. ON_COMMAND(ID_BUTTONCLOSE, OnButtonclose)
  47. //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49. /////////////////////////////////////////////////////////////////////////////
  50. // CEquipKind message handlers
  51. void CEquipKind::OnOK() 
  52. {
  53. // TODO: Add extra validation here
  54. //CDialog::OnOK();
  55. }
  56. void CEquipKind::OnShowWindow(BOOL bShow, UINT nStatus) 
  57. {
  58. CDialog::OnShowWindow(bShow, nStatus);
  59. OnButtonrefresh(); //调用刷新按钮的单击事件
  60. // TODO: Add your message handler code here
  61. }
  62. BEGIN_EVENTSINK_MAP(CEquipKind, CDialog)
  63.     //{{AFX_EVENTSINK_MAP(CEquipKind)
  64. ON_EVENT(CEquipKind, IDC_DATAGRID, -600 /* Click */, OnClickDatagrid, VTS_NONE)
  65. //}}AFX_EVENTSINK_MAP
  66. END_EVENTSINK_MAP()
  67. BOOL CEquipKind::OnInitDialog() 
  68. {
  69. CDialog::OnInitDialog();
  70. //PostMessage(WM_SETICON,ICON_SMALL,(LPARAM)theApp.LoadIcon(IDR_MAINTITLE));
  71. SetIcon(theApp.LoadIcon(IDR_MAINTITLE), TRUE); //设置窗口图标
  72. toolbar.Create(TBSTYLE_FLAT|CCS_TOP|WS_CHILD|WS_VISIBLE|WS_BORDER|CCS_ADJUSTABLE|TBSTYLE_WRAPABLE,CRect(0,0,0,0),this,IDR_TOOLBAR2);
  73. toolbar.SetBitmapSize(CSize(32,32));
  74. imagelist.Create(32,32,ILC_COLOR32|ILC_MASK,0,0);
  75. for (int n =0;n<5;n++)
  76. {
  77. imagelist.Add(theApp.LoadIcon(n+IDI_ICON1));
  78. }
  79. toolbar.SetImageList(&imagelist);
  80. TBBUTTON  buttons[6];
  81. for (int i =0; i<6;i++)
  82. {
  83. CString str;
  84. int strlength;
  85. CCHAR *temp;
  86. str.LoadString(ID_BUTTONADD+i-1);//第一个按钮为分隔条
  87. strlength = str.GetLength()+1;
  88. temp = str.GetBufferSetLength(strlength);
  89. temp[strlength]= '';
  90. temp[strlength-1]= '';
  91. if (i<1)
  92. buttons[i].fsStyle =TBSTYLE_SEP;
  93. else
  94. {
  95. buttons[i].fsStyle =TBSTYLE_BUTTON;
  96. }
  97. buttons[i].fsState =TBSTATE_ENABLED;
  98. buttons[i].dwData =0;
  99. buttons[i].idCommand = ID_BUTTONADD+i-1;
  100. buttons[i].iString = toolbar.AddStrings(temp);
  101. buttons[i].iBitmap = i-1;
  102. str.ReleaseBuffer();
  103. }
  104. toolbar.AutoSize();
  105. toolbar.AddButtons(6,buttons);
  106. toolbar.ShowWindow(SW_SHOW);
  107. return TRUE;   
  108. }
  109. void CEquipKind::OnPaint() 
  110. {
  111. CPaintDC dc(this); // device context for painting
  112. // Do not call CDialog::OnPaint() for painting messages
  113. }
  114. void CEquipKind::OnClickDatagrid() 
  115. {
  116. CString str;
  117. _variant_t temp;
  118. if ((!theApp.datamanage->GetRecordset()->ADOEOF)&&(!theApp.datamanage->GetRecordset()->BOF))
  119. {
  120. temp = theApp.datamanage->GetRecordset()->GetFields()->GetItem((long)0)->Value;
  121. str = temp.bstrVal;
  122. kind.SetWindowText(str);
  123. }
  124. }
  125. void CEquipKind::OnButtonadd() 
  126. {
  127. CString str;
  128. kind.GetWindowText(str);
  129. if (!str.IsEmpty())
  130. {
  131. if (!IsRepeated(str))
  132. {
  133. try
  134. {
  135. CString sql;
  136. sql.Format("insert into tb_kinds values ('%s')",str);
  137. theApp.datamanage->ExecSQL(sql);
  138. this->MessageBox("操作成功.","提示",64);
  139. kind.SetWindowText("");
  140. kind.SetFocus();
  141. }
  142. catch(...)
  143. {
  144. this->MessageBox("操作失败.","提示",64);
  145. kind.SetFocus();
  146. kind.SetSel(0);
  147. }
  148. OnButtonrefresh();
  149. }
  150. else
  151. MessageBox("该类别已存在.","提示",64);
  152. }
  153. else
  154. AfxMessageBox("基础信息不能为空.",0,0);
  155. }
  156. void CEquipKind::OnButtonrefresh() 
  157. {
  158. kind.SetWindowText(""); //清空编辑框文本
  159. DBgrid1.SetRefDataSource(NULL);
  160. theApp.datamanage->GetRecordset()->raw_Close();
  161. theApp.datamanage->GetRecordset()->Open("select kinds as 资产类别 from tb_kinds",theApp.datamanage->GetConn().GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
  162. if(theApp.datamanage->GetRecordset()->RecordCount>0)
  163. DBgrid1.SetRefDataSource(theApp.datamanage->GetRecordset()->DataSource);
  164. }
  165. void CEquipKind::OnButtonupdate() 
  166. {
  167. if (theApp.datamanage->GetRecordset()->RecordCount >0)
  168. {
  169. if(MessageBox("确实要修改当前数据吗?","提示",MB_YESNO)==IDYES )
  170. {
  171. CString str;
  172. kind.GetWindowText(str);
  173. if (str.IsEmpty()) //判断基础信息是否为空
  174. {
  175. MessageBox("基础信息不能为空","提示",64);
  176. }
  177. else if (!IsRepeated(str))
  178. {
  179. try
  180. {
  181. _bstr_t temp= str;
  182. theApp.datamanage->GetRecordset()->GetFields()->GetItem((long)0)->Value =temp;
  183. theApp.datamanage->GetRecordset()->Update();
  184. this->MessageBox("操作成功.","提示",64);
  185. kind.SetWindowText("");
  186. kind.SetFocus();
  187. }
  188. catch(...)
  189. {
  190. MessageBox("操作失败.","提示",64);
  191. }
  192. OnButtonrefresh();
  193. }
  194. else
  195. MessageBox("该类别已经存在.","提示",64);
  196. }
  197. }
  198. else
  199. MessageBox("当前没有可修改的数据.","提示",64);
  200. }
  201. void CEquipKind::OnButtondelete() 
  202. {
  203. if(MessageBox("确实要删除当前记录吗?","提示",MB_YESNO)==IDYES)
  204. if (theApp.datamanage->GetRecordset()->RecordCount>0)
  205. {
  206. CString i ;
  207. kind.GetWindowText(i);
  208. if (! i.IsEmpty() )
  209. {
  210. CString sql;
  211. theApp.datamanage->GetRecordset()->raw_Close();
  212. sql.Format("delete tb_kinds where kinds = '%s'",i);
  213. theApp.datamanage->GetConn()->Execute((_bstr_t)sql,NULL,0);
  214. OnButtonrefresh();
  215. }
  216. else
  217. MessageBox("请在编辑框中输入信息或在表格中选择信息","提示");
  218. }
  219. else
  220. MessageBox("当前没有可删除的记录.","提示");
  221. }
  222. void CEquipKind::OnButtonclose() 
  223. {
  224. EndDialog(0);
  225. }