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

数据库系统

开发平台:

Visual C++

  1. // UseState.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Equipment.h"
  5. #include "UseState.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. extern CEquipmentApp theApp;
  12. /////////////////////////////////////////////////////////////////////////////
  13. // CUseState dialog
  14. CUseState::CUseState(CWnd* pParent /*=NULL*/)
  15. : CDialog(CUseState::IDD, pParent)
  16. {
  17. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINTITLE);
  18. }
  19. void CUseState::DoDataExchange(CDataExchange* pDX)
  20. {
  21. CDialog::DoDataExchange(pDX);
  22. //{{AFX_DATA_MAP(CUseState)
  23. DDX_Control(pDX, IDC_EDIT1, kind);
  24. DDX_Control(pDX, IDC_LIST, list);
  25. //}}AFX_DATA_MAP
  26. }
  27. BEGIN_MESSAGE_MAP(CUseState, CDialog)
  28. //{{AFX_MSG_MAP(CUseState)
  29. ON_NOTIFY(NM_CLICK, IDC_LIST, OnClickList)
  30. ON_COMMAND(ID_BUTTONADD, OnButtonadd)
  31. ON_COMMAND(ID_BUTTONCLOSE, OnButtonclose)
  32. ON_COMMAND(ID_BUTTONDELETE, OnButtondelete)
  33. ON_COMMAND(ID_BUTTONREFRESH, OnButtonrefresh)
  34. ON_COMMAND(ID_BUTTONUPDATE, OnButtonupdate)
  35. //}}AFX_MSG_MAP
  36. END_MESSAGE_MAP()
  37. /////////////////////////////////////////////////////////////////////////////
  38. // CUseState message handlers
  39. void CUseState::OnOK() 
  40. {
  41. // TODO: Add extra validation here
  42. }
  43. void CUseState::OnCancel() 
  44. {
  45. // TODO: Add extra cleanup here
  46. CDialog::OnCancel();
  47. }
  48. BOOL CUseState::OnInitDialog() 
  49. {
  50. CDialog::OnInitDialog();
  51. SetIcon(m_hIcon,true);
  52. toolbar.Create(TBSTYLE_FLAT|CCS_TOP|WS_CHILD|WS_VISIBLE|WS_BORDER|CCS_ADJUSTABLE|TBSTYLE_WRAPABLE,CRect(0,0,0,0),this,IDR_TOOLBAR2);
  53. toolbar.SetBitmapSize(CSize(32,32));
  54. imagelist.Create(32,32,ILC_COLOR32|ILC_MASK,0,0);
  55. for (int n =0;n<5;n++)
  56. {
  57. imagelist.Add(theApp.LoadIcon(n+IDI_ICON1));
  58. }
  59. toolbar.SetImageList(&imagelist);
  60. TBBUTTON  buttons[6];
  61. for (int i =0; i<6;i++)
  62. {
  63. CString str;
  64. int strlength;
  65. CCHAR *temp;
  66. str.LoadString(ID_BUTTONADD+i-1);//第一个按钮为分隔条
  67. strlength = str.GetLength()+1;
  68. temp = str.GetBufferSetLength(strlength);
  69. temp[strlength]= '';
  70. temp[strlength-1]= '';
  71. if (i<1)
  72. buttons[i].fsStyle =TBSTYLE_SEP;
  73. else
  74. {
  75. buttons[i].fsStyle =TBSTYLE_BUTTON;
  76. }
  77. buttons[i].fsState =TBSTATE_ENABLED;
  78. buttons[i].dwData =0;
  79. buttons[i].idCommand = ID_BUTTONADD+i-1;
  80. buttons[i].iString = toolbar.AddStrings(temp);
  81. buttons[i].iBitmap = i-1;
  82. str.ReleaseBuffer();
  83. }
  84. toolbar.AutoSize();
  85. toolbar.AddButtons(6,buttons);
  86. toolbar.ShowWindow(SW_SHOW);
  87. list.SetExtendedStyle(LVS_REPORT|LVS_EX_FULLROWSELECT|LVS_OWNERDRAWFIXED |LVS_EX_FLATSB|LVS_SHOWSELALWAYS|LVS_EX_HEADERDRAGDROP);
  88. list.InsertColumn(1,"资产使用状况");//向列表中添加列
  89. list.SetColumnWidth(0,340);//设置列宽度
  90. OnButtonrefresh();
  91. return TRUE;  
  92. }
  93. bool CUseState::IsRepeated(CString str)
  94. {
  95. CString sql;
  96. sql.Format("select * from tb_usestate where usestate = '%s'",str);
  97. theApp.datamanage->Record1->raw_Close();
  98. theApp.datamanage->Record1->Open((_bstr_t)sql,theApp.datamanage->GetConn().GetInterfacePtr(),adOpenKeyset,adLockOptimistic,adCmdText);
  99. if(theApp.datamanage->Record1->RecordCount>0)
  100. return true;
  101. return false;
  102. }
  103. void CUseState::OnClickList(NMHDR* pNMHDR, LRESULT* pResult) 
  104. {
  105. int currentrow = list.GetSelectionMark();//得到当前行
  106. if (currentrow!=-1)
  107. {
  108. kind.SetWindowText(list.GetItemText(currentrow,0));
  109. }
  110. else
  111. kind.SetWindowText("");
  112. *pResult = 0;
  113. }
  114. void CUseState::OnButtonadd() 
  115. {
  116. CString str;
  117. kind.GetWindowText(str);
  118. if (!str.IsEmpty())
  119. {
  120. if (!IsRepeated(str))
  121. {
  122. try
  123. {
  124. CString sql;
  125. sql.Format("insert into tb_usestate values ('%s')",str);
  126. theApp.datamanage->ExecSQL(sql);
  127. MessageBox("操作成功.","提示",64);
  128. kind.SetWindowText("");
  129. kind.SetFocus();
  130. }
  131. catch(...)
  132. {
  133. MessageBox("操作失败.","提示",64);
  134. kind.SetFocus();
  135. kind.SetSel(0);
  136. }
  137. OnButtonrefresh() ;
  138. }
  139. else
  140. MessageBox("该类别已存在.","提示",64);
  141. }
  142. else
  143. AfxMessageBox("基础信息不能为空.",0,0);
  144. }
  145. void CUseState::OnButtonclose() 
  146. {
  147. EndDialog(01);
  148. }
  149. void CUseState::OnButtondelete() 
  150. {
  151. if(MessageBox("确实要删除当前记录吗?","提示",MB_YESNO)==IDYES)
  152. if (theApp.datamanage->GetRecordset()->RecordCount>0)
  153. {
  154. int currentrow = list.GetSelectionMark();//得到当前行
  155. if (currentrow !=-1)
  156. {
  157. CString sql,olddata;
  158. olddata = list.GetItemText(currentrow,0);
  159. sql.Format("delete tb_usestate where usestate = '%s'",olddata);
  160. try
  161. {
  162. theApp.datamanage->ExecSQL(sql);
  163. MessageBox("操作成功.","提示",64);
  164. }
  165. catch(...)
  166. {
  167. MessageBox("操作失败.","提示",64);
  168. }
  169. OnButtonrefresh() ;
  170. }
  171. else
  172. {
  173. MessageBox("请在表格中选择欲删除的数据.","提示",64);
  174. }
  175. }
  176. else
  177. MessageBox("当前没有可删除的记录.","提示");
  178. }
  179. void CUseState::OnButtonrefresh() 
  180. {
  181. kind.SetWindowText("");
  182. list.DeleteAllItems();
  183. theApp.datamanage->GetRecordset()->raw_Close();
  184. theApp.datamanage->GetRecordset()->Open("select usestate as 使用状况 from tb_usestate",theApp.datamanage->GetConn().GetInterfacePtr(),adOpenKeyset,adLockPessimistic,adCmdText);
  185. if (theApp.datamanage->GetRecordset()->RecordCount >0)
  186. {
  187. while (! theApp.datamanage->GetRecordset()->ADOEOF)
  188. {
  189. CString str;
  190. _variant_t temp;
  191. temp =theApp.datamanage->GetRecordset()->GetCollect((long)0);
  192. str = temp.bstrVal;
  193. list.InsertItem(0,str,0);
  194. theApp.datamanage->GetRecordset()->MoveNext();
  195. }
  196. }
  197. }
  198. void CUseState::OnButtonupdate() 
  199. {
  200. if (theApp.datamanage->GetRecordset()->RecordCount >0)
  201. {
  202. if(MessageBox("确实要修改当前数据吗?","提示",MB_YESNO)==IDYES )
  203. {
  204. CString str;
  205. kind.GetWindowText(str);
  206. if (str.IsEmpty()) //判断能力基础信息是否为空
  207. {
  208. MessageBox("基础信息不能为空","提示",64);
  209. }
  210. else if (!IsRepeated(str))
  211. {
  212. int currentrow = list.GetSelectionMark();//得到当前行
  213. if (currentrow!=-1)
  214. {
  215. try
  216. {
  217. CString sql,olddata;
  218. olddata = list.GetItemText(currentrow,0);
  219. sql.Format("update tb_usestate set usestate = '%s' where usestate = '%s'",str,olddata);
  220. theApp.datamanage->ExecSQL(sql);
  221. MessageBox("操作成功.","提示",64);
  222. kind.SetWindowText("");
  223. kind.SetFocus();
  224. }
  225. catch(...)
  226. {
  227. MessageBox("操作失败.","提示",64);
  228. }
  229. OnButtonrefresh() ;
  230. }
  231. else
  232. {
  233. MessageBox("当前没有可修改的数据.","提示",64);
  234. }
  235. }
  236. else
  237. MessageBox("该类别已经存在.","提示",64);
  238. }
  239. }
  240. else
  241. MessageBox("当前没有可修改的数据.","提示",64);
  242. }