Hi! I'm having trouble creating a save funktion. How can I save the
contens of a new richTextBox that is created from an "external" class
file? The open funktion looks like this:
public void openFile()
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Select a Text File";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
StreamReader sr = new
StreamReader(openFileDialog1.FileName);
string title =
Path.GetFileName(openFileDialog1.FileName);
TabPage myTabPage = new TabPage(title);
tabControl1.TabPages.Add(myTabPage);
int index = tabControl1.TabPages.IndexOf(myTabPage);
//Add the RichTextBox to the tabPage.
NumberedTextBox.NumberedTextBoxUC textBox = new
NumberedTextBox.NumberedTextBoxUC();
this.textBox.richTextBox1 = new
System.Windows.Forms.RichTextBox();
// Load the contents of the file into the RichTextBox.
textBox.richTextBox1.LoadFile(openFileDialog1.File Name,
RichTextBoxStreamType.PlainText);
this.tabControl1.TabPages[index].Controls.Add(textBox);
// Active tabPage is the new tabPage.
this.tabControl1.SelectedTab = myTabPage;
sr.Close();
So the file i want to save is the loaded contens of
textBox.richTextBox1. The save funktion i made(doesn't work) looks like
this:
private void saveFile()
{
// Create a SaveFileDialog to request a path and file name
to save to.
SaveFileDialog saveFile1 = new SaveFileDialog();
// Initialize the SaveFileDialog to specify the extension
for the file.
saveFile1.FileName = tabControl1.SelectedTab.Text;
saveFile1.DefaultExt = "*.pon";
saveFile1.Filter = "Point (*.pon)|*.pon|Luna
(*.lun)|*.lun";
// Determine if the user selected a file name from the
saveFileDialog.
if (saveFile1.ShowDialog() ==
System.Windows.Forms.DialogResult.OK &&
saveFile1.FileName.Length > 0)
{
// Save the contents of the RichTextBox into the file.
this.boxen.richTextBox1.SaveFile(saveFile1.FileNam e,
RichTextBoxStreamType.PlainText);