Bravo659
Hello I need some assistance in the MDI ChildForm to save contents of the multiline textbox in the ChildForm and open the text file. It writes to the text file but not will not close nor open with the open existing file or the open file. Below is the code i have generated.
I should be able to save the contents of the ChildForm and open existing file form the MDI parent form and the open a file to the ChildForm. Can you assist? Thanks.

public partial class TextEditorForm : Form
    {
        public int ChildOneCounterInteger;
        StreamWriter editorStreamWriter;


        public TextEditorForm()
        {
            InitializeComponent();
        }

        private void NewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ChildForm childOneForm = new ChildForm();
            childOneForm.MdiParent = this;
            childOneForm.Show();
            ChildOneCounterInteger++;
            childOneForm.Text = "Child One Document " + ChildOneCounterInteger.ToString();
            childOneForm.Show();

        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            DialogResult responseDialogResult;
            ofd.Filter = "Text Files | *.txt | All Files | *.* | RichText Files | *.rtf";
            ofd.InitialDirectory = Directory.GetCurrentDirectory();
            responseDialogResult = ofd.ShowDialog();

            if (responseDialogResult != DialogResult.Cancel)
            {
                StreamReader sr;
                sr = new StreamReader(ofd.FileName);
                sr.Close();
            }
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();

        }

        private void tileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void openExistingToolStripMenuItem_Click(object sender, EventArgs e)
        {

            DialogResult responseDialogResult;
            string fileTextString = " ";

            ofd.InitialDirectory = Directory.GetCurrentDirectory();
            responseDialogResult = ofd.ShowDialog();
            if (responseDialogResult != DialogResult.Cancel)
            {
                StreamReader sw = new StreamReader(ofd.FileName);

                while (sw.Peek() != 1)
                {
                   fileTextString = (sw.ReadLine());
                }
                sw.Close();
            }
            ChildForm childOneForm = new ChildForm();
            childOneForm.MdiParent = this;
            ChildOneCounterInteger++;
            childOneForm.Text = "Child One Document " + ChildOneCounterInteger.ToString();
            childOneForm.txtEditor.Text = fileTextString;
            childOneForm.Show();
        }

        private void TextEditorForm_Load(object sender, EventArgs e)
        {
            ChildForm childOneForm = new ChildForm();
            childOneForm.MdiParent = this;
            childOneForm.Show();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();

            sfd.Title = "Save a Text File";
            sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";

            DialogResult dr = sfd.ShowDialog();
            if (dr == DialogResult.OK)
            {
                System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName);

                Form activeChildForm = this.ActiveMdiChild;

                if (activeChildForm != null)
                {
                    TextBox txtEditor = activeChildForm.ActiveControl as TextBox;

                    if (txtEditor != null)
                    {
                        sw.Write(txtEditor.Text);
                    }

                    sw.Close();
                    

                }
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutBox1 aAboutForm = new AboutBox1();
            aAboutForm.ShowDialog();
        }
    }

I appreciate for your assistance and let me know what I am doing wrong so i can fix the problem.


Please disregard this message is not part of the posting above. Seems its a coding or server error in part of Daniweb. Error message is too short.
With this extra text it allow me to post to this thread.

Have a look at following code.

Mdiform - Form1.cs

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Text = "Untitled";
            frm.MdiParent = this;
            frm.Show();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.HasChildren)
            {
              DialogResult res=saveFileDialog1.ShowDialog();
              if (res != DialogResult.Cancel)
              {
                  string path=saveFileDialog1.FileName;

                  Form2 child = this.ActiveMdiChild as Form2;
                  string content = child.FileContent;
                  System.IO.File.WriteAllText(path, content);
                  child.Text = path;
              }
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
                     
                DialogResult res = openFileDialog1.ShowDialog();

                if (res != DialogResult.Cancel)
                {
                    string path = openFileDialog1.FileName;

                    if (this.ActiveMdiChild==null)
                    {
                        Form2 frm = new Form2();
                        frm.MdiParent = this;
                        frm.Show();
                        frm.Activate();
                    }
                    Form2 child = this.ActiveMdiChild as Form2;
                    child.FileContent = System.IO.File.ReadAllText(path);
                    child.Text = path;
                }
           
        }
    }

Child form - Form2.cs (Multiline TextBox padded center)

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

      
        public string FileContent
        {
            get
            {

                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }

    }

Have a look at following code.

Mdiform - Form1.cs

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void newToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Text = "Untitled";
            frm.MdiParent = this;
            frm.Show();
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (this.HasChildren)
            {
              DialogResult res=saveFileDialog1.ShowDialog();
              if (res != DialogResult.Cancel)
              {
                  string path=saveFileDialog1.FileName;

                  Form2 child = this.ActiveMdiChild as Form2;
                  string content = child.FileContent;
                  System.IO.File.WriteAllText(path, content);
                  child.Text = path;
              }
            }
        }

        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
                     
                DialogResult res = openFileDialog1.ShowDialog();

                if (res != DialogResult.Cancel)
                {
                    string path = openFileDialog1.FileName;

                    if (this.ActiveMdiChild==null)
                    {
                        Form2 frm = new Form2();
                        frm.MdiParent = this;
                        frm.Show();
                        frm.Activate();
                    }
                    Form2 child = this.ActiveMdiChild as Form2;
                    child.FileContent = System.IO.File.ReadAllText(path);
                    child.Text = path;
                }
           
        }
    }

Child form - Form2.cs (Multiline TextBox padded center)

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

      
        public string FileContent
        {
            get
            {

                return textBox1.Text;
            }
            set
            {
                textBox1.Text = value;
            }
        }

    }

Hello adatpost,
I appreciate for your help actually i studied your code and that would work fine. I found the problem with my code and it was within the while loop. By replacing the sw.Peek() != -1 and fileTextString = fileTextString + " " + (sw.ReadLine()); The program worked.

while (sw.Peek() != 1)                {                   fileTextString = (sw.ReadLine());                }                sw.Close();

I do appreciate your help.

You're welcome. I'm glad you got it working. Please mark this thread as solved if you have found an answer to your question.

commented: adatapost posted a good and clear logic to assist with the project. +5
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.