public void AddBinding(string fieldName, Control bindedField, eNumberFormat format = eNumberFormat.Default) { if (BindedType == null) { throw new Exception("Need to override BindedType to use bindings"); } if (_fieldInfo == null) { _fieldInfo = new Dictionary <string, FieldInfo>(); FieldInfo[] members = BindedType.GetFields(); foreach (FieldInfo info in members) { _fieldInfo[info.Name] = info; } } if (_fieldInfo.ContainsKey(fieldName)) { Type fieldType = _fieldInfo[fieldName].FieldType; if (fieldType.IsSubclassOf(typeof(Enum)) && bindedField is ComboBox) { BaseConfigForm.InitializeComboBox(((ComboBox)bindedField), fieldType); } _bindings[fieldName] = bindedField; _fieldFormat[fieldName] = format; } else { throw new Exception("Invalid field name"); } }
public void AddBinding(string fieldName, object bindedField, eNumberFormat format = eNumberFormat.Default, EventHandler onEditHandler = null) { if (BindedType == null) { throw new Exception("Need to override BindedType to use bindings"); } if (_fieldInfo == null) { _fieldInfo = new Dictionary <string, FieldInfoWrapper>(); PropertyInfo[] properties = BindedType.GetProperties(); foreach (PropertyInfo info in properties) { _fieldInfo[info.Name] = new FieldInfoWrapper(info); } FieldInfo[] members = BindedType.GetFields(); foreach (FieldInfo info in members) { _fieldInfo[info.Name] = new FieldInfoWrapper(info); } } if (_fieldInfo.ContainsKey(fieldName)) { Type fieldType = _fieldInfo[fieldName].FieldType; if (fieldType.IsSubclassOf(typeof(Enum)) && bindedField is ComboBox) { BaseConfigForm.InitializeComboBox(((ComboBox)bindedField), fieldType); } _bindings[fieldName] = bindedField; _bindedHandlers[fieldName] = onEditHandler; if (bindedField is TextBox) { ((TextBox)bindedField).Leave += onEditHandler; } else if (bindedField is ctrlPathSelection) { ((ctrlPathSelection)bindedField).Leave += onEditHandler; } else if (bindedField is CheckBox) { ((CheckBox)bindedField).CheckedChanged += onEditHandler; } else if (bindedField is ToolStripMenuItem) { ((ToolStripMenuItem)bindedField).CheckedChanged += onEditHandler; } else if (bindedField is ctrlRiskyOption) { ((ctrlRiskyOption)bindedField).Click += onEditHandler; } else if (bindedField is RadioButton) { ((RadioButton)bindedField).CheckedChanged += onEditHandler; } else if (bindedField is PictureBox) { ((PictureBox)bindedField).BackColorChanged += onEditHandler; } else if (bindedField is Panel) { FieldInfoWrapper field = _fieldInfo[fieldName]; object value = field.GetValue(this.Entity); RadioButton radio = ((Panel)bindedField).Controls.OfType <RadioButton>().FirstOrDefault(r => r.Tag.Equals(value)); if (radio != null) { radio.CheckedChanged += onEditHandler; } else { throw new Exception("No radio button matching value found"); } } else if (bindedField is ctrlTrackbar) { ((ctrlTrackbar)bindedField).ValueChanged += onEditHandler; } else if (bindedField is ctrlHorizontalTrackbar) { ((ctrlHorizontalTrackbar)bindedField).ValueChanged += onEditHandler; } else if (bindedField is TrackBar) { ((TrackBar)bindedField).ValueChanged += onEditHandler; } else if (bindedField is MesenNumericUpDown) { ((MesenNumericUpDown)bindedField).ValueChanged += onEditHandler; } else if (bindedField is ComboBox) { ((ComboBox)bindedField).SelectedIndexChanged += onEditHandler; } _fieldFormat[fieldName] = format; } else { throw new Exception("Invalid field name"); } }