4 Replies - 1759 Views - Last Post: 06 April 2011 - 10:16 AM Rate Topic: -----

#1 ITJobSeeker   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 22-October 09

Accessing another form and saving multiple files at once

Posted 05 April 2011 - 09:23 PM

I'm writing a basic text editor program that will open/save .txt, .shtml, and .html files. I want a feature that allows me to be able to save the same text as multiple files at once. I use 2 forms...1 for the actual editor, and the other for the multiple save option. On the multiple save option form, I have 9 text boxes and 9 buttons associated with each for the purpose of browsing for file locations to save to.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

//This program is a basic text editor that opens txt, shtml, and html files as text files
//in a textbox.

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

        public void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Opens the files and displays them in textBox1 (the main textbox)
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";
 
            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    string fileText = "";
                    StreamReader sr = new StreamReader(ofd.FileName);
                    fileText = sr.ReadToEnd();
                    textBox1.Text = fileText;
                        break;

                case (DialogResult.Cancel):
                        ofd.Dispose();
                        break;
                    
            }
        }

        public void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Saves the files to one single file name
            SaveFileDialog  sd = new SaveFileDialog();
            sd.Title = "Save As";
            sd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (sd.ShowDialog())
            {
                case (DialogResult.OK):
                    StreamWriter sw = new StreamWriter(sd.FileName);
                    sw.Write(textBox1.Text);
                    sw.Close();
                    break;
                case (DialogResult.Cancel):
                    sd.Dispose();
                    break;
            }
        }

        private void saveMultipleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //Opens up the instance of the save multiple form, which allows me to write
            //the file to multiple file names at once.
            SaveMultiple sm = new SaveMultiple();
            sm.ShowDialog();
        }
    }
}



Then here is the code for the multiple save form.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class SaveMultiple : Form
    {
        public SaveMultiple()
        {
            InitializeComponent();
        }
            

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox1.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox2.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox3.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button4_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox4.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox5.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox6.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button7_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox7.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox8.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }

        private void button9_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Save As";
            ofd.Filter = "SHTML files | *.shtml|Text files | *.txt|HTML files | *.html";

            switch (ofd.ShowDialog())
            {
                case (DialogResult.OK):
                    textBox9.Text = ofd.FileName;
                    break;
                case (DialogResult.Cancel):
                    ofd.Dispose();
                    break;
            }
        }


        private void buttonSave_Click(object sender, EventArgs e)
        {
            //I want to be able to use this button to save to all the file paths in the textboxes
            //but don't know how to access the text in textBox1 in my other form. As you
            //may be able to tell, I'm running into some confusion here.
            Form1 f1 = new Form1();
            StreamWriter sw = new StreamWriter(textBox1.Text, textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text, textBox7.Text, textBox8.Text, textBox9.Text);

        }
    }
}



As you can tell by my comments, by the time I get to the buttonSave_Click method, I don't know how to save the text from the other form's textbox to all of the filenames in the textboxes in the SaveMultiple form. Here's my main problems...

I need to know how to access the controls on my Form1 from the SaveMultiple form. How do I access the texBox1.text from Form1 while using SaveMultiple??

I don't know how to write to all of the filenames in all of the textboxes at once. My sw instance of StreamWriter won't accept all 9 arguments like I'm trying to pass to it. Is there another class that will save to multiple files at once??

Thanks for any help you guys can give me.

Brandon

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing another form and saving multiple files at once

#2 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Accessing another form and saving multiple files at once

Posted 05 April 2011 - 09:45 PM

Add this property to your first form:
public string Text { get { return textbox1.Text;}}


Change line 70 of Form1 to
SaveMultiple sm = new SaveMultiple(this);


Add the following members and constructor to SaveMultiple class
private Form1 frm;
private[] textboxes;
public SaveMultiple(Form1 form) {
    frm = form;
    InitializeComponent();
    // yes, there are other ways to do this, just doing this way because it's easy
    textboxes = {textbox1, textbox2, textbox3, textbox4, textbox5, textbox6, textbox7, textbox8, textbox9};
}


Delete the default constructor from SaveMultiple (this will prevent you from using it incorrectly).

Replace save button event with:
private void buttonSave_Click(object sender, EventArgs e) {
    foreach (Textbox t in textboxes) {
        using (StreamWriter sw = new StreamWriter (t.Text)) {
            sw.Write(frm.Text);
        }
    }
}


If you want to watch your system spaz while attempting to write 9 files at once:
private void buttonSave_Click(object sender, EventArgs e) {
    Parallel.For(0, textboxes.Length, (i) => {
        using (StreamWriter sw = new StreamWriter(textboxes[i].Text)) {
            sw.Write(frm.Text);
        }
    });
}


Warning: This is off the top of my head, if any typos appear, I blame my cat.

This post has been edited by Momerath: 05 April 2011 - 09:46 PM

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Accessing another form and saving multiple files at once

Posted 06 April 2011 - 08:51 AM

View PostMomerath, on 05 April 2011 - 10:45 PM, said:

Add this property to your first form:
public string Text { get { return textbox1.Text;}}

A form already has a Text property. So you would need to add 'new' to this line to indicate you are deliberatly hiding the built-in property.
new public string Text { get { return textbox1.Text;}}

And you realize that by doing this you won't be able to reach the original .Text property of the Form, which is how you change the text seen in the titlebar of the form.

I think it's bad to hide the built-in property when there is no real need for it. Just use a different name that doesn't hide it, like UserText, then adjust the rest of your code accordingly.

Quote

Change line 70 of Form1 to
SaveMultiple sm = new SaveMultiple(UserText);

Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Accessing another form and saving multiple files at once

Posted 06 April 2011 - 09:00 AM

If it were me, I'd do this in the opposite direction. SaveMultiple would have a List<string> of file names as a property, and when it was closed, I'd grab the list in the original form, and then save them there.

But it's all just a stylistic choice. I prefer to keep as much of the IO code together instead of spreading it across two forms.
Was This Post Helpful? 0
  • +
  • -

#5 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1021
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: Accessing another form and saving multiple files at once

Posted 06 April 2011 - 10:16 AM

View PosttlhIn`toq, on 06 April 2011 - 08:51 AM, said:

View PostMomerath, on 05 April 2011 - 10:45 PM, said:

Add this property to your first form:
public string Text { get { return textbox1.Text;}}

A form already has a Text property. So you would need to add 'new' to this line to indicate you are deliberatly hiding the built-in property.


Was not intentional, was just typing stuff in. Yes, I wouldn't hide the property. My cat suggested I use Text as the name. It's his fault.

This post has been edited by Momerath: 06 April 2011 - 03:09 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1