I need to implement something similar to Notepads' save option. Assuming I have a button placed next to a RichTextBox
, what I want is, when this button is clicked, a Dialogue box will open up, which will look similar to the one that appears when Save As is clicked. I would like to save the content of the RichTextBox in text format, by entering the name of file in the Save Dialogue box.
Asked
Active
Viewed 2.0k times
5

Random Dev
- 51,810
- 9
- 92
- 119

Shamim Hafiz - MSFT
- 21,454
- 43
- 116
- 176
-
Hmm, how did you know to use the [savefiledialog] tag? Fake question? – Hans Passant Sep 14 '11 at 12:04
-
1@Hans: I was typing out savefile and then I was suggested this tag. Prior to the answers below, I though this tag literally meant "Dialogue to Save file". That is, I didn't know it was name of a Control. – Shamim Hafiz - MSFT Sep 14 '11 at 12:07
-
1Well, credit to SO to give you the answer before you post it. – Hans Passant Sep 14 '11 at 12:21
6 Answers
10
private void Save_As_Click(object sender, EventArgs e)
{
SaveFileDialog _SD = new SaveFileDialog();
_SD.Filter = "Text File (*.txt)|*.txt|Show All Files (*.*)|*.*";
_SD.FileName = "Untitled";
_SD.Title = "Save As";
if (__SD.ShowDialog() == DialogResult.OK)
{
RTBox1.SaveFile(__SD.FileName, RichTextBoxStreamType.UnicodePlainText);
}
}

Overlock
- 128
- 7
-
Thanks for the detailed post which clarifies more on file types and Title. What about adding a default file name, how to do that? – Shamim Hafiz - MSFT Sep 14 '11 at 13:17
-
1
7
For WPF you should use this SaveFileDialog.
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Rich Text File (*.rtf)|*.rtf|All Files (*.*)|*.*";
dialog.FileName = "Filename.rtf"; //set initial filename
if (dialog.ShowDialog() == true)
{
using (var stream = dialog.OpenFile())
{
var range = new TextRange(myRichTextBox.Document.ContentStart,
myRichTextBox.Document.ContentEnd);
range.Save(stream, DataFormats.Rtf);
}
}

Ray
- 45,695
- 27
- 126
- 169
-
what about saving the content. The XAML tag will possibly open up a Dialog Box, but how is the content supposed to get save? – Shamim Hafiz - MSFT Sep 14 '11 at 12:05
-
@Sam, OpenFile requires less permissions. Also isn't FileOutputStream java?? – Ray Sep 14 '11 at 12:12
-
i use FileOutputStream in C# 2005.Used like this.. java.io.OutputStream ostream = new java.io.FileOutputStream(new java.io.File(FileName)); WorkBook.write(ostream); ostream.close(); – Sam Casil Sep 15 '11 at 00:32
2
This works for text files and was tested in WPF
.
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.Filter = "Text documents (.txt)|*.txt|All Files (*.*)|*.*";
dialog.FileName = "Filename.txt";
if (dialog.ShowDialog() == true)
{
File.WriteAllText(dialog.FileName, MyTextBox.Text);
}

lko
- 8,161
- 9
- 45
- 62
1
misread the question - Ray's answer is valid for OP
This works only in Windows Forms.
You should take a look at the SaveFileDialog class: http://msdn.microsoft.com/en-us/library/system.windows.forms.savefiledialog.aspx
And save the file using something like this (see here):
rtf.SaveFile(dialog.FileName);

Joel A. Villarreal Bertoldi
- 7,829
- 8
- 35
- 51
-
Thanks for the link. What about the content of the file which is to come from RichTextBox. How is that content going to be saved to the corresponding file? – Shamim Hafiz - MSFT Sep 14 '11 at 12:02
-
1Added some more info on saving the file from the RTF. – Joel A. Villarreal Bertoldi Sep 14 '11 at 12:06
-
Replace the `System.Windows.Forms` namespace with `Microsoft.Win32.SaveFileDialog` and I think you're good to go. – Joel A. Villarreal Bertoldi Sep 14 '11 at 12:12
-
-
1Damn, I missed that one. You're right. Your answer solves the OP's question perfectly, my bad. – Joel A. Villarreal Bertoldi Sep 14 '11 at 12:16
1
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();

Sam Casil
- 938
- 1
- 11
- 16