private void AddButton_Click(object sender, RoutedEventArgs e) { ControllerModel controller = new ControllerModel(); controller.Name = ControllerNameInputTextBox.Text; controller.Type = (ControllerType)ControllerTypeComboBox.SelectedItem; controller.DeviceAddressLength = Convert.ToInt32(DeviceCodeLengthComboBox.SelectedItem); controller.MachineNumber = ControllerMachineNumInputTextBox.Text; controller.PortName = SerialPortNumberComboBox.SelectedItem.ToString(); controller.LoopAddressLength = 2;//回路地址长度默认为2 ControllerManager controllerManager = new ControllerManager(); controllerManager.InitializeAllControllerOperation(null); IControllerOperation controllerOperation = controllerManager.GetController(controller.Type); controllerOperation.AddControllerToProject(controller); RaiseEvent(new RoutedEventArgs(AddButtonClickEvent)); }
private void AddButton_Click(object sender, RoutedEventArgs e) { ClearAllErrorMessage(); ControllerModel controller = new ControllerModel(); bool verifyFlag = true; if (ControllerTypeComboBox.SelectedItem != null) { controller.Type = (ControllerType)ControllerTypeComboBox.SelectedItem; } else { this.ErrorMessageControllerType.Text = "请选择控制器类型"; verifyFlag = false; } if (DeviceCodeLengthComboBox.SelectedItem != null) { controller.DeviceAddressLength = Convert.ToInt32(DeviceCodeLengthComboBox.SelectedItem); } else { this.ErrorMessageControllerDeviceAddressLength.Text = "请选择器件长度"; verifyFlag = false; } if (SerialPortNumberComboBox.SelectedItem != null) { controller.PortName = SerialPortNumberComboBox.SelectedItem.ToString(); } else { this.ErrorMessageControllerPortName.Text = "请选择端口"; verifyFlag = false; } if (this.ControllerMachineNumInputTextBox.Text == "") { this.ErrorMessageControllerDeviceAddressLength.Text = "请填写器件长度"; verifyFlag = false; } if (verifyFlag) { controller.Name = ControllerNameInputTextBox.Text; controller.MachineNumber = ControllerMachineNumInputTextBox.Text; controller.LoopAddressLength = 2;//回路地址长度默认为2 IControllerConfig config = ControllerConfigManager.GetConfigObject(controller.Type); int maxMachineNumber = config.GetMaxMachineAmountValue(controller.DeviceAddressLength); Dictionary <string, RuleAndErrorMessage> dictRule = config.GetControllerInfoRegularExpression(controller.DeviceAddressLength); RuleAndErrorMessage rule = dictRule["Name"]; Regex exminator = new Regex(rule.Rule); if (!string.IsNullOrEmpty(controller.Name)) { if (!exminator.IsMatch(controller.Name)) { this.ErrorMessageControllerName.Text = rule.ErrorMessage; verifyFlag = false; } } else { this.ErrorMessageControllerName.Text = "请填写控制器名称"; verifyFlag = false; } rule = dictRule["MachineNumber"]; exminator = new Regex(rule.Rule); if (!exminator.IsMatch(ControllerMachineNumInputTextBox.Text)) { this.ErrorMessageControllerMachineNumber.Text = rule.ErrorMessage; verifyFlag = false; } else { controller.MachineNumber = this.ControllerMachineNumInputTextBox.Text; } if (verifyFlag) { if (Convert.ToInt16(controller.MachineNumber) > maxMachineNumber) { this.ErrorMessageControllerMachineNumber.Text = "机号超出范围,最大机号为" + maxMachineNumber.ToString(); verifyFlag = false; } } } if (verifyFlag) { ControllerManager controllerManager = new ControllerManager(); controllerManager.InitializeAllControllerOperation(null); IControllerOperation controllerOperation = controllerManager.GetController(controller.Type); controllerOperation.AddControllerToProject(controller); RaiseEvent(new RoutedEventArgs(AddButtonClickEvent)); } }