using DevExpress.Utils; using DevExpress.XtraEditors.Controls; using HStation.Vmo; namespace HStation.WinFrmUI.Assets { public partial class EditAssetsTankMainDlg : DevExpress.XtraEditors.XtraForm { public EditAssetsTankMainDlg() { InitializeComponent(); this.treeList1.InitialMultiColSettings(); this.IconOptions.Icon = Yw.WinFrmUI.GlobalParas.AppIcon; } private Vmo.AssetsTankMainVmo _TankVmo = null; private List _AssetsTankCoefficient; public async void SetBindingData(Vmo.AssetsTankMainVmo TankVmo) { var bll = new BLL.AssetsTankCoefficient(); _AssetsTankCoefficient = await bll.GetAll(); this.TankCoefficientViewModelBindingSource.DataSource = _AssetsTankCoefficient; _TankVmo = TankVmo; var allCaliber = await new Yw.BLL.SysDictData().GetByTypeCode("1"); if (allCaliber != null) { foreach (var item in allCaliber) { var imageItem = new ImageComboBoxItem(item.Name, item.Name); TextEditCaliber.Properties.Items.Add(imageItem); } } var allMaterial = await new Yw.BLL.SysDictData().GetByTypeCode("3"); if (allMaterial != null) { foreach (var item in allMaterial) { var imageItem = new ImageComboBoxItem(item.Name, item.Name); TextEditMaterial.Properties.Items.Add(imageItem); } } this.TextEditName.Text = _TankVmo.Name; this.DescriptionTextEdit.Text = _TankVmo.Description; this.TextEditKeyWord.Text = string.Join(",", _TankVmo.KeyWord); this.selectFlagsPopupCtrl1.SetBindingData(_TankVmo.Flags); this.textEditDN.Text = _TankVmo.DN.ToString(); this.textEditMinLevel.Text = _TankVmo.MinLevel.ToString(); this.textEditMaxLevel.Text = _TankVmo.MaxLevel.ToString(); this.textEditMinVol.Text = _TankVmo.MinVol.ToString(); this.textEditVoerFlow.Checked = _TankVmo.OverFlow; } public event Func> ReloadDataEvent = null; //数据验证 private bool Valid() { this.dxErrorProvider1.ClearErrors(); if (string.IsNullOrEmpty(TextEditName.Text.Trim())) { this.dxErrorProvider1.SetError(this.TextEditName, "必填项"); return false; } return true; } //材料选择变化事件 private void TextEditMaterial_SelectedIndexChanged(object sender, EventArgs e) { if (_AssetsTankCoefficient == null) return; var select = GetCoefficientByMaterial(TextEditMaterial.Text); if (select == null) { this.TextEditMinorLoss.Text = string.Empty; return; } this.TextEditMinorLoss.Text = select.MinorLoss.ToString(); } //口径选择变化事件 private void TextEditCaliber_SelectedIndexChanged(object sender, EventArgs e) { if (_AssetsTankCoefficient == null) return; if (double.TryParse(TextEditCaliber.Text, out double caliber)) { foreach (var item in _AssetsTankCoefficient) { if (item.Caliber.HasValue) { if (Math.Abs(Convert.ToDouble(item.Caliber) - caliber) < 10) { this.TextEditMinorLoss.Text = item.MinorLoss.ToString(); return; } } } } } //找到最相近的材料 private AssetsTankCoefficientVmo GetCoefficientByMaterial(string name) { AssetsTankCoefficientVmo select = null; int maxMatchedChars = 0; foreach (var item in _AssetsTankCoefficient) { int matchedChars = GetIntersect(item.Material, name); if (matchedChars > maxMatchedChars) { maxMatchedChars = matchedChars; select = item; return select; } } return select; } private int GetIntersect(string str1, string str2) { if (str1 == null || str2 == null) return 0; return string.Join("", str1.Intersect(str2)).Count(); } //完成 private async void BtnOk_ClickAsync(object sender, EventArgs e) { if (!(Valid())) return; _TankVmo.Description = DescriptionTextEdit.Text.Trim(); _TankVmo.Name = TextEditName.Text.Trim(); _TankVmo.KeyWord = TextEditKeyWord.Text.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList(); _TankVmo.Flags = this.selectFlagsPopupCtrl1.SelectedFlags; if (double.TryParse(this.textEditDN.Text, out double DN)) { _TankVmo.DN = DN; } if (double.TryParse(this.textEditMinLevel.Text, out double MinLevel)) { _TankVmo.MinLevel = MinLevel; } if (double.TryParse(this.textEditMaxLevel.Text, out double MaxLevel)) { _TankVmo.MaxLevel = MaxLevel; } if (double.TryParse(this.textEditMinVol.Text, out double MinVol)) { _TankVmo.MinVol = MinVol; } _TankVmo.OverFlow = this.textEditVoerFlow.Checked; if (await this.ReloadDataEvent.Invoke(_TankVmo)) { TipFormHelper.ShowSucceed("修改成功!"); } else { TipFormHelper.ShowSucceed("修改失败!"); } this.DialogResult = DialogResult.OK; this.Close(); } private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) { var vm = this.treeList1.GetCurrentViewModel(_AssetsTankCoefficient); if (vm == null) return; this.TextEditMinorLoss.Text = vm.MinorLoss.ToString(); } } }