I’m making a windows forms application which is involving several forms in C#.
I call the second form from the main one with ShowDialog().
Then I try to populate a combo box (on the second form) with custom objects. The custom class has a method which converts a DataTable into an ArrayList. But, whenever I try to add an item to the ArrayList the event handler for a SplitPanel paint event is raised. Further more, that panel is placed on the main form.
Here is the code:
This is where I display the second form:
frmInsertSurovina insertSurovina = new frmInsertSurovina(0);
insertSurovina.ShowDialog();
This is where I populate the combobox:
private void frmInsertSurovina_Load(object sender, EventArgs e)
{
if (mode == 0) button1.Text = "Внеси";
else button1.Text = "Зачувај";
DB data=new DB();
DataTable devizi = data.GetDevizi();
DeviziCollection collDevizi = new DeviziCollection();
collDevizi.LoadCollection(devizi);
cmbDeviza.DataSource = collDevizi.Collection;
cmbDeviza.DisplayMember = "Text";
cmbDeviza.ValueMember = "Value";
//some other code here
}
This is the custom class:
public class DeviziCollection
{
public ArrayList Collection { get; set; }
public DeviziCollection()
{
}
public void LoadCollection(DataTable table)
{
foreach (DataRow row in table.Rows)
{
Deviza item = new Deviza();
item.Text = row["Naziv"].ToString();
item.Value = row["ID"].ToString();
Collection.Add(item);
}
}
}
Now, on the last line Collection.Add(Item); the execution of frmInsertSurovina_Load beaks and the program jumps somewhere undefined. (I mentioned before that a paint event raises, but I deleted it form the code and it doesn’t raise anymore. When I’m debugging, the program just continues to function with the Load event handler unfinished. What is interesting is that the code for the Paint event handler was just after the code block where I’m calling the second form).
Can this be a bug?
Please help!
It was an exception after all. I just added
in the LoadCollection() method.