So I'm moderately confused. I'm instantiating an object called Form2 in Form1 of my main program. In Form1 I am consistently reading and altering the data of listbox controls with no issues of cross threading. However, now that I'm in Form2 this issue seems to popup every time I attempt to run the program.
namespace WindowsFormsApplication1
{
//from program.cs file
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1()); //form 2 is instantiated in Form1
}
}
//In my Form2.Designer.CS file
partial class Form2
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false. </param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// directories
//
this.directories.FormattingEnabled = true;
this.directories.Location = new System.Drawing.Point(323, 80);
this.directories.Name = "directories";
this.directories.Size = new System.Drawing.Size(241, 186);
this.directories.TabIndex = 0;
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// Form2
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(578, 578);
this.Controls.Add(this.directories);
this.Name = "Form2";
this.Text = "Form2";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public System.Windows.Forms.ListBox directories;
private System.ComponentModel.BackgroundWorker backgroundWorker1;
}
//separated into my Form2.CS file
partial class Form2
{
private void PopulateListBox(ListBox lsb, string directory) //populates a given list block. takes the name of a list block and the directory to be seen.
{
List<string> fileList = new List<string>();
DirectoryInfo dinfo = new DirectoryInfo(directory);
FileInfo[] Files = dinfo.GetFiles("*.xml"); //only display xmls. we don't care about other file types at this point.
foreach (FileInfo file in Files)
{
string[] aFolder = file.Name.Split('.');
fileList.Add(aFolder[0]); //adds items to the list block
}
fileList.Sort();
foreach (string fileName in fileList)
{
lsb.Items.Add(fileName); //adds items to the list block
}
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
//heres where I have the error thrown
DirectoryInfo dinfo = new DirectoryInfo(baseDirectory + directories.SelectedItem.ToString());
FileInfo[] Files = dinfo.GetFiles("*.xml");
XmlDocument doc = new XmlDocument();
int currentInterval = 0;
//More occurs after this point but I can't list the info.
}
}
}
The frustrating part is that I can still load data into the listbox no issues. The only issue I'm having is when I'm attempting to access the data I've stored in the listbox. I have the listbox.SelectionMode set to multi-select. Tried it with singular select as well. Any help would be greatly appreciated. I am not attempting to alter the contents of the listbox in any way. Simply access the data I've already stored there.