0

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.

NoobWebDev
  • 161
  • 1
  • 1
  • 14
  • For all cross threading issues, suggested duplicate will help. Otherwise drop a comment. – Sriram Sakthivel Jan 02 '15 at 16:04
  • The answer you've listed is how to update a controls value from another thread. My code already successfully does that. I'm wondering why all of a sudden I can't access that control's data ? Especially since I've retrieved the value of directories.SelectedItem on a multi-select listbox in form1 without seeing this problem before. – NoobWebDev Jan 02 '15 at 16:13
  • 1
    Just pass `directories.SelectedItem` as a parameter to the Bgw. See [this answer](http://stackoverflow.com/a/4807200/60761) – H H Jan 02 '15 at 16:17
  • Thank you. That actually fixed the problem. Not totally sure why I didn't have to do that in a similar fashion in the previous form but I'll take it. – NoobWebDev Jan 02 '15 at 20:50

0 Answers0