public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) { AApiVersionedFieldsCTD.TypedValuePropertyDescriptor pd = (AApiVersionedFieldsCTD.TypedValuePropertyDescriptor)context.PropertyDescriptor; IResourceKeyCTD rkCTD = (IResourceKeyCTD)pd.GetValue(null); AApiVersionedFields owner = rkCTD.owner; string field = rkCTD.field; object component = rkCTD.component; IResourceKey rk = (IResourceKey)AApiVersionedFieldsCTD.GetFieldValue(owner, field).Value; if (typeof(IResourceKey).IsAssignableFrom(value.GetType())) { IResourceKey rkNew = (IResourceKey)value; rk.ResourceType = rkNew.ResourceType; rk.ResourceGroup = rkNew.ResourceGroup; rk.Instance = rkNew.Instance; return(rk); } if (value != null && value is string) { if (AResourceKey.TryParse((string)value, rk)) { return(rk); } else { throw new NotSupportedException("Invalid data: " + (string)value); } } return(base.ConvertFrom(context, culture, value)); }
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (value as EnumChooserCTD != null && destinationType.Equals(typeof(string))) { try { EnumChooserCTD ctd = (EnumChooserCTD)value; TypedValue tv = (TypedValue)AApiVersionedFieldsCTD.GetFieldValue(ctd.owner, ctd.field); return("" + AApiVersionedFieldsCTD.GetFieldValue(ctd.owner, ctd.field)); } catch (Exception ex) { throw new NotSupportedException("Invalid data", ex); } } return(base.ConvertTo(context, culture, value, destinationType)); }
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) { IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); EnumChooserCTD ctd = (EnumChooserCTD)value; TypedValue tv = AApiVersionedFieldsCTD.GetFieldValue(ctd.owner, ctd.field); List <string> enumValues = new List <string>(); int index = -1; int i = 0; foreach (Enum e in Enum.GetValues(tv.Type)) { if (e.Equals((Enum)tv.Value)) { index = i; } enumValues.Add(new TypedValue(e.GetType(), e, "X")); i++; } int maxWidth = Application.OpenForms[0].Width / 3; if (maxWidth < Screen.PrimaryScreen.WorkingArea.Size.Width / 4) { maxWidth = Screen.PrimaryScreen.WorkingArea.Size.Width / 4; } int maxHeight = Application.OpenForms[0].Height / 3; if (maxHeight < Screen.PrimaryScreen.WorkingArea.Size.Height / 4) { maxHeight = Screen.PrimaryScreen.WorkingArea.Size.Height / 4; } TextBox tb = new TextBox { AutoSize = true, Font = new ListBox().Font, Margin = new Padding(0), MaximumSize = new System.Drawing.Size(maxWidth, maxHeight), Multiline = true, Lines = enumValues.ToArray(), }; tb.PerformLayout(); ListBox lb = new ListBox() { IntegralHeight = false, Margin = new Padding(0), Size = tb.PreferredSize, Tag = edSvc, }; lb.Items.AddRange(enumValues.ToArray()); lb.PerformLayout(); if (index >= 0) { lb.SelectedIndices.Add(index); } lb.SelectedIndexChanged += new EventHandler(lb_SelectedIndexChanged); edSvc.DropDownControl(lb); return(lb.SelectedItem == null ? value : (Enum) new EnumChooserConverter().ConvertFrom(context, System.Globalization.CultureInfo.CurrentCulture, lb.SelectedItem)); }