using AntdUI; using DH.Commons.Base; using DH.Commons.Enums; using DH.Devices.PLC; namespace DHSoftware.Views { public partial class MotionEdit : UserControl { private AntdUI.Window window; private PLCItem plcItem; public bool submit; public MotionEdit(AntdUI.Window _window, PLCItem _plcItem) { InitializeComponent(); plcItem = _plcItem; // 绑定事件 BindEventHandler(); //设置默认值 InitData(); } private void BindEventHandler() { button_ok.Click += Button_ok_Click; button_cancel.Click += Button_cancel_Click; sltDataType.SelectedIndexChanged += SltDataType_SelectedIndexChanged; sltStart.SelectedIndexChanged += SltStart_SelectedIndexChanged; sltEnd.SelectedIndexChanged += SltEnd_SelectedIndexChanged; } private void SltEnd_SelectedIndexChanged(object sender, IntEventArgs e) { iptEndIndex.Visible = e.Value == 0; } private void SltDataType_SelectedIndexChanged(object sender, IntEventArgs e) { var isMType = e.Value == 2; stpNum.Visible = !isMType; sltBoolValue.Visible = isMType; } private void SltStart_SelectedIndexChanged(object sender, IntEventArgs e) { iptStartIndex.Visible = e.Value == 0; } private void Button_cancel_Click(object sender, EventArgs e) { submit = false; this.Dispose(); } private void Button_ok_Click(object sender, EventArgs e) { sltName.Status = AntdUI.TType.None; //检查输入内容 if (String.IsNullOrEmpty(sltName.Text)) { sltName.Status = AntdUI.TType.Error; AntdUI.Message.warn(window, "参数名称不能为空!", autoClose: 3); return; } plcItem.Name = sltName.Text; plcItem.Address = iptAddress.Text; plcItem.Type = sltDataType.Text; plcItem.Value = sltDataType.SelectedIndex == 2 ? sltBoolValue.Text : iptValue.Text; plcItem.NumTpye = sltDataType.SelectedIndex == 2 ? string.Empty : sltIntType.Text; plcItem.StartExecute = sltStart.Text == "执行" ? true : false; plcItem.StartIndex = sltStart.Text == "执行" ? Convert.ToInt32(iptStartIndex.Value) : -1; plcItem.EndExecute = sltEnd.Text == "执行" ? true : false; plcItem.EndIndex = sltEnd.Text == "执行" ? Convert.ToInt32(iptEndIndex.Value) : -1; submit = true; this.Dispose(); } private void InitData() { var PLCItemNames = Enum.GetNames(typeof(EnumPLCOutputIO)); foreach (var name in PLCItemNames) { sltName.Items.Add(name); } var PLCDataTypes = Enum.GetNames(typeof(EnumPLCDataType)); foreach (var name in PLCDataTypes) { sltDataType.Items.Add(name); } sltDataType.SelectedIndex = 0; var PLCINTTypes = Enum.GetNames(typeof(EnumPLCINTType)); foreach (var name in PLCINTTypes) { sltIntType.Items.Add(name); } sltStart.SelectedIndex = 1; sltEnd.SelectedIndex = 1; if (!string.IsNullOrEmpty(plcItem.Name)) { sltName.Text = plcItem.Name; iptAddress.Text = plcItem.Address; sltDataType.SelectedValue = plcItem.Type; iptValue.Text = sltDataType.SelectedIndex != 2 ? plcItem.Value : string.Empty; sltBoolValue.SelectedValue = sltDataType.SelectedIndex == 2 ? plcItem.Value : string.Empty; sltIntType.SelectedValue = sltDataType.SelectedIndex == 2 ? string.Empty : plcItem.NumTpye; sltStart.SelectedValue = plcItem.StartExecute == true ? "执行" : "取消"; sltEnd.SelectedValue = plcItem.EndExecute == true ? "执行" : "取消"; iptStartIndex.Value = plcItem.StartExecute == true ? plcItem.StartIndex : -1; iptEndIndex.Value = plcItem.EndExecute == true ? plcItem.EndIndex : -1; } } } }