Save

created the save as

Page 1 of 1

3 Replies - 1748 Views - Last Post: 18 June 2009 - 08:28 AM Rate Topic: -----

#1 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Save

Posted 17 June 2009 - 01:54 PM

i have created a small app in which he user opens some small files...txt and some other extensions.
there he can modify the document he opened, and now i want to create a save button.

i did it for the save as button:
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            saveFileDialog1.FileName = sub;
            if (DialogResult.OK == saveFileDialog1.ShowDialog())
            {

                string input = richTextBox1.Text;



                using (Stream stream = saveFileDialog1.OpenFile())
                {

                    using (StreamWriter writer = new StreamWriter(stream))
                    {

                        writer.Write(input);

                    }

                }



                MessageBox.Show("Saving the document was successful.");

            }



but how can i create a "save" button, so that when he hits the button, he overwrites the document he opened with his doc.

and also for the save as button...how can i make it so that if the user opened a .txt file he will only be able to save the file he modifies with .txt, and this for every extension he gets. so that he is not able to modify the extension of the document. the saved document extension to be the same with the extension of the document he opened.

thx in advance :)

This post has been edited by Bocard: 17 June 2009 - 01:56 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Save

#2 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Save

Posted 17 June 2009 - 02:02 PM

You could create a boolean to store whether any changes have been made. If so, just save over the file. Otherwise, show a save file dialog.

You can see if the user has changed any data by using the rich text box's "TextChanged" event.

For Example:

if (txtChanged)
{
    if (saveFileDialog.ShowDialog() == DialogResult.Ok)
    {
        richTextBox1.SaveFile(saveFileDialog.FileName, RichTextBoxStreamType.PlainText);
    }
}
else
{
    richTextBox1.SaveFile(fileName, RichTextBoxStreamType.PlainText);
}



To keep the user from saving in different extensions, add a filter to the saveFileDialog:

saveFileDialog1.Filter = "Text Files|*.txt";


This post has been edited by lesPaul456: 17 June 2009 - 02:27 PM

Was This Post Helpful? 0
  • +
  • -

#3 Bocard  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 223
  • Joined: 24-September 08

Re: Save

Posted 18 June 2009 - 03:02 AM

thx man i'll give it a try.

Quote

To keep the user from saving in different extensions, add a filter to the saveFileDialog:

saveFileDialog1.Filter = "Text Files|*.txt";



this will work if the user opens a .txt file. but he might also open a doc...or any other document formats..rtf, etc. How can i keep him from saving in a different extension. If he opens a .rtf i want him to be able to save his document only in .rtf....and so on for any extension. Do you have any idea how can I achieve this?
Was This Post Helpful? 0
  • +
  • -

#4 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 173
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Save

Posted 18 June 2009 - 08:28 AM

You can grab the extension when the user opens the file, and then add that to the filter:

First, add the "System.IO" namespace:
using System.IO;




Now, load the file and get the extension:

// These should be global
string fileName;
string extension;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
    fileName = openFileDialog.FileName;
    extension = Path.GetExtension(fileName);
}



Now, just add the extension to the filters:

saveFileDialog.Filter = string.Format("{0}|*{1}", extension, extension);



Hope this helps!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1