Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You should try adding the file name or FileInfo from which the rich text is loaded from. If the file has not been saved, prompt to save the file. Otherwise, save to the file info cached.</p> <pre><code>partial class YourForm : Form { string filePath; private void SaveMyFile_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(filePath)) { SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.DefaultExt = "*.rtf"; saveFile1.Filter = "RTF Files|*.rtf"; if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &amp;&amp; saveFile1.FileName.Length &gt; 0) { filePath = saveFile1.FileName; } else return; } try { richTextBox1.SaveFile(filePath, RichTextBoxStreamType.PlainText); } catch (Exception ee) { // Put exception handling code here } } } </code></pre> <p>As per comment below, if you'd like a save as button, you can try the following:</p> <pre><code>partial class YourForm : Form { Button saveFileAsButton; // Add this using the Forms Designer private void saveFileAsButton_Click(object sender, EventArgs e) { SaveFileDialog saveFile1 = new SaveFileDialog(); saveFile1.DefaultExt = "*.rtf"; saveFile1.Filter = "RTF Files|*.rtf"; if (saveFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &amp;&amp; saveFile1.FileName.Length &gt; 0) { try { richTextBox1.SaveFile(saveFile1.FileName, RichTextBoxStreamType.PlainText); filePath = saveFile1.FileName; } catch (Exception ee) { // Put exception handling code here (e.g. error saying file cannot be saved) } } } } </code></pre> <p>Note how the setting of <code>filePath</code> is in the try block. In case the save failed, you don't want to lose your original file path.</p> <p>If you form has a <code>MenuStrip</code>, I recommend moving the save functions into the menus.</p> <p>(BTW, the type name for RTF in your filter would be better as "<code>Rich Text Document</code>".)</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload