[C#] "Value cannot be null" Help
I have a method that runs if either Form1 loads (the main form) or if Form2 is closed. The method populates a combobox from a text file. When the form loads, the combobox populates just fine. But, when Form2 is closed I get a "Value cannot be null. Parameter name: item". I'm lost as to why it works when the form loads, but not when the 2nd form closes.
The code for the method in question (which is located in Form1):
public void PrinterDD()
{
string sprinters = @"pathtofile";
using (StreamReader sr = new StreamReader(sprinters))
{
foreach (object item in sprinters)
{
string strPrnItem = sr.ReadLine();
printer.Items.Add(strPrnItem); //error occurs here
}
}
}
The code in Form2:
private void accept_Click(object sender, EventArgs e)
{
this.AcceptButton = accept;
if (selectedprinters != null && selectedprinters.Items != null && selectedprinters.Items.Count > 0)
{
string txtfile=@"pathtofile";
File.Delete(txtfile);
using (StreamWriter writer = new StreamWriter(File.OpenWrite(txtfile)))
{
foreach (object item in selectedprinters.Items)
{
writer.WriteLine(item.ToString());
}
}
}
Voicemails.PrinterDD();
this.Close();
}
Posted Dec 2, 2011, 12:50 AM UTC · edited
23