using System; using System.Windows.Forms; using log4net; using TBF.BenchControl.Generic; namespace TBF.BenchControl.Elde.PressureMeter { public partial class PressureMeterCfgCtrl : UserControl, IComponentCfgCtrl { static readonly ILog log = LogManager.GetLogger(typeof(PressureMeterCfgCtrl)); ComponentParametersDlg parent; PressureMeterCfg config; public IComponentCfg Config { get { return config as IComponentCfg; } set { config = value as PressureMeterCfg; Redraw(); } } public PressureMeterCfgCtrl() { InitializeComponent(); } private void PressureMeterCfgCtrl_Load(object sender, EventArgs e) { parent = ParentForm as ComponentParametersDlg; if (parent == null) return; if (parent.TbfComponents != null) { foreach (var cmpnt in parent.TbfComponents) { if (TbfComponents.CmpntFactoryFromClassName(cmpnt.ClassName) is Elde.ControlBoardFactory) { parentNameComboBox.Items.Add(cmpnt.Name); } } } Redraw(); } void Redraw() { if (config == null) return; /// Control was not loaded, settings were not changed componentNameLabel.Text = config.Factory.ClassName; nameTextBox.Text = config.Name; parentNameComboBox.Text = string.IsNullOrEmpty(config.ParentName) ? "---" : config.ParentName; positionTextBox.Text = config.Position.ToString(); rs485AddressTextBox.Text = config.RS485Address.ToString(); } public void Unlock() { nameTextBox.Enabled = true; parentNameComboBox.Enabled = true; positionTextBox.Enabled = true; rs485AddressTextBox.Enabled = true; } public CfgVerifyFlags VerifyCfg(ref string message) { CfgVerifyFlags flags = CfgVerifyFlags.None; int dummy; if (!parentNameComboBox.Items.Contains(parentNameComboBox.Text)) { flags |= CfgVerifyFlags.Error; message += Environment.NewLine + "Invalid 'Parent Name'"; } if (!int.TryParse(positionTextBox.Text, out dummy) || dummy < 0 || dummy > 7) { flags |= CfgVerifyFlags.Error; message += Environment.NewLine + "'Position' should be between 0 and 7"; } if (!int.TryParse(rs485AddressTextBox.Text, out dummy) || dummy < 0 || dummy > 255) { flags |= CfgVerifyFlags.Error; message += Environment.NewLine + "'RS485 Address' should be between 0 and 255"; } return flags; } public void UpdateCfg() { if (config == null) return; /// Control was not loaded, settings were not changed config.Name = nameTextBox.Text; config.ParentName = parentNameComboBox.Text.Equals("---") ? string.Empty : parentNameComboBox.Text; config.Position = int.Parse(positionTextBox.Text); config.RS485Address = (byte)int.Parse(rs485AddressTextBox.Text); } } }