I have a rich text box, and am having difficulty saving my file using the save file dialog (I want to let the user be able to browse to a specific location on their computer.) Could anyone please help me.
Printable View
I have a rich text box, and am having difficulty saving my file using the save file dialog (I want to let the user be able to browse to a specific location on their computer.) Could anyone please help me.
What exactly are you having trouble with? Can't get the file save dlg to display? Can't write out the data to the file?
Problem Solved now. But I am having one other little issue. When I have a rich text box that is blank, I want a messagbox to display saying that it is not possible to save a blank textbox. I have tried if/else statements but this did not seem to work. Here is the code below.
private void button4_Click(object sender, RoutedEventArgs e)
{
TextRange textRange = new TextRange(richTextBox2.Document.ContentStart, richTextBox2.Document.ContentEnd);
SaveFileDialog dlg = new SaveFileDialog();
dlg.Filter = "pdb Files (*pdb) | * pdb";
if (dlg.ShowDialog() == true)
{
using (FileStream fs = new FileStream(dlg.FileName, FileMode.OpenOrCreate))
{
textRange.Save(fs, DataFormats.Text);
I would approach that problem differently.
Instead of being reactive and telling the user that they can't save an empty document after they've tried to save, I would be proactive and only enable the save button if there is something to save.
To do this, handle the TextChanged event and check the size of the content. If it's zero, then disable the save button; otherwise enable it.