D
Developer
Hello,
I have a mysterious problem with a class derived from CollectionBase, and
its use of the collection's List.
public class MyTypes : CollectionBase, Windows.Forms.IExplorerMember,
ICloneable
An editor control for a property grid has two list boxes, and buttons to
move items back and forth.
The editor control's ctor is passed a MyTypes instance. The editor control
attaches event handlers to the MyTypes collection for ItemInserted,
ItemsChanged, and a few other events.
When I click the button to move an item from the left list to the right --
to add it to the MyTypes collection -- the event handlers are called. If I
remove the item, then add it again, the event handlers (ItemInserted and
ItemsChanged) are called as though there are two items in the collection's
List. If I remove the item (0 items in list now), and add it again, the
handlers are called as though there are 3 items in the list...
Any ideas on what is going on?
Thanks for any insights...
Here is some (simplified) source code:
[Serializable]
public class MyTypes : CollectionBase, Windows.Forms.IExplorerMember,
ICloneable
{
public MyTypes() {
}
public int Add(MyType value) {
return(List.Add(value));
}
public void Remove(MyType value) {
List.Remove(value);
}
protected override void OnInsertComplete(int index, object value) {
if (!m_Updating) {
base.OnInsertComplete (index, value);
OnItemInserted(value, new EventArgs());
OnItemsChanged(value, new EventArgs());
}
}
public event System.EventHandler ItemInserted;
protected virtual void OnItemInserted(object sender, System.EventArgs e) {
if (ItemInserted != null) {
ItemInserted(sender, e);
}
}
public event System.EventHandler ItemsChanged;
protected virtual void OnItemsChanged(object sender, System.EventArgs e) {
if (ItemsChanged != null) {
ItemsChanged(sender, e);
}
}
}
public class SomeTypeEditor : System.Windows.Forms.UserControl
{
private Core.MyGroup m_MyGroup;
private System.Windows.Forms.Button btnAddAll;
private System.Windows.Forms.Button btnAddMyType;
private System.Windows.Forms.Button btnRemoveMyType;
private System.Windows.Forms.Button btnRemoveAll;
private System.Windows.Forms.ListBox lstAllTypes;
private System.Windows.Forms.ListBox lstGroupTypes;
private System.ComponentModel.Container components = null;
public SomeTypeEditor(Core.MyGroup MyGroup)
{
InitializeComponent();
m_MyGroup = MyGroup;
this.MyTypes = MyGroup.MyTypes;
this.MyTypes.ItemsChanged += new EventHandler(MyTypes_ItemsChanged);
this.MyTypes.ItemInserted += new EventHandler(MyTypes_ItemInserted);
this.MyTypes.ItemRemoved += new EventHandler(MyTypes_ItemRemoved);
}
private void MyTypes_ItemsChanged(object sender, EventArgs e)
{
// Refresh the lists in the editor control.
}
private void MyTypes_ItemInserted(object sender, EventArgs e)
{
// add the item to storage.
Utils.DataEnvironmentHelper.InsertMyType(m_MyGroup, (Core.MyType)sender);
}
}
I have a mysterious problem with a class derived from CollectionBase, and
its use of the collection's List.
public class MyTypes : CollectionBase, Windows.Forms.IExplorerMember,
ICloneable
An editor control for a property grid has two list boxes, and buttons to
move items back and forth.
The editor control's ctor is passed a MyTypes instance. The editor control
attaches event handlers to the MyTypes collection for ItemInserted,
ItemsChanged, and a few other events.
When I click the button to move an item from the left list to the right --
to add it to the MyTypes collection -- the event handlers are called. If I
remove the item, then add it again, the event handlers (ItemInserted and
ItemsChanged) are called as though there are two items in the collection's
List. If I remove the item (0 items in list now), and add it again, the
handlers are called as though there are 3 items in the list...
Any ideas on what is going on?
Thanks for any insights...
Here is some (simplified) source code:
[Serializable]
public class MyTypes : CollectionBase, Windows.Forms.IExplorerMember,
ICloneable
{
public MyTypes() {
}
public int Add(MyType value) {
return(List.Add(value));
}
public void Remove(MyType value) {
List.Remove(value);
}
protected override void OnInsertComplete(int index, object value) {
if (!m_Updating) {
base.OnInsertComplete (index, value);
OnItemInserted(value, new EventArgs());
OnItemsChanged(value, new EventArgs());
}
}
public event System.EventHandler ItemInserted;
protected virtual void OnItemInserted(object sender, System.EventArgs e) {
if (ItemInserted != null) {
ItemInserted(sender, e);
}
}
public event System.EventHandler ItemsChanged;
protected virtual void OnItemsChanged(object sender, System.EventArgs e) {
if (ItemsChanged != null) {
ItemsChanged(sender, e);
}
}
}
public class SomeTypeEditor : System.Windows.Forms.UserControl
{
private Core.MyGroup m_MyGroup;
private System.Windows.Forms.Button btnAddAll;
private System.Windows.Forms.Button btnAddMyType;
private System.Windows.Forms.Button btnRemoveMyType;
private System.Windows.Forms.Button btnRemoveAll;
private System.Windows.Forms.ListBox lstAllTypes;
private System.Windows.Forms.ListBox lstGroupTypes;
private System.ComponentModel.Container components = null;
public SomeTypeEditor(Core.MyGroup MyGroup)
{
InitializeComponent();
m_MyGroup = MyGroup;
this.MyTypes = MyGroup.MyTypes;
this.MyTypes.ItemsChanged += new EventHandler(MyTypes_ItemsChanged);
this.MyTypes.ItemInserted += new EventHandler(MyTypes_ItemInserted);
this.MyTypes.ItemRemoved += new EventHandler(MyTypes_ItemRemoved);
}
private void MyTypes_ItemsChanged(object sender, EventArgs e)
{
// Refresh the lists in the editor control.
}
private void MyTypes_ItemInserted(object sender, EventArgs e)
{
// add the item to storage.
Utils.DataEnvironmentHelper.InsertMyType(m_MyGroup, (Core.MyType)sender);
}
}