Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can create your own collection editor, and hook into events on the default editor's controls. You can use these events to, say, disable the OK button. Something like:</p> <pre><code>public class MyCollectionEditor : CollectionEditor { private static Dictionary&lt;CollectionForm, Button&gt; okayButtons = new Dictionary&lt;CollectionForm, Button&gt;(); // Inherit the default constructor from CollectionEditor public MyCollectionEditor(Type type) : base(type) { } // Override this method in order to access the containing user controls // from the default Collection Editor form or to add new ones... protected override CollectionForm CreateCollectionForm() { CollectionForm collectionForm = base.CreateCollectionForm(); collectionForm.FormClosed += new FormClosedEventHandler(collectionForm_FormClosed); collectionForm.Load += new EventHandler(collectionForm_Load); if (collectionForm.Controls.Count &gt; 0) { TableLayoutPanel mainPanel = collectionForm.Controls[0] as TableLayoutPanel; if ((mainPanel != null) &amp;&amp; (mainPanel.Controls.Count &gt; 7)) { // Get a reference to the inner PropertyGrid and hook // an event handler to it. PropertyGrid propertyGrid = mainPanel.Controls[5] as PropertyGrid; if (propertyGrid != null) { propertyGrid.PropertyValueChanged += new PropertyValueChangedEventHandler( propertyGrid_PropertyValueChanged); } // Also hook to the Add/Remove TableLayoutPanel buttonPanel = mainPanel.Controls[1] as TableLayoutPanel; if ((buttonPanel != null) &amp;&amp; (buttonPanel.Controls.Count &gt; 1)) { Button addButton = buttonPanel.Controls[0] as Button; if (addButton != null) { addButton.Click += new EventHandler(addButton_Click); } Button removeButton = buttonPanel.Controls[1] as Button; if (removeButton != null) { removeButton.Click += new EventHandler(removeButton_Click); } } // Find the OK button, and hold onto it. buttonPanel = mainPanel.Controls[6] as TableLayoutPanel; if ((buttonPanel != null) &amp;&amp; (buttonPanel.Controls.Count &gt; 1)) { Button okayButton = buttonPanel.Controls[0] as Button; if (okayButton != null) { okayButtons[collectionForm] = okayButton; } } } } return collectionForm; } private static void collectionForm_FormClosed(object sender, FormClosedEventArgs e) { CollectionForm collectionForm = (CollectionForm)sender; if (okayButtons.ContainsKey(collectionForm)) { okayButtons.Remove(collectionForm); } } private static void collectionForm_Load(object sender, EventArgs e) { ValidateEditValue((CollectionForm)sender); } private static void propertyGrid_PropertyValueChanged(object sender, PropertyValueChangedEventArgs e) { ValidateEditValue((CollectionForm)sender); } private static void addButton_Click(object sender, EventArgs e) { Button addButton = (Button)sender; ValidateEditValue((CollectionForm)addButton.Parent.Parent.Parent); } private static void removeButton_Click(object sender, EventArgs e) { Button removeButton = (Button)sender; ValidateEditValue((CollectionForm)removeButton.Parent.Parent.Parent); } private static void ValidateEditValue(CollectionForm collectionForm) { if (okayButtons.ContainsKey(collectionForm)) { Button okayButton = okayButtons[collectionForm]; IList&lt;MyClass&gt; items = collectionForm.EditValue as IList&lt;MyClass&gt;; okayButton.Enabled = MyCollectionIsValid(items); } } private static bool MyCollectionIsValid(IList&lt;MyClass&gt; items) { // Perform validation here. return (items.Count == 2); } } </code></pre> <p>You will also need to add an Editor attribute to you collection:</p> <pre><code>class MyClass { [Editor(typeof(MyCollectionEditor), typeof(System.Drawing.Design.UITypeEditor))] List&lt;Foo&gt; MyCollection { get; set; } } </code></pre> <p><strong>NOTE:</strong> I found that the value of items in removeButton_Click was not correct - so some tweaking may need to take place.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload