using System; using System.Collections.Generic; using System.Text; using DevComponents.Editors; using System.Drawing; namespace DevComponents.DotNetBar { /// /// Defines a attribute which applies an integer type numeric editor to a property when used with AdvPropertyGrid control. Applies to int property types only. /// public class PropertyIntegerEditor : PropertyValueEditor { #region Implementation /// /// Gets or sets whether up/down button is shown. /// public bool ShowUpDownButton = true; /// /// Gets or sets the display format for the control when control does not have input focus. /// public string DisplayFormat = ""; /// /// Gets or sets the minimum value that can be entered. /// public int MinValue = int.MinValue; /// /// Gets or sets the maximum value that can be entered. /// public int MaxValue = int.MaxValue; /// /// Gets or sets whether empty state i.e. null/nothing value is allowed when editor is used with nullable types. /// public bool AllowEmptyState = false; /// /// Initializes a new instance of the PropertyIntegerEditor class. /// public PropertyIntegerEditor() { } /// /// Initializes a new instance of the PropertyIntegerEditor class. /// /// public PropertyIntegerEditor(bool showUpDownButton) { ShowUpDownButton = showUpDownButton; } /// /// Initializes a new instance of the PropertyIntegerEditor class. /// /// /// public PropertyIntegerEditor(int minValue, int maxValue) { MinValue = minValue; MaxValue = maxValue; } /// /// Initializes a new instance of the PropertyIntegerEditor class. /// /// /// /// public PropertyIntegerEditor(bool showUpDownButton, int minValue, int maxValue) { ShowUpDownButton = showUpDownButton; MinValue = minValue; MaxValue = maxValue; } /// /// Initializes a new instance of the PropertyIntegerEditor class. /// /// /// /// /// public PropertyIntegerEditor(bool showUpDownButton, string displayFormat, int minValue, int maxValue) { ShowUpDownButton = showUpDownButton; DisplayFormat = displayFormat; MinValue = minValue; MaxValue = maxValue; } /// /// Initializes a new instance of the PropertyIntegerEditor class. /// /// /// public PropertyIntegerEditor(bool showUpDownButton, string displayFormat) { ShowUpDownButton = showUpDownButton; DisplayFormat = displayFormat; } public override IPropertyValueEditor CreateEditor(System.ComponentModel.PropertyDescriptor propertyDescriptor, object targetObject) { if (propertyDescriptor.PropertyType != typeof(int) && propertyDescriptor.PropertyType != typeof(int?) && propertyDescriptor.PropertyType != typeof(byte) && propertyDescriptor.PropertyType != typeof(byte?)) throw new InvalidOperationException("PropertyIntegerEditor works only with int or byte type properties"); IntegerValueEditor editor = new IntegerValueEditor(); editor.AutoBorderSize = 1; if (!string.IsNullOrEmpty(DisplayFormat)) { editor.DisplayFormat = DisplayFormat; } editor.ShowUpDown = ShowUpDownButton; editor.Height = Dpi.Height14; editor.BackgroundStyle.Class = ""; editor.BackgroundStyle.BackColor = Color.White; editor.AllowEmptyState = this.AllowEmptyState; if (propertyDescriptor.PropertyType == typeof(byte) || propertyDescriptor.PropertyType == typeof(byte?)) { editor.MinValue = Math.Max(byte.MinValue, MinValue); editor.MaxValue = Math.Min(byte.MaxValue, MaxValue); } else { editor.MinValue = MinValue; editor.MaxValue = MaxValue; } return editor; } #endregion private class IntegerValueEditor : IntegerInput, IPropertyValueEditor { #region IPropertyValueEditor Members public System.Drawing.Font EditorFont { get { return this.Font; } set { this.Font = value; } } public bool IsEditorFocused { get { return this.Focused; } } public void FocusEditor() { this.Focus(); } public object EditValue { get { return this.ValueObject; } set { if (value == null) this.ValueObject = null; else this.Value = (int)value; } } protected override void OnValueChanged(EventArgs e) { OnEditValueChanged(e); base.OnValueChanged(e); } private void OnEditValueChanged(EventArgs e) { EventHandler ev = EditValueChanged; if (ev != null) ev(this, e); } public event EventHandler EditValueChanged; #endregion } } }