Question Opening a file from a parent form into a child form


New member
Jun 22, 2014
Programming Experience
Hey everyone,
I'm new at c# and I'm having some difficulties with a project I'm doing. I have a mdi program with two forms. I need to open a file into a richTextBox in the child form from the parent form. I don't know what I'm doing wrong. Any help would be much appreciated. Thanks! -Evans1404

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            ofd.Filter = "Text Files|*.txt";
            openFileDialog1.Title = "";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
                Form2 f2 = new Form2();
                f2.MdiParent = this;
                StreamReader sr = new StreamReader(openFileDialog1.FileName);
                Text = sr.ReadToEnd();
Last edited by a moderator:


C# Forum Moderator
Staff member
Apr 23, 2011
Sydney, Australia
Programming Experience
The proper way to handle this is to write a method in the child form that does what needs to be done and then call that method from the parent form. If the method is going to be called from outside the type that it's declared in, it needs to be declared 'public'.

It's up to you whether you write the method to take a file path as an argument and then have it open the file itself or have it take the text from an already opened file. Given that the RichTextBox itself has a LoadFile method to read in a file, you might lean towards the former. The latter will still work though, and you'll simply assign the string to the Text property of the control.

I would also suggest calling that method before calling Show on the child form. That way, the text is already there when the form is displayed.
Top Bottom