FrmLAddEdit.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using DevComponents.DotNetBar;
  10. using DevComponents.DotNetBar.Layout;
  11. namespace WCS_Client.UC
  12. {
  13. public partial class FrmLAddEdit : Office2007Form
  14. {
  15. public FrmLAddEdit()
  16. {
  17. InitializeComponent();
  18. ErrItem.BlinkStyle = ErrorBlinkStyle.BlinkIfDifferentError;
  19. }
  20. ErrorProvider ErrItem = new ErrorProvider();
  21. internal List<LayoutControlItem> _LCItemList = new List<LayoutControlItem>();
  22. public delegate DataRow GetDrHandler(int StepQty);
  23. GetDrHandler _GetDrMethods = null;
  24. public delegate DataRow EditDataHandler(DataRow dr);
  25. EditDataHandler _EditDataMethods = null;
  26. public delegate bool SaveDataHandler(DataRow dr);
  27. SaveDataHandler _SaveDataMethods = null;
  28. public delegate void LCItemEditHandler(FrmLAddEdit frm);
  29. LCItemEditHandler _LCItemEditMethods = null;
  30. public delegate void LCItemNewHandler(FrmLAddEdit frm);
  31. LCItemNewHandler _LCItemNewMethods = null;
  32. public void SetLCItemErr(LayoutControlItem LCItem, string ErrText)
  33. {
  34. if (LCItem == null || LCItem.Control == null)
  35. {
  36. return;
  37. }
  38. if (!string.IsNullOrEmpty(ErrText))
  39. {
  40. LCItem.Control.Padding = new System.Windows.Forms.Padding(0, 0, 15, 0);
  41. LCItem.Control.Margin = new System.Windows.Forms.Padding(0, 0, 15, 0);
  42. }
  43. else
  44. {
  45. LCItem.Control.Padding = new System.Windows.Forms.Padding(0, 0, 0, 0);
  46. LCItem.Control.Margin = new System.Windows.Forms.Padding(0, 0, 0, 0);
  47. }
  48. ErrItem.SetError(LCItem.Control, ErrText);
  49. }
  50. public void ClearLCItemErr()
  51. {
  52. ErrItem.Clear();
  53. }
  54. public void SetLCItemErr(string LCName, string ErrText)
  55. {
  56. LayoutControlItem LCItem = GetLCItem(LCName);
  57. SetLCItemErr(LCItem, ErrText);
  58. }
  59. public List<LayoutControlItem> GetLCItemList()
  60. {
  61. return _LCItemList;
  62. }
  63. public LayoutControlItem GetLCItem(string LCName)
  64. {
  65. return _LCItemList.Find(a => a.Name == LCName);
  66. }
  67. public void ClearLCItemValue()
  68. {
  69. LCItemUtil.ClearValue_LCItem(_LCItemList);
  70. }
  71. public void LCItemIsEnable(string LCName, bool IsEnable)
  72. {
  73. LayoutControlItem item = GetLCItem(LCName);
  74. if (item == null || item.Control == null)
  75. {
  76. return;
  77. }
  78. item.Control.Enabled = IsEnable;
  79. }
  80. public void LCItemIsEnable(bool IsEnable)
  81. {
  82. foreach (LayoutControlItem item in _LCItemList)
  83. {
  84. if (item == null || item.Control == null)
  85. {
  86. continue;
  87. }
  88. item.Control.Enabled = IsEnable;
  89. }
  90. }
  91. public DataRow GetData()
  92. {
  93. DataTable dt = new DataTable();
  94. foreach (LayoutControlItem Item in _LCItemList)
  95. {
  96. dt.Columns.Add(Item.Name, typeof(object));
  97. }
  98. DataRow dr = dt.NewRow();
  99. foreach (LayoutControlItem Item in _LCItemList)
  100. {
  101. dr[Item.Name] = LCItemUtil.GetValue_LCItem(Item);
  102. }
  103. return dr;
  104. }
  105. public void SetLCItemValue(DataRow dr)
  106. {
  107. foreach (LayoutControlItem Item in _LCItemList)
  108. {
  109. LCItemUtil.SetValue_LCItem(Item, dr[Item.Name]);
  110. }
  111. }
  112. public void InitFrm(bool IsBtnNew, bool IsBtnEdit, List<LayoutControlItem> LCItemList, GetDrHandler GetDrMethods, LCItemNewHandler LCItemNewMethods, LCItemEditHandler LCItemEditMethods, EditDataHandler EditDataMethods, SaveDataHandler SaveDataMethods)
  113. {
  114. _GetDrMethods = GetDrMethods;
  115. _EditDataMethods = EditDataMethods;
  116. _SaveDataMethods = SaveDataMethods;
  117. _LCItemNewMethods = LCItemNewMethods;
  118. _LCItemEditMethods = LCItemEditMethods;
  119. this.Btn_New.Visible = IsBtnNew;
  120. this.Btn_Edit.Visible = IsBtnEdit;
  121. this.Btn_Save.Visible = (IsBtnNew || IsBtnEdit);
  122. _LCItemList = LCItemList;
  123. //LayoutControl添加控件
  124. if (_LCItemList != null)
  125. {
  126. foreach (LayoutControlItem LocItem in _LCItemList)
  127. {
  128. this.layoutControl1.Controls.Add(LocItem.Control);
  129. this.layoutControl1.RootGroup.Items.Add(LocItem);
  130. }
  131. }
  132. }
  133. public void FrmEditShowDialog(DataRow dr)
  134. {
  135. try
  136. {
  137. if (dr == null)
  138. {
  139. throw new Exception("数据为空,无法进行编辑!!!");
  140. }
  141. DataRow ndr = _EditDataMethods(dr);
  142. if (ndr == null)
  143. {
  144. throw new Exception("数据为空,无法进行编辑!!!");
  145. }
  146. ClearLCItemValue();
  147. ClearLCItemErr();
  148. _LCItemEditMethods(this);
  149. SetLCItemValue(dr);
  150. this.Btn_Save.Enabled = true;
  151. this.Btn_New.Enabled = false;
  152. this.Btn_Edit.Enabled = false;
  153. this.ShowDialog();
  154. }
  155. catch (Exception ex)
  156. {
  157. MessageUtil.ShowError(ex.Message);
  158. this.Close();
  159. }
  160. }
  161. public void FrmEditShowDialog(DataRow dr,DataTable dt)
  162. {
  163. try
  164. {
  165. if (dr == null)
  166. {
  167. throw new Exception("数据为空,无法进行编辑!!!");
  168. }
  169. DataRow ndr = _EditDataMethods(dr);
  170. if (ndr == null)
  171. {
  172. throw new Exception("数据为空,无法进行编辑!!!");
  173. }
  174. ClearLCItemValue();
  175. ClearLCItemErr();
  176. _LCItemEditMethods(this);
  177. SetLCItemValue(dr);
  178. Editstatus = 1;
  179. dtTable = dt;
  180. this.Btn_Save.Enabled = true;
  181. this.Btn_New.Enabled = false;
  182. this.Btn_Edit.Enabled = false;
  183. this.ShowDialog();
  184. }
  185. catch (Exception ex)
  186. {
  187. MessageUtil.ShowError(ex.Message);
  188. this.Close();
  189. }
  190. }
  191. public void FrmNewShowDialog()
  192. {
  193. try
  194. {
  195. ClearLCItemValue();
  196. ClearLCItemErr();
  197. _LCItemNewMethods(this);
  198. this.Btn_Save.Enabled = true;
  199. this.Btn_New.Enabled = false;
  200. this.Btn_Edit.Enabled = false;
  201. this.ShowDialog();
  202. }
  203. catch (Exception ex)
  204. {
  205. MessageUtil.ShowError(ex.Message);
  206. this.Close();
  207. }
  208. }
  209. public void FrmViewShowDialog(DataRow dr)
  210. {
  211. try
  212. {
  213. if (dr == null)
  214. {
  215. throw new Exception("数据为空,无法进行编辑!!!");
  216. }
  217. ClearLCItemValue();
  218. LCItemIsEnable(false);
  219. ClearLCItemErr();
  220. SetLCItemValue(dr);
  221. this.Btn_Save.Enabled = false;
  222. this.Btn_New.Enabled = true;
  223. this.Btn_Edit.Enabled = true;
  224. this.ShowDialog();
  225. }
  226. catch (Exception ex)
  227. {
  228. MessageUtil.ShowError(ex.Message);
  229. this.Close();
  230. }
  231. }
  232. private void Btn_New_Click(object sender, EventArgs e)
  233. {
  234. try
  235. {
  236. ClearLCItemValue();
  237. ClearLCItemErr();
  238. _LCItemNewMethods(this);
  239. Btn_New.Enabled = false;
  240. Btn_Edit.Enabled = false;
  241. Btn_Save.Enabled = true;
  242. }
  243. catch (Exception ex)
  244. {
  245. MessageUtil.ShowError(ex.Message);
  246. this.Close();
  247. }
  248. }
  249. private void Btn_Close_Click(object sender, EventArgs e)
  250. {
  251. if (IsGiveUpData())
  252. {
  253. this.Close();
  254. }
  255. }
  256. private bool IsGiveUpData()
  257. {
  258. if (Btn_Save.Visible && Btn_Save.Enabled && MessageUtil.ShowYesNoAndWarning("是否放弃修改数据!!!") == DialogResult.No)
  259. {
  260. return false;
  261. }
  262. return true;
  263. }
  264. private void Btn_Edit_Click(object sender, EventArgs e)
  265. {
  266. try
  267. {
  268. DataRow dr = _EditDataMethods(GetData());
  269. if (dr == null)
  270. {
  271. throw new Exception("数据有可能发生变化!!!");
  272. }
  273. ClearLCItemValue();
  274. ClearLCItemErr();
  275. _LCItemEditMethods(this);
  276. SetLCItemValue(dr);
  277. Btn_New.Enabled = false;
  278. Btn_Edit.Enabled = false;
  279. Btn_Save.Enabled = true;
  280. }
  281. catch (Exception ex)
  282. {
  283. MessageUtil.ShowError(ex.Message);
  284. this.Close();
  285. }
  286. }
  287. private void Btn_Down_Click(object sender, EventArgs e)
  288. {
  289. try
  290. {
  291. if (IsGiveUpData())
  292. {
  293. ClearLCItemValue();
  294. ClearLCItemErr();
  295. LCItemIsEnable(false);
  296. DataRow dr = _GetDrMethods(1);
  297. if (dr == null)
  298. {
  299. throw new Exception("数据为空!!!");
  300. }
  301. SetLCItemValue(dr);
  302. Btn_New.Enabled = true;
  303. Btn_Edit.Enabled = true;
  304. Btn_Save.Enabled = false;
  305. }
  306. }
  307. catch (Exception ex)
  308. {
  309. MessageUtil.ShowError(ex.Message);
  310. this.Close();
  311. }
  312. }
  313. private void Btn_Up_Click(object sender, EventArgs e)
  314. {
  315. try
  316. {
  317. if (IsGiveUpData())
  318. {
  319. ClearLCItemValue();
  320. ClearLCItemErr();
  321. LCItemIsEnable(false);
  322. DataRow dr = _GetDrMethods(-1);
  323. if (dr == null)
  324. {
  325. throw new Exception("数据为空!!!");
  326. }
  327. SetLCItemValue(dr);
  328. Btn_New.Enabled = true;
  329. Btn_Edit.Enabled = true;
  330. Btn_Save.Enabled = false;
  331. }
  332. }
  333. catch (Exception ex)
  334. {
  335. MessageUtil.ShowError(ex.Message);
  336. this.Close();
  337. }
  338. }
  339. private void Btn_Save_Click(object sender, EventArgs e)
  340. {
  341. try
  342. {
  343. if (_SaveDataMethods(GetData()))
  344. {
  345. LCItemIsEnable(false);
  346. Btn_New.Enabled = true;
  347. Btn_Edit.Enabled = true;
  348. Btn_Save.Enabled = false;
  349. }
  350. }
  351. catch (Exception ex)
  352. {
  353. MessageUtil.ShowError(ex.Message);
  354. }
  355. }
  356. int Editstatus = 0;
  357. DataTable dtTable = null;
  358. private void FrmLAddEdit_Load(object sender, EventArgs e)
  359. {
  360. if (Editstatus == 1)
  361. {
  362. if (dtTable != null)
  363. SetLCItemValue(dtTable.Rows[0]);
  364. }
  365. }
  366. }
  367. }