TargetEditor.cs 2.8 KB

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