Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class GenericCollectionEditor : UITypeEditor
- {
- public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
- {
- if (context == null && context.Instance == null) return base.GetEditStyle(context);
- return UITypeEditorEditStyle.Modal;
- }
- public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
- {
- if (context == null || provider == null || context.Instance == null) return value;
- IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
- Type type = value.GetType().GetGenericArguments().SingleOrDefault() ?? typeof(object);
- Type genericClass = typeof(GenericCollectionEditorForm<>);
- Type constructedClass = genericClass.MakeGenericType(type);
- dynamic form = Activator.CreateInstance(constructedClass);
- switch (type)
- {
- case Type t when t == typeof(SupportApplications):
- List<SupportApplications> list = new List<SupportApplications>();
- foreach (var item in (List<SupportApplications>)value) list.Add(item);
- form.Objects = list;
- break;
- default:
- form.Text = $"Edit {type.Name} Collection";
- break;
- }
- form.Text = $"Edit {type.Name} Collection";
- if (editorService.ShowDialog((Form)form) == System.Windows.Forms.DialogResult.OK)
- {
- switch (type)
- {
- case Type t when t == typeof(SupportApplications):
- return new List<SupportApplications>(form.Objects);
- default:
- throw new NotSupportedException($"Type {type.Name} is not supported in GenericCollectionEditor.");
- }
- }
- return base.EditValue(context, provider, value);
- }
- // =============================
- public partial class GenericCollectionEditorForm<T> : FormViewBase
- {
- private List<T> objects = new List<T>();
- [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
- public List<T> Objects
- {
- get { return objects; }
- set
- {
- objects = value;
- objectsBindingSource.DataSource = Objects;
- RefreshObjectListDataBindings();
- }
- }
- private Type type = null;
- private BindingSource objectsBindingSource = new BindingSource();
- public GenericCollectionEditorForm()
- {
- InitializeComponent();
- MainLB.DataSource = objectsBindingSource;
- }
- private void AddBT_Click(object sender, EventArgs e)
- {
- Objects.Add((T)Activator.CreateInstance(typeof(T)));
- RefreshObjectListDataBindings();
- }
- private void MainLB_SelectedValueChanged(object sender, EventArgs e)
- {
- MainPG.SelectedObject = MainLB.SelectedItem;
- }
- private void RefreshObjectListDataBindings()
- {
- MainLB.DataSource = null;
- MainLB.DataSource = objectsBindingSource;
- }
- private void MainPG_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
- {
- Debug.WriteLine(e.ChangedItem.DisplayName());
- int selectedIndex = MainLB.SelectedIndex;
- RefreshObjectListDataBindings();
- MainLB.SelectedIndex = selectedIndex;
- }
- private void DeleteBT_Click(object sender, EventArgs e)
- {
- if (MainLB.SelectedItems.Count == 0) return;
- Objects.Remove((T)objectsBindingSource.Current);
- RefreshObjectListDataBindings();
- }
- private void UpBT_Click(object sender, EventArgs e)
- {
- int newIndex = Math.Max(MainLB.SelectedIndex - 1, 0);
- Objects.Move(MainLB.SelectedIndex, newIndex);
- RefreshObjectListDataBindings();
- MainLB.SelectedIndex = newIndex;
- }
- private void DownBT_Click(object sender, EventArgs e)
- {
- int newIndex = Math.Min(MainLB.SelectedIndex + 1, MainLB.Items.Count - 1);
- Objects.Move(MainLB.SelectedIndex, newIndex);
- RefreshObjectListDataBindings();
- MainLB.SelectedIndex = newIndex;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement