When the Form1_FormClosing property is Form1_FormClosing text from RichTextBox should be saved in the rtf format to the project folder. How to do it?

    2 answers 2

    You need to subscribe to the Form1_FormClosing event:

     private void Form1_FormClosing(object sender, FormClosingEventArgs e) { richTextBox1.SaveFile("richtext1.rtf"); } 
    • And how to make that was saved only when there is text in the RichTextBox? - Fanning
    • one
      Put the condition in the beginning: if (!string.IsNullOrWhiteSpace(richTextBox1.Text)) - koshe
      public void SaveMyFile() { SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.DefaultExt = "*.rtf"; saveFile1.Filter = "RTF Files|*.rtf"; if (saveFile1.ShowDialog() ==DialogResult.OK && saveFile1.FileName.Length > 0) { richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText); } } 
    • Without SaveFileDialog. The file itself should be saved. - Fanning
    • @Fanning, "I have to save myself" - this is generally the top condition. Nothing will happen by itself, call this function when you need to save the text to a file. - koks_rs
    • Is it really impossible to do without OpenFileDialog? So that when you close the program, the text is saved to a file without asking where to save. Something like a cache. - Fanning