I have a file - open - openToolStripMenuItem in form1, and I have a listbox1 in form2, I want to open file from form1 (openfiledialog1) and then readwrite the txt file content into the listbox1 on form2. The only problem is that listbox1 does not exist in current context in form1, how do I get openToolStripMenuItem in form1 to see listbox1 on form2 ??? L
public void openToolStripMenuItem_Click(object sender, EventArgs e) { FileInfo ReadFile = new FileInfo(@"c:\New Folder\BBBBOOOOO.txt"); StreamReader sr = ReadFile.OpenText(); while (!sr.EndOfStream) { listBox1.Items.Add(sr.ReadLine()); } }
Loading
Sam HobbsPosted Oct 23, 2010, 5:58 PM
Update: Note that Jure and I replied in parallel; I did not see Jure's reply before I replied. I think that when I said "a class to contain the data" I am trying to describe something similar to what Jure says about a controller.
JurePosted Oct 23, 2010, 5:43 PM
Or, which is what I would do, you can create some sort of controller, so that you're not adding items to your listbox directly from Form1, but through a controlled environment (this could simply be a set of functions). That way you can simply send the file name to the controller and let the controller examine the file and extract data properly, and then finally add the collection to the listbox. I think it's best to make controller completely separate from both forms (i.e. to not implement controller inside Form2), however, implement the adding itself inside Form2.
In short, you have to make listbox in Form2 accessible to Form1 in some way (directly or indirectly).