Advertisement
stigzler

Untitled

May 27th, 2025
770
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. public class SupportApplications: object
  2. {
  3.     public string Name { get; set; } = "{Please Set}";
  4.     public ExecutionTime ExecutionTime { get; set; } = ExecutionTime.BeforeLaunch;
  5.     public ExecutionMode ExecutionMode { get; set; } = ExecutionMode.Exclusive;
  6. }
  7.  
  8. public class ComputerVM : BaseVM
  9. {
  10.     [Editor(typeof(GenericCollectionEditor), typeof(UITypeEditor))]
  11.     public List<SupportApplications> SupportApplications
  12.     { get { return Computer.BigBoxSupportApplications; } set { Computer.BigBoxSupportApplications = value; OnPropertyChanged(); } }
  13. }
  14.  
  15. internal class GenericCollectionEditor : UITypeEditor
  16. {
  17.     public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
  18.     {
  19.         if (context == null && context.Instance == null) return base.GetEditStyle(context);
  20.         return UITypeEditorEditStyle.Modal;
  21.     }
  22.  
  23.     public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
  24.     {
  25.         if (context == null || provider == null || context.Instance == null) return value;
  26.  
  27.         IWindowsFormsEditorService editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
  28.  
  29.         Type type = value.GetType().GetGenericArguments().SingleOrDefault() ?? typeof(object);
  30.  
  31.         GenericCollectionEditorForm form = new GenericCollectionEditorForm((List<object>)value, type);
  32.  
  33.         if (editorService.ShowDialog(form) == System.Windows.Forms.DialogResult.OK)
  34.         {
  35.             return form.Objects.ToList();
  36.         }
  37.  
  38.         return base.EditValue(context, provider, value);
  39.     }
  40. }
  41.  
  42. public partial class GenericCollectionEditorForm : FormViewBase
  43. {
  44.     public List<object> Objects { get; set; }
  45.  
  46.     private Type type = null;
  47.  
  48.     private BindingSource objectsBindingSource = new BindingSource();
  49.  
  50.     public GenericCollectionEditorForm(List<object> objects, Type type)
  51.     {
  52.         Objects = objects;
  53.         this.type = type;
  54.  
  55.         objectsBindingSource.DataSource = Objects;
  56.         MainLB.DataSource = objectsBindingSource;
  57.     }
  58.  
  59.     private void AddBT_Click(object sender, EventArgs e)
  60.     {
  61.         if (type == null) return;
  62.         Objects.Add(type);
  63.         RefreshEffectListDataBindings();
  64.     }
  65.  
  66.     private void MainLB_SelectedValueChanged(object sender, EventArgs e)
  67.     {
  68.         MainPG.SelectedObject = MainLB.SelectedItem;
  69.     }
  70.  
  71.     private void RefreshEffectListDataBindings()
  72.     {
  73.         MainLB.DataSource = null;
  74.         MainLB.DataSource = objectsBindingSource;
  75.     }
  76.  
  77.     private void DeleteBT_Click(object sender, EventArgs e)
  78.     {
  79.         if (MainLB.SelectedItems.Count == 0) return;
  80.         Objects.Remove(objectsBindingSource.Current);
  81.         RefreshEffectListDataBindings();
  82.     }
  83. }
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement