FormEditPLC.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Linq;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10. using DevExpress.XtraEditors;
  11. using SIMDP.Model;
  12. using SIMDP.BLL;
  13. using ProjectBase.Data.Logs;
  14. using SIMDP.Util;
  15. using ProjectBase.Data.BaseDAL;
  16. namespace SIMDP
  17. {
  18. public partial class FormEditPLC : DevExpress.XtraEditors.XtraForm
  19. {
  20. /// <summary>
  21. /// 定义PLC对象
  22. /// </summary>
  23. public MoPlcInfo plc;
  24. /// <summary>
  25. /// 定义委托
  26. /// </summary>
  27. public delegate void save();
  28. /// <summary>
  29. /// 定义事件,保存数据成功后刷新FormPLC中的数据
  30. /// </summary>
  31. public event save saveData;
  32. public FormEditPLC(MoPlcInfo plcInfo)
  33. {
  34. InitializeComponent();
  35. BindingSource bs = new BindingSource();
  36. bs.DataSource = SysEnvironment.dirPlcLinkType;
  37. this.lookUp_Type.Properties.DataSource = bs;
  38. this.lookUp_Type.Properties.ValueMember = "Key";
  39. this.lookUp_Type.Properties.DisplayMember = "Value";
  40. this.lookUp_Type.Properties.NullText = "请您选择";
  41. this.btn_Save.Enabled = false;
  42. if (plcInfo != null) //修改功能
  43. {
  44. plc = plcInfo;
  45. txt_Name.Text = plcInfo.PlcName.ToString();
  46. this.lookUp_Type.EditValue = plcInfo.LinkType; //设置默认值
  47. txt_Config.Text = plcInfo.LinkConfig.ToString();
  48. this.btn_Save.Enabled = true;
  49. this.btn_Add.Enabled = false;
  50. }
  51. }
  52. private void btn_Cancel_Click(object sender, EventArgs e)
  53. {
  54. this.Close();
  55. }
  56. private void btn_Save_Click(object sender, EventArgs e)
  57. {
  58. try
  59. {
  60. plc.PlcName = txt_Name.Text;
  61. plc.LinkType = Convert.ToInt64(lookUp_Type.EditValue);
  62. plc.LinkConfig = txt_Config.Text;
  63. bool flag = BLLFactory<BlPlcInfo>.Instance.Update(plc, plc.PlcId);
  64. if (flag)
  65. {
  66. DevExpress.XtraEditors.XtraMessageBox.Show("修改成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  67. saveData();
  68. this.Close();
  69. }
  70. }
  71. catch (Exception ex)
  72. {
  73. DevExpress.XtraEditors.XtraMessageBox.Show("修改失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  74. LogHelper.log.Error(string.Format("修改数据库plc_info出现错误:{0}", ex));
  75. }
  76. }
  77. private void btn_Add_Click(object sender, EventArgs e)
  78. {
  79. try
  80. {
  81. if (string.IsNullOrEmpty(txt_Name.Text) || string.IsNullOrEmpty(txt_Config.Text) || lookUp_Type.Text == "请您选择")
  82. {
  83. DevExpress.XtraEditors.XtraMessageBox.Show("请将信息填写完整。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  84. return;
  85. }
  86. MoPlcInfo plc = new MoPlcInfo();
  87. plc.PlcName = txt_Name.Text;
  88. plc.LinkType = Convert.ToInt64(lookUp_Type.EditValue);
  89. plc.LinkConfig = txt_Config.Text;
  90. //BlPlcInfo bl = new BlPlcInfo();
  91. bool flag = BLLFactory<BlPlcInfo>.Instance.Insert(plc);
  92. if (flag)
  93. {
  94. DevExpress.XtraEditors.XtraMessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  95. saveData();
  96. this.Close();
  97. }
  98. }
  99. catch (Exception ex)
  100. {
  101. DevExpress.XtraEditors.XtraMessageBox.Show("添加失败。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  102. LogHelper.log.Error(string.Format("插入数据库plc_info出现错误:{0}", ex));
  103. }
  104. }
  105. }
  106. }