To detect if the collection editor for the .NET PropertyGrid has the OK or Cancel button clicked you can use the below code and subscribe to the MyFormOK or MyFormCancel events.
public class MyCollectionEditor : CollectionEditor
{
public delegate void MyFormClosedEventHandler(object sender, FormClosedEventArgs e);
public delegate void MyFormCancelEventHandler(object sender);
public static event MyFormClosedEventHandler MyFormOK;
public static event MyFormCancelEventHandler MyFormCancel;
private bool cancel;
public MyCollectionEditor(Type type) : base(type) { }
protected override CollectionForm CreateCollectionForm()
{
CollectionForm collectionForm = base.CreateCollectionForm();
collectionForm.FormClosed += new FormClosedEventHandler(collection_FormClosed);
cancel = false;
return collectionForm;
}
protected override void CancelChanges()
{
if (MyFormCancel != null)
{
MyFormCancel(this);
}
cancel = true;
base.CancelChanges();
}
void collection_FormClosed(object sender, FormClosedEventArgs e)
{
if (MyFormOK != null && !cancel)
{
MyFormOK(this, e);
}
}
}
Activate the enhanced MyCollection editor:
[Editor(typeof(MyCollectionEditor), typeof(UITypeEditor))]
public List<Person> UserList { get; set; } = new List<Person>();