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.
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);
}
}
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);
}
}
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);
}
SaveFileDialog sfDialog = new SaveFileDialog();
sfDialog.ShowDialog();
OutputStream ostream = new FileOutputStream(new File(sfDialog.FileName));
WorkBook.write(ostream);
ostream.close();
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);
There is a SaveFileDialog component which you can use, read here to find out how it works and a working sample.
Related
I am writing the code editor in C# and i want to make the saving with keyboard shorcut CTRL + S this is my saving file code
private void saveToolStripMenuItem1_Click(object sender, EventArgs e)
{
var di = new SaveFileDialog();
di.Filter = "Text Files|*.txt";
di.FileName = "Txt" + ".txt";
var result = di.ShowDialog();
if (result == DialogResult.OK)
{
File.WriteAllText(di.FileName, textBox1.Text);
}
}
I want the tip how to solve it or piece of code that will work.
Select the Save menu strip item:
Open the properties window and set the keyboard shortcut:
Note that your code is only saving the rich text (rich = formatted) as plain text, so your file will not have that formatting. If you want to save as rich text, use
richTextBox1.SaveFile("whatever.rtf");
So I made code again about saving files (you can select file name)
here:
private void button3_Click(object sender, EventArgs e)
{
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
using (Stream s = File.Open(saveFileDialog1.FileName, FileMode.CreateNew))
using (StreamWriter sw = new StreamWriter(s))
{
sw.Write(fastColoredTextBox1.Text);
}
}
}
the problem is I want to have 2 selectable extensions:
-.txt
-.md
because the code I wrote can save to any type of file(and if you didn't put anything I will save as . FILE) and I just want only 1 save file type.
Save dialog
You need to set the dialog's Filter property according to the pattern:
Text to show|Filter to apply|Text to show 2|Filter to apply 2|...
For example in your case, where you seem to be saying you want the save dialog to have a combo dropdown containing two things, one being Text and the other being Markdown, your Filter has 4 things:
.Filter = "Text files|*.txt|Markdown files|*.md";
It is typical to put the filter you will apply into the text you will show too, so the user can see what you mean by e.g. "Text files" but I've left that out for clarity.
For example you might choose to make it say Text files(*.txt)|*.txt - the text in the parentheses has no bearing on the way the filter works, it's literally just shown to the user, but it tells them "files with a .txt extension"
Footnote, you may have to add some code that detects the extension from the FileName if you're saving different content per what the user chose e.g.:
var t = Path.GetExtension(saveFileDialog.FileName);
if(t.Equals(".md", StringComparison.OrdinalIgnoreCase))
//save markdown
else
//save txt
You can just use the filter of your dialog to set the allowed extensions:
// Filter by allowed extensions
saveFileDialog1.Filter = "Allowed extensions|*.txt;*.md";
I have a WPF application and I use external library for generating documents. This library returns document as System.Drawing.Printing.PrintDocument. How can I print this document in WPF? I can use Print() method directly, but I need to allow user to select printer and settings. If I use WPF PrintDocument dialog, I can't set my document to it as in WinForms dialog.Document. Is there a way to convert old PrintDocument to some WPF friendly form?
WinForms way:
// get document for printing
PrintDocument document = exporter.GetPrintDocument();
System.Windows.Forms.PrintDialog dialog = new System.Windows.Forms.PrintDialog();
dialog.Document = document;
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
document.Print();
}
WPF way:
System.Windows.Controls.PrintDialog dialog = new System.Windows.Controls.PrintDialog();
if (dialog.ShowDialog() == true)
{
// how to print old PrintDocument???
dialog.PrintDocument(...);
}
I also tried to open WinForms dialog in WPF but it is not possible. Dialog is just not shown.
Thanks for any help.
I found an answer. You have to set UseDialogEx dialog property to true.
MessageBox.Show(printDialog1.PrinterSettings.PrinterName);
printDialog1.PrinterSettings.PrintFileName = "A.txt";
MessageBox.Show(printDialog1.PrinterSettings.PrintFileName);
printDialog1.ShowDialog();
printDocument1.DocumentName = "A.txt";
if (printDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
printDocument1.Print();
}
How can I do this?
I want a user to click a button and then a small window pops up and lets me end-user navigate to X folder. Then I need to save the location of the folder to a string variable.
Any help, wise sages of StackOverflow?
using (FolderBrowserDialog dlg = new FolderBrowserDialog())
{
if (dlg.ShowDialog(this) == DialogResult.OK)
{
string s = dlg.SelectedPath;
}
}
(remove this if you aren't already in a winform)
If you're using Winforms, you can use a FolderBrowserDialog control. The path the user selects will be in the SelectedPath property.
string path = null;
FolderBrowserDialog dlg = new ();
if (dlg.ShowDialog() == DialogResult.OK)
{
path = dlg.SelectedPath;
}
I want to eliminate the need for SaveFileDialog and just add the ability for a user to save the input onClick without having to go through the SaveFileDialog. The filename will be set by txtModuleName.Textand the location/directory and file type will always remain the same.
string Saved_Module = "";
SaveFileDialog saveFD = new SaveFileDialog();
saveFD.InitialDirectory = "C:/Modules/";
saveFD.Title = "Save your file as...";
saveFD.FileName = txtModuleName.Text;
saveFD.Filter = "Text (*.txt)|*.txt|All Files(*.*)|*.*";
if (saveFD.ShowDialog() != DialogResult.Cancel)
{
Saved_Module = saveFD.FileName;
RichTextBox allrtb = new RichTextBox();
// This one add new lines using the "\n" every time you add a rich text box
allrtb.AppendText(txtModuleName.Text + "\n" + ModuleDueDate.Text + "\n" + txtModuleInfo.Text + "\n" + txtModuleLO.Text);
allrtb.SaveFile(Saved_Module, RichTextBoxStreamType.PlainText);
RE-ATTACK - How do I replace the use of the Dialog box when saving with a simple button that says 'Save' which OnClick uses the txtModuleName.Text as its name and saves it to a set directory C:/Modules/ in the form of a .txt file.
Thanks in advance!
You need to provide the full path and filename to RichTextBox.SaveFile instead of using SaveFileDialog.FileName. Add using System.IO;, and then use something similar to this:
private void button1_Click(object sender, EventArgs e)
{
String Saved_Module = Path.Combine("C:\\Module", txtModuleName.Text);
allrtb.SaveFile(Saved_Module, RichTextBoxStreamType.PlainText);
}