I am new and trying to do something in c# but I dont know how to solve this prbolem.
My MainWindow.xaml.cs I have a RichTextBox with name RTB that I wanna save into text file.
I created new file Saving.cs where I would write every thing that I need for saving. This is my code.
Saving.cs
public string VarName = "";
public void Save()
{
MainWindow main = new MainWindow();
TextRange range = new TextRange(main.RTB.Document.ContentStart, main.RTB.Document.ContentEnd);
FileStream stream = new FileStream(VarName, FileMode.Create);
range.Save(stream, DataFormats.Text);
stream.Close();
}
MainWindow.xaml.cs
private void Button_Save(object sender, RoutedEventArgs e)
{
saveDoc.VarName = SaveName.Text + ".txt";
saveDoc.Save();
}
Everything works fine except that there is nothing in saved document. My problem is that I really dont know how to acces RTB from different file. It works when I put this code into MainWindow.xaml.cs but not when its in different file like Saving.cs.
I also dont know if its ok to have this in my Saving.cs "MainWindow main = new MainWindow();" or how to aproach this problem.
Thanks for your help kind stranger.