I have two ComboBoxes on a winform, one has all the values from a List "MyList" the other I want to have all the values from the selected index of the first, onwards. But I think there is a problem with when items are loaded into the ComboBoxes.
public partial class Form1 : Form
ComboBox box = new ComboBox();
box.DropDownStyle = ComboBoxStyle.DropDownList;
box.DataSource = MyList.ToList();
box.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
ComboBox box2 = new ComboBox();
box2.DropDownStyle = ComboBoxStyle.DropDownList;
foreach (object o in box.Items)
//This last line throws an error
//"InvalidArgument=Value of '0' is not valid for 'index'."
Data-binding will not work before the form and control be in
Created status and before the form and controls get visible they are not in
The problem here is because of above fact. You are using data-binding to add items to first combo in constructor and as mentioned above, the data-binding will not work there and so in the loop,
Items collection of first combo is empty yet and no items will be added to second combo.
You can solve the problem using either of these options:
Itemsof first combo:
Loadevent of the form.
Itemsof first combo. For example call
this.Show();after assigning values to
DataSourceof first combo. Calling
this.Visible = true;causes the form and all of its visible controls get
Created. So the controls should be visible and should be a member of visible part of a visible form.