Problem with CollectionBase.List

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);
}

}
 
J

Jeffrey Tan[MSFT]

Hi,

Thanks for your posting!

I think there is still something in your issue unclear for me:

#1, what does your "editor control" refer to? I think it should refer to
SomeTypeEditor class which inerits from UserControl. Do you attach this
SomeTypeEditor to the MyTypes type?
#2, There are some types in your application that are un-known types: such
as IExplorerMember, Core.MyGroup, Utils.DataEnvironmentHelper.InsertMyType
etc...

For your issue, I suggest you create a sample project for me to reproduce
your problem, then I can understand and find the cause for you. Without
sample project, there is no easy way for us to reproduce the problem.

I will wait for your further feedback, thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Ok, I will wait for your further feedback. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi,

Have you successfully created a sample project? Is your problem resolved?
Please feel free to tell me, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Developer

I am working on the sample, but can't reproduce the behavior my application
shows.
I'll post again if I can create an app that demonstrates the problem.
Thanks for following up....
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top