public void BindData(ITypeDescriptorContext context, object value, object[] attributeArguments) { filterStandardTypes.Clear(); if (attributeArguments != null && attributeArguments.Length > 0) { foreach (Type type in attributeArguments.OfType <Type>().Where(item => iFilterType.IsAssignableFrom(item))) { IFilterObjectValueEditorTypes filterTypeObj = (IFilterObjectValueEditorTypes)Activator.CreateInstance(type); filterTypeObj.Initialize(context); filterStandardTypes.Add(filterTypeObj); } } //allowedStandardTypes.AddRange(EnumType<StandartType>.Values); IEnumerable <StandartType> _stdTypes = EnumType <StandartType> .Values; foreach (var filterStandardTypeObj in filterStandardTypes) { _stdTypes = filterStandardTypeObj.FilterStandartTypes(_stdTypes); } allowedStandardTypes.AddRange(_stdTypes); PopulateForm(); ObjectValue objectValue = (ObjectValue)value; object objValue = objectValue.Value; StandartType resolvedStandardType = objValue == null ? StandartType.String : StandardValues.ResolveStandardType(objValue.GetType()); StandartType valueType = objValue == null ? StandartType.String : resolvedStandardType; if (!allowedStandardTypes.Contains(valueType)) { valueType = StandartType.String; objValue = objValue.ToString(); } CurrentValueType = valueType; if (objectValue.UseCustomExpression) { CurrentValueType = null; objValue = objectValue.CustomExpression; valueTextBox.Text = objectValue.CustomExpression; } else if (objValue != null && valueType == StandartType.String && objValue.GetType() != StandardValues.TYPE_STRING) { objValue = objValue.ToString(); } this.CurrentValue = objValue; UpdateControls(); }
private void UpdateControls() { HideAllSettingControls(); chUseNullValue.Visible = !CurrentValueType.HasValue || CurrentValueType.Value == StandartType.String; useNullValueAllowed = chUseNullValue.Visible; lbValueHint.Visible = !UseNullValueChecked; if (UseNullValueChecked) { if (CurrentValueType.HasValue && CurrentValueType.Value != StandartType.String) { UseNullValueChecked = false; } else { return; } } Action resetValueTextBox = () => { valueTextBox.MaxLength = Int16.MaxValue; valueTextBox.ReadOnly = false; valueTextBox.Text = string.Empty; valueTextBox.Size = new Size(VALUE_TEXT_BOX_WIDTH_NORMAL, valueTextBox.Size.Height); }; string hintText; object currentValue = CurrentValue; if (!CurrentValueType.HasValue) { valueTextBox.Visible = true; resetValueTextBox(); valueTextBox.Text = currentValue == null ? string.Empty : currentValue.ToString(); valueTextBox.Size = new Size(VALUE_TEXT_BOX_WIDTH_SMALL_WITH_NULL, valueTextBox.Size.Height); hintText = typeHints[VALUE_TYPE_CUSTOM]; } else { StandartType currentValueType = CurrentValueType.Value; hintText = typeHints[(int)currentValueType]; switch (currentValueType) { case StandartType.String: case StandartType.Char: case StandartType.ByteArray: case StandartType.Guid: { // handle as string in textbox valueTextBox.Visible = true; resetValueTextBox(); int textMaxLength = Int16.MaxValue; if (currentValueType == StandartType.Char) { textMaxLength = 1; } else if (currentValueType == StandartType.Guid) { textMaxLength = 38; } bool buttonsVisible = currentValueType.In(StandartType.ByteArray, StandartType.Guid); valueTextBox.MaxLength = textMaxLength; valueTextBox.ReadOnly = currentValueType == StandartType.ByteArray; int sizeWidth = buttonsVisible ? VALUE_TEXT_BOX_WIDTH_SMALL : VALUE_TEXT_BOX_WIDTH_NORMAL; if (currentValueType == StandartType.String) { sizeWidth = VALUE_TEXT_BOX_WIDTH_SMALL_WITH_NULL; } valueTextBox.Size = new Size(sizeWidth, valueTextBox.Size.Height); if (buttonsVisible) { btnAction1.Visible = true; btnAction1.Text = currentValueType == StandartType.ByteArray ? BUTTON_TEXT_LOAD_FILE : BUTTON_TEXT_NEW_GUID; btnAction2.Visible = true; btnAction2.Text = currentValueType == StandartType.ByteArray ? BUTTON_TEXT_CLEAR_FILE : BUTTON_TEXT_VALIDATE_VALUE; } if (currentValueType == StandartType.ByteArray) { int byteLength = 0; byte[] byteArray = null; if (currentValue != null) { byteArray = (byte[])currentValue; byteLength = byteArray.Length; } valueTextBox.Text = string.Format(BYTE_ARRAY_TEXT, byteLength); valueTextBox.Tag = byteArray; } else { valueTextBox.Text = currentValue != null?currentValue.ToString() : string.Empty; } break; } case StandartType.Boolean: { // handle boolean in combobox as True/False items valueComboBox.Visible = true; valueComboBox.Items.Clear(); valueComboBox.Items.AddRange(new[] { "false", "true" }); int idx = 0; if (currentValue != null) { idx = Convert.ToInt32((bool)currentValue); } valueComboBox.SelectedIndex = idx; break; } case StandartType.Byte: case StandartType.Int16: case StandartType.Int32: case StandartType.Int64: case StandartType.SByte: case StandartType.UInt16: case StandartType.UInt32: case StandartType.UInt64: { valueUpDown.Visible = true; decimal maxValue = 0; decimal minValue = 0; Action <object, Func <object, decimal> > setValue = (value, castValue) => valueUpDown.Value = value == null ? 0 : castValue(value); if (currentValueType == StandartType.Byte) { minValue = byte.MinValue; maxValue = byte.MaxValue; //valueUpDown.Value = (byte)currentValue; setValue(currentValue, value => (byte)value); } else if (currentValueType == StandartType.Int16) { minValue = Int16.MinValue; maxValue = Int16.MaxValue; //valueUpDown.Value = (Int16)currentValue; setValue(currentValue, value => (Int16)value); } else if (currentValueType == StandartType.Int32) { minValue = Int32.MinValue; maxValue = Int32.MaxValue; //valueUpDown.Value = (Int32)currentValue; setValue(currentValue, value => (Int32)value); } else if (currentValueType == StandartType.Int64) { minValue = Int64.MinValue; maxValue = Int64.MaxValue; //valueUpDown.Value = (Int64)currentValue; setValue(currentValue, value => (Int64)value); } else if (currentValueType == StandartType.SByte) { minValue = SByte.MinValue; maxValue = SByte.MaxValue; //valueUpDown.Value = (sbyte)currentValue; setValue(currentValue, value => (sbyte)value); } else if (currentValueType == StandartType.UInt16) { minValue = UInt16.MinValue; maxValue = UInt16.MaxValue; //valueUpDown.Value = (UInt16)currentValue; setValue(currentValue, value => (UInt16)value); } else if (currentValueType == StandartType.UInt32) { minValue = UInt32.MinValue; maxValue = UInt32.MaxValue; //valueUpDown.Value = (UInt32)currentValue; setValue(currentValue, value => (UInt32)value); } else if (currentValueType == StandartType.UInt64) { minValue = UInt64.MinValue; maxValue = UInt64.MaxValue; //valueUpDown.Value = (UInt64)currentValue; setValue(currentValue, value => (UInt64)value); } valueUpDown.Minimum = minValue; valueUpDown.Maximum = maxValue; break; } case StandartType.DateTime: case StandartType.TimeSpan: { panelDateTime.Visible = true; valueDate.Visible = currentValueType == StandartType.DateTime; lbDate.Visible = currentValueType == StandartType.DateTime; valueTime.ShowCheckBox = currentValueType == StandartType.DateTime; TimeSpan timeValue; DateTime dateValue; if (currentValueType == StandartType.TimeSpan) { timeValue = currentValue == null ? TimeSpan.Zero : (TimeSpan)currentValue; dateValue = DateTime.Today; } else { DateTime dateTime = currentValue == null ? DateTime.Now : (DateTime)currentValue; timeValue = dateTime.TimeOfDay; dateValue = dateTime.Date; } valueDate.Value = dateValue; valueTime.Value = dateValue.Add(timeValue); break; } case StandartType.Double: case StandartType.Single: case StandartType.Decimal: { valueTextBox.Visible = true; resetValueTextBox(); btnAction2.Visible = true; btnAction2.Text = BUTTON_TEXT_VALIDATE_VALUE; valueTextBox.Text = currentValue == null ? "0" : currentValue.ToString(); break; } } } lbValueHint.Text = hintText; }
private object SaveData(int typeValue) { string valueText = valueTextBox.Text; bool isSelectedCustomExpression = IsSelectedCustomExpression; string customExpression = isSelectedCustomExpression ? valueText : null; object result = null; if (!useNullValueAllowed || !UseNullValueChecked) { StandartType standartType = (StandartType)typeValue; switch (standartType) { case StandartType.String: case StandartType.Char: case StandartType.ByteArray: case StandartType.Guid: case StandartType.Double: case StandartType.Single: case StandartType.Decimal: { result = valueText; if (!string.IsNullOrEmpty(valueText)) { if (standartType == StandartType.Char) { result = valueText[0]; } else if (standartType == StandartType.ByteArray) { result = (byte[])valueTextBox.Tag; } else if (standartType == StandartType.Guid) { result = new Guid(valueText); } else if (standartType == StandartType.Double) { result = double.Parse(valueText); } else if (standartType == StandartType.Single) { result = Single.Parse(valueText); } else if (standartType == StandartType.Decimal) { result = Decimal.Parse(valueText); } } break; } case StandartType.Boolean: { result = Convert.ToBoolean(valueComboBox.SelectedIndex); break; } case StandartType.Byte: case StandartType.Int16: case StandartType.Int32: case StandartType.Int64: case StandartType.SByte: case StandartType.UInt16: case StandartType.UInt32: case StandartType.UInt64: { decimal currentValue = valueUpDown.Value; if (standartType == StandartType.Byte) { result = Convert.ToByte(currentValue); } else if (standartType == StandartType.Int16) { result = Convert.ToInt16(currentValue); } else if (standartType == StandartType.Int32) { result = Convert.ToInt32(currentValue); } else if (standartType == StandartType.Int64) { result = Convert.ToInt64(currentValue); } else if (standartType == StandartType.SByte) { result = Convert.ToSByte(currentValue); } else if (standartType == StandartType.UInt16) { result = Convert.ToUInt16(currentValue); } else if (standartType == StandartType.UInt32) { result = Convert.ToUInt32(currentValue); } else if (standartType == StandartType.UInt64) { result = Convert.ToUInt64(currentValue); } break; } case StandartType.DateTime: case StandartType.TimeSpan: { DateTime date = valueDate.Value; TimeSpan time = valueTime.Value.TimeOfDay; if (standartType == StandartType.DateTime) { result = date; if (valueTime.Checked) { result = date.Add(time); } } else { result = time; } break; } } } ObjectValue objectValue = new ObjectValue(isSelectedCustomExpression, customExpression, result); return(objectValue); }