Advertisement
stigzler

Untitled

May 28th, 2025
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.62 KB | None | 0 0
  1.     internal class GenericCollectionEditor : UITypeEditor
  2.     {
  3.         public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
  4.         {
  5.             if (context == null && context.Instance == null) return base.GetEditStyle(context);
  6.             return UITypeEditorEditStyle.Modal;
  7.         }
  8.  
  9.         public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  10.         {
  11.             if (context == null || provider == null || context.Instance == null) return value;
  12.  
  13.             IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  14.  
  15.             Type type = value.GetType().GetGenericArguments().SingleOrDefault() ?? typeof(object);
  16.  
  17.             Type genericClass = typeof(GenericCollectionEditorForm<>);
  18.             Type constructedClass = genericClass.MakeGenericType(type);
  19.             dynamic form = Activator.CreateInstance(constructedClass);
  20.  
  21.             switch (type)
  22.             {
  23.                 case Type t when t == typeof(SupportApplications):
  24.                     List<SupportApplications> list = new List<SupportApplications>();
  25.                     foreach (var item in (List<SupportApplications>)value) list.Add(item);
  26.                     form.Objects = list;
  27.                     break;
  28.                 default:
  29.                     form.Text = $"Edit {type.Name} Collection";
  30.                     break;
  31.             }
  32.  
  33.             form.Text = $"Edit {type.Name} Collection";
  34.  
  35.             if (editorService.ShowDialog((Form)form) == System.Windows.Forms.DialogResult.OK)
  36.             {
  37.                 switch (type)
  38.                 {
  39.                     case Type t when t == typeof(SupportApplications):
  40.                         return new List<SupportApplications>(form.Objects);
  41.                     default:
  42.                         throw new NotSupportedException($"Type {type.Name} is not supported in GenericCollectionEditor.");
  43.                 }
  44.             }
  45.  
  46.             return base.EditValue(context, provider, value);
  47.  
  48.         }
  49.  
  50. // =============================
  51.  
  52.     public partial class GenericCollectionEditorForm<T> : FormViewBase
  53.     {
  54.         private List<T> objects = new List<T>();
  55.  
  56.         [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
  57.         public List<T> Objects
  58.         {
  59.             get { return objects; }
  60.             set
  61.             {
  62.                 objects = value;
  63.                 objectsBindingSource.DataSource = Objects;
  64.                 RefreshObjectListDataBindings();
  65.             }
  66.         }
  67.  
  68.         private Type type = null;
  69.  
  70.         private BindingSource objectsBindingSource = new BindingSource();
  71.  
  72.  
  73.         public GenericCollectionEditorForm()
  74.         {
  75.             InitializeComponent();
  76.             MainLB.DataSource = objectsBindingSource;
  77.         }
  78.  
  79.         private void AddBT_Click(object sender, EventArgs e)
  80.         {
  81.             Objects.Add((T)Activator.CreateInstance(typeof(T)));
  82.             RefreshObjectListDataBindings();
  83.         }
  84.  
  85.         private void MainLB_SelectedValueChanged(object sender, EventArgs e)
  86.         {
  87.             MainPG.SelectedObject = MainLB.SelectedItem;
  88.         }
  89.  
  90.         private void RefreshObjectListDataBindings()
  91.         {
  92.             MainLB.DataSource = null;
  93.             MainLB.DataSource = objectsBindingSource;
  94.         }
  95.  
  96.         private void MainPG_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
  97.         {
  98.             Debug.WriteLine(e.ChangedItem.DisplayName());
  99.             int selectedIndex = MainLB.SelectedIndex;
  100.             RefreshObjectListDataBindings();
  101.             MainLB.SelectedIndex = selectedIndex;
  102.         }
  103.  
  104.         private void DeleteBT_Click(object sender, EventArgs e)
  105.         {
  106.             if (MainLB.SelectedItems.Count == 0) return;
  107.             Objects.Remove((T)objectsBindingSource.Current);
  108.             RefreshObjectListDataBindings();
  109.         }
  110.  
  111.         private void UpBT_Click(object sender, EventArgs e)
  112.         {
  113.             int newIndex = Math.Max(MainLB.SelectedIndex - 1, 0);
  114.             Objects.Move(MainLB.SelectedIndex, newIndex);
  115.             RefreshObjectListDataBindings();
  116.             MainLB.SelectedIndex = newIndex;
  117.         }
  118.  
  119.         private void DownBT_Click(object sender, EventArgs e)
  120.         {
  121.             int newIndex = Math.Min(MainLB.SelectedIndex + 1, MainLB.Items.Count - 1);
  122.             Objects.Move(MainLB.SelectedIndex, newIndex);
  123.             RefreshObjectListDataBindings();
  124.             MainLB.SelectedIndex = newIndex;
  125.         }
  126.  
  127.     }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement