G
Guest
I'm have a sample form, with a ListBox and a Remove button, I'm doing data
binding to a custom collection, everything is working find, I can bind the
list box, I can see the items appear on the list, I can remove items from the
list, BUT my problem is when I attempt to remove the last item on the list, I
get the following error:
System.ArgumentOutOfRangeException: Specified argument was out of the range
of valid values. Parameter name: Index was out of range. Must be
non-negative and less than the size of the collection
at
System.Collections.CollectionBase.System.Collections.IList.get_Item(Int32
index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
This is the source code I'm using (C#):
Collection.cs
public class Collection : System.Collections.CollectionBase
{
private Collection()
{
}
public static Collection NewCollection()
{
return new Collection();
}
public void Remove(CollectionItem item)
{
if (this.List.Contains(item))
this.List.Remove(item);
}
public int Add(string displayText, string valueText)
{
return this.List.Add(CollectionItem.NewCollectionItem(displayText,
valueText));
}
public CollectionItem this[int index]
{
get{return (CollectionItem)this.List[index];}
}
}
CollectionItem.cs
public class CollectionItem
{
private string _displayText = string.Empty;
private string _valueText = string.Empty;
public string DisplayText
{
get{return this._displayText;}
set{this._displayText = value;}
}
public string ValueText
{
get{return this._valueText;}
set{this._valueText = value;}
}
private CollectionItem(string displayText, string valueText)
{
this._displayText = displayText;
this._valueText = valueText;
}
public static CollectionItem NewCollectionItem(string displayText, string
valueText)
{
return new CollectionItem(displayText, valueText);
}
}
Form code:
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox lisItems;
private Collection items = Collection.NewCollection();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
items.Add("One", "1");
items.Add("Two", "2");
items.Add("Three", "3");
this.lisItems.DataSource = items;
this.lisItems.DisplayMember = "DisplayText";
this.lisItems.ValueMember = "ValueText";
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
if (this.lisItems.SelectedItem == null)
return;
CollectionItem item = (CollectionItem)this.lisItems.SelectedItem;
this.lisItems.SuspendLayout();
this.lisItems.DataSource = null;
items.Remove(item);
this.lisItems.DataSource = items;
this.lisItems.DisplayMember = "DisplayText";
this.lisItems.ValueMember = "ValueText";
this.lisItems.ResumeLayout();
}
}
NOTE: I just paste the relevant code!
Upon clicking the Remove button, I'm removing the selected item from the
collection and re-binding the collection. I can remove any element, BUT ONLY
THE LAST ONE GIVES ME THE ERROR.
Thanks in advance for your help
binding to a custom collection, everything is working find, I can bind the
list box, I can see the items appear on the list, I can remove items from the
list, BUT my problem is when I attempt to remove the last item on the list, I
get the following error:
System.ArgumentOutOfRangeException: Specified argument was out of the range
of valid values. Parameter name: Index was out of range. Must be
non-negative and less than the size of the collection
at
System.Collections.CollectionBase.System.Collections.IList.get_Item(Int32
index)
at System.Windows.Forms.CurrencyManager.EndCurrentEdit()
at System.Windows.Forms.CurrencyManager.ChangeRecordState(Int32
newPosition, Boolean validating, Boolean endCurrentEdit, Boolean
firePositionChange, Boolean pullData)
at System.Windows.Forms.CurrencyManager.set_Position(Int32 value)
at System.Windows.Forms.ListBox.OnSelectedIndexChanged(EventArgs e)
at System.Windows.Forms.ListBox.WmReflectCommand(Message& m)
at System.Windows.Forms.ListBox.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg,
IntPtr wparam, IntPtr lparam)
This is the source code I'm using (C#):
Collection.cs
public class Collection : System.Collections.CollectionBase
{
private Collection()
{
}
public static Collection NewCollection()
{
return new Collection();
}
public void Remove(CollectionItem item)
{
if (this.List.Contains(item))
this.List.Remove(item);
}
public int Add(string displayText, string valueText)
{
return this.List.Add(CollectionItem.NewCollectionItem(displayText,
valueText));
}
public CollectionItem this[int index]
{
get{return (CollectionItem)this.List[index];}
}
}
CollectionItem.cs
public class CollectionItem
{
private string _displayText = string.Empty;
private string _valueText = string.Empty;
public string DisplayText
{
get{return this._displayText;}
set{this._displayText = value;}
}
public string ValueText
{
get{return this._valueText;}
set{this._valueText = value;}
}
private CollectionItem(string displayText, string valueText)
{
this._displayText = displayText;
this._valueText = valueText;
}
public static CollectionItem NewCollectionItem(string displayText, string
valueText)
{
return new CollectionItem(displayText, valueText);
}
}
Form code:
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox lisItems;
private Collection items = Collection.NewCollection();
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
items.Add("One", "1");
items.Add("Two", "2");
items.Add("Three", "3");
this.lisItems.DataSource = items;
this.lisItems.DisplayMember = "DisplayText";
this.lisItems.ValueMember = "ValueText";
}
private void btnRemove_Click(object sender, System.EventArgs e)
{
if (this.lisItems.SelectedItem == null)
return;
CollectionItem item = (CollectionItem)this.lisItems.SelectedItem;
this.lisItems.SuspendLayout();
this.lisItems.DataSource = null;
items.Remove(item);
this.lisItems.DataSource = items;
this.lisItems.DisplayMember = "DisplayText";
this.lisItems.ValueMember = "ValueText";
this.lisItems.ResumeLayout();
}
}
NOTE: I just paste the relevant code!
Upon clicking the Remove button, I'm removing the selected item from the
collection and re-binding the collection. I can remove any element, BUT ONLY
THE LAST ONE GIVES ME THE ERROR.
Thanks in advance for your help