using DevExpress.XtraEditors; using ExtensionsDev; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.Entity; using System.Drawing; using System.Linq; using System.Windows.Forms; using DataSimulation.Repostory; using DataSimulation.Repostory.EFContext; using DataSimulation.Repostory.Model; using DxHelper; namespace DataSimulation.Forms.EditForms { public partial class RefEditor : DevExpress.XtraEditors.XtraForm { public RefInfo info; public RefEditor() { InitializeComponent(); this.layoutControl1.UseDefault(); this.Text = "添加参考站"; info = new RefInfo(); this.StartPosition = FormStartPosition.CenterParent; btnOk.ImageOptions.SvgImage = SvgHelper.LoadFromFile("Images\\Ok.svg"); btnOk.ImageOptions.SvgImageSize = new Size(20, 20); btnCancel.ImageOptions.SvgImage = SvgHelper.LoadFromFile("Images\\Cancel.svg"); btnCancel.ImageOptions.SvgImageSize = new Size(20, 20); txtRefLon.EditValueChanged += TxtLon_EditValueChanged; txtRefLat.EditValueChanged += TxtLat_EditValueChanged; } private void TxtLat_EditValueChanged(object sender, EventArgs e) { txtRefLat.CheckLat(dxErrorProvider, "参考站"); } private void TxtLon_EditValueChanged(object sender, EventArgs e) { txtRefLon.CheckLon(dxErrorProvider, "参考站"); } public RefEditor(RefInfo info) : this() { this.Text = "编辑参考站"; this.info = info; } private void Editor_Load(object sender, EventArgs e) { if (this.Text == "编辑参考站" && info != null) { this.txtRefName.Text = info.RefName; this.txtRefLon.Text = $"{info.RefLon}"; this.txtRefLat.Text = $"{info.RefLat}"; } } private void btnCancel_Click(object sender, EventArgs e) { this.DialogResult = DialogResult.Cancel; } private async void btnOk_Click(object sender, EventArgs e) { if (!txtRefLon.CheckLon(dxErrorProvider, "参考站") || !txtRefLat.CheckLat(dxErrorProvider, "参考站")) { return; } try { info.RefName = txtRefName.Text.Trim(); info.RefLon = Convert.ToDouble(txtRefLon.Text); info.RefLat = Convert.ToDouble(txtRefLat.Text); if (this.Text == "添加参考站") { using (SimulationContext db = new SimulationContext()) { bool isAny = db.RefInfos.Any(s => s.RefName == info.RefName && s.RefLon == info.RefLon && s.RefLat == info.RefLat); if (isAny) { DxHelper.MsgBoxHelper.ShowError($"添加参考站信息失败,已经存在参考站:{info.Ref}"); return; } db.RefInfos.Add(info); await db.SaveChangesAsync(); } } else { using (SimulationContext db = new SimulationContext()) { bool isAny = db.RefInfos.Any(s => s.ID != info.ID && s.RefName == info.RefName && s.RefLon == info.RefLon && s.RefLat == info.RefLat); if (isAny) { DxHelper.MsgBoxHelper.ShowError($"编辑参考站信息失败,已经存在参考站:{info.Ref}"); return; } var find = await db.RefInfos.Where(p => p.ID == info.ID).FirstOrDefaultAsync(); find.RefName = info.RefName; find.RefLon = info.RefLon; find.RefLat = info.RefLat; await db.SaveChangesAsync(); } } this.DialogResult = DialogResult.OK; } catch (Exception ex) { Serilog.Log.Error(ex, $"{this.Text}信息出错"); DxHelper.MsgBoxHelper.ShowError($"{this.Text}信息出错"); } } } }