public void SetTextMargin() { int maxWidth = 0; if (_component != null) { foreach (PropertyInfo _info in _component.GetType().GetProperties()) { if (_info.IsDefined(typeof(SquidComponentPropertyAttribute), true)) { object[] attributes = _info.GetCustomAttributes( typeof(SquidComponentPropertyAttribute), false); SquidComponentPropertyAttribute attribute = attributes[0] as SquidComponentPropertyAttribute; Font font = this.Font; int textWidth = 2 + (int)this.CreateGraphics().MeasureString(attribute.FriendlyName, font).Width; // keep the biggest string's width if (textWidth > maxWidth) { maxWidth = textWidth; } } } for (int i = 0; i < this.Controls.Count; i++) { AutoControl control = this.Controls[i] as AutoControl; control.SetFriendlyNameWidth(maxWidth); } } }
private void AddNewControl(PropertyInfo _info) { AutoControl control = null; control = CreateCustomAutoControlInstance(_info); if (control != null) { object[] attributes; attributes = _info.GetCustomAttributes( typeof(SquidComponentPropertyAttribute), false); SquidComponentPropertyAttribute attribute = attributes[0] as SquidComponentPropertyAttribute; object value = _info.GetValue(_component, null); control.SetFriendlyName(attribute.FriendlyName); if (value == null) { if (attribute.DefaultValue != null) { control.SetValue(attribute.DefaultValue); } else { control.SetValue(null); } } else { // special case for enums, convert to int first if (value is Enum) { value = (int)value; } control.SetValue(value.ToString()); } //control.BackColor = Color.RoyalBlue; control.Location = new System.Drawing.Point(0, _lastTop); control.Size = new Size(this.Size.Width, control.Size.Height); control.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right; control.Tag = _info; control.ValueChanged += ValueChangedEvent; this.Controls.Add(control); _lastTop += control.Height + _padding; } }
private void ValueChangedEvent(object sender, EventArgs e) { AutoControl control = sender as AutoControl; PropertyInfo prop = control.Tag as PropertyInfo; object valueObject = prop.GetValue(_component, null); object newValue = control.GetValue(); // special case for Enum, we need to convert back the int value to object if (valueObject is Enum) { newValue = Enum.ToObject(prop.PropertyType, newValue); } // if the value has really been changed, modify the component if (newValue.Equals(prop.GetValue(_component, null)) == false) { Console.WriteLine("Value of prop \"" + prop.Name + "\" has changed to " + newValue.ToString()); prop.SetValue(_component, newValue, null); } }