using System; using System.Collections.Generic; using System.Windows.Forms; using Mesnac.Action.ChemicalWeighing.LjMaterial; namespace Mesnac.Action.ChemicalWeighing.LjMixFormula { public partial class FrmMixFormula : Form { private LjMixView _view = new LjMixView(); public FrmMixFormula() { InitializeComponent(); Init(); } public FrmMixFormula(string id) : this() { _view = MixDb.GetById(id); txtName.Text = _view.Name; this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = _view.Data; } private void Init() { DrpDeviceUnit.ValueMember = "Id"; DrpDeviceUnit.DisplayMember = "Name"; DrpAction.ValueMember = "Id"; DrpAction.DisplayMember = "Name"; IList drpList = MixDb.GetDeviceUnit(); DrpDeviceUnit.DataSource = drpList; } private void DrpDeviceUnit_SelectedIndexChanged(object sender, EventArgs e) { IList drpList = MixDb.GetActionByDeviceUnit(Convert.ToInt32(DrpDeviceUnit.SelectedValue)); DrpAction.DataSource = drpList; } private void btnCancel_Click(object sender, EventArgs e) { this.Close(); } private void button1_Click(object sender, EventArgs e) { bool isShiHun = false; string name = txtName.Text.Trim(); if (string.IsNullOrEmpty(name)) { txtName.Focus(); MessageBox.Show("请输入配方名称"); return; } _view.Name = name; int deviceUnitId = Convert.ToInt32(DrpDeviceUnit.SelectedValue); if (deviceUnitId == 3) { isShiHun = true; } int actionId = (int)DrpAction.SelectedValue; string actionName = DrpAction.Text; LjMixDetailView viewDetail = new LjMixDetailView(); if (_view.DeviceUnitId == 0) { _view.DeviceUnitId = deviceUnitId; } else { if (_view.DeviceUnitId != deviceUnitId) { MessageBox.Show("请选择相同的设备"); return; } } _view.DeviceUnitName = DrpDeviceUnit.Text; _view.CreateTime=DateTime.Now; if (!int.TryParse(TxtSendTime.Text.Trim(), out int send)) { TxtSendTime.Focus(); MessageBox.Show("请输入正确的时间"); return; } viewDetail.ActionId = (int)DrpAction.SelectedValue; viewDetail.ActionName = DrpAction.Text; viewDetail.MixId = _view.Id; viewDetail.SecondTime = send; if (!float.TryParse(txtTemperature.Text.Trim(), out float temperature)) { txtTemperature.Focus(); MessageBox.Show(@"请输入正确的温度"); return; } viewDetail.Temperature = temperature; if (!float.TryParse(txtSpeed.Text.Trim(), out float speed)) { txtSpeed.Focus(); MessageBox.Show("请输入正确的速度"); return; } viewDetail.Speed = speed; viewDetail.Weight = 0; if (isShiHun) { if (!float.TryParse(txtWeight.Text.Trim(), out float weight)) { txtWeight.Focus(); MessageBox.Show("请输入正确的重量"); return; } viewDetail.Weight = 0; } viewDetail.Difference = 0; if (isShiHun) { if (!float.TryParse(txtDifference.Text.Trim(), out float difference)) { txtDifference.Focus(); MessageBox.Show("请输入正确的公差"); return; } viewDetail.Difference = difference; } _view.AddData(viewDetail); this.dataGridView1.AutoGenerateColumns = false; this.dataGridView1.DataSource = null; this.dataGridView1.DataSource = _view.Data; Clean(); } private void Clean() { txtDifference.Text = string.Empty; txtWeight.Text = string.Empty; txtSpeed.Text = string.Empty; TxtSendTime.Text = string.Empty; txtTemperature.Text = string.Empty; } private void btnOK_Click(object sender, EventArgs e) { if (_view.Data.Count == 0) { MessageBox.Show("请添加配方动作"); return; } MixDb.Save(_view); this.DialogResult = System.Windows.Forms.DialogResult.OK; } } }