TargetEditor.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. using DevExpress.XtraBars.Customization;
  2. using DevExpress.XtraEditors;
  3. using ExtensionsDev;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.ComponentModel;
  7. using System.Data;
  8. using System.Data.Entity;
  9. using System.Drawing;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Documents;
  14. using System.Windows.Forms;
  15. using XdCxRhDW.Entity;
  16. using XdCxRhDW.Repostory;
  17. namespace XdCxRhDW.App.EditForms
  18. {
  19. public partial class TargetEditor : DevExpress.XtraEditors.XtraForm
  20. {
  21. public TargetInfo info;
  22. List<TargetInfo> infos;
  23. public TargetEditor()
  24. {
  25. InitializeComponent();
  26. this.layoutControl1.UseDefault();
  27. this.Text = "添加目标";
  28. info = new TargetInfo();
  29. this.StartPosition = FormStartPosition.CenterParent;
  30. }
  31. public TargetEditor(TargetInfo info)
  32. : this()
  33. {
  34. this.Text = "编辑目标";
  35. this.info = info;
  36. }
  37. private async void TargetEditor_Load(object sender, EventArgs e)
  38. {
  39. if (this.Text == "编辑目标" && info != null)
  40. {
  41. this.txtTarName.Text = info.TargetName;
  42. var clolrRes = ColorTranslator.FromHtml(info.TargeColor);
  43. this.txtTarColor.EditValue = clolrRes;
  44. }
  45. infos = new List<TargetInfo>();
  46. using (RHDWContext db = new RHDWContext())
  47. {
  48. var res =await db.TargetInfos.ToListAsync();
  49. infos.AddRange(res);
  50. }
  51. }
  52. private void btnCancel_Click(object sender, EventArgs e)
  53. {
  54. this.DialogResult = DialogResult.Cancel;
  55. }
  56. private void btnOk_Click(object sender, EventArgs e)
  57. {
  58. try
  59. {
  60. string tarName=txtTarName.Text.Trim();
  61. if (string.IsNullOrWhiteSpace(tarName))
  62. {
  63. DxHelper.MsgBoxHelper.ShowError($"目标名称不能为空!");
  64. return;
  65. }
  66. if (infos.Any(i => i.ID != info.ID && i.TargetName == tarName))
  67. {
  68. DxHelper.MsgBoxHelper.ShowError($"目标名称[{tarName}]已经存在!");
  69. return;
  70. }
  71. info.TargetName = tarName;
  72. info.TargeColor=ColorTranslator.ToHtml(txtTarColor.Color);
  73. this.DialogResult = DialogResult.OK;
  74. }
  75. catch (Exception ex)
  76. {
  77. Serilog.Log.Error(ex,"编辑目标信息出错");
  78. DxHelper.MsgBoxHelper.ShowError("编辑目标信息出错");
  79. }
  80. }
  81. }
  82. }