none
Save and Save As file dialog

    Question

  • Hi, I would like to be able to save the users data that is in richTextBox1, if not done so already. It will pop up the Save As Dialog.

    If a user has saved their work and clicks save, it saves to the file path it is already in.

    Any help?

    It would be much appreciated because i am a n00b

    Friday, January 18, 2013 12:20 AM

Answers

All replies

  • Here is an example of how ths can be done.  The form also tracks whether the file needs saving when the form is closed.  You basically need to track whether text events have changed in the richtextbox prior to the save button being clicked.  The save function either uses the last filename, or forces a call to SaveAs, which if a filename is set, then calls Save.
     
    using System;
    using System.Drawing;
    using System.Windows.Forms;
     
    namespace WindowsFormsApplication1
    {
     public partial class Form1 : Form
     {
      Button SaveButton;
      RichTextBox Editor;
      bool NeedsSaving = false;
     
      // current saved file
      string fn = string.Empty;
     
      void Save()
      {
       if (fn == string.Empty)
        SaveAs();
       Editor.SaveFile(fn);
       NeedsSaving = false;
      }
     
      void SaveAs()
      {
       SaveFileDialog sfd = new SaveFileDialog();
       sfd.Filter = "Rich Text Files (*.rtf)|*.rtf";
     
       if (sfd.ShowDialog() == DialogResult.OK)
       {
        fn = sfd.FileName;
        Save();
       }
      }
     
      public Form1()
      {
       InitializeComponent();
     
       SaveButton = new Button()
       {
        Text = "Save",
        Location = new Point(10, 130),
        Enabled = false
       };
       Controls.Add(SaveButton);
       SaveButton.Click += delegate(object sender, EventArgs e)
        {
         Save();
        };
     

       Editor = new RichTextBox()
       {
        Location = new Point(10, 10), Size = new Size(200, 80)
       };
       Editor.TextChanged += delegate(object sender, EventArgs e)
        {
         SaveButton.Enabled = Editor.Text.Length > 0;
         NeedsSaving = true;
        };
     
       Controls.Add(Editor);
     

       FormClosing += delegate(object sender, FormClosingEventArgs e)
        {
         if (NeedsSaving)
          Save();
        };
      }
     }
    }

    --
    Mike
    Friday, January 18, 2013 1:40 AM
  • Check out RichTextBox.SaveFile: http://msdn.microsoft.com/en-us/library/e4a710b1.aspx example is included at the link.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Marked as answer by jesushimself Friday, January 18, 2013 3:19 AM
    Friday, January 18, 2013 1:50 AM
  • Thank you sooo much. Extremely simple and effective
    Friday, January 18, 2013 3:20 AM
  • Your welcome and happy coding.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Friday, January 18, 2013 3:22 AM