Hi...
I am trying to save a file in the MDI - which uses the RichTextBox... But I am not able to save the formatting to the text..
Example : - If I chnage the font, color, size of the text.. it doesnt save the formatting but stores the simple text..
NOTE: this is happening when I select the RTF option in the Save File Dialog box
public
void save_file(){
savedlg.Filter =
"Text Documents (*.txt)|*.txt|Rich Text Documents (*.rtf)|*.rtf"; DialogResult res = savedlg.ShowDialog(); RichTextBox t1 = (RichTextBox)tabControl1.SelectedTab.Controls[0]; if (res == DialogResult.OK){
FileStream f1 = new FileStream(savedlg.FileName, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(f1); char[] ary = t1.Text.ToCharArray(); int i = 0; while (i <= ary.Length - 1){
sw.Write(ary[i++]);
}
sw.Close();
f1.Close();
fileName = savedlg.FileName;
tabControl1.SelectedTab.Text = System.IO.
Path.GetFileName(fileName);fileName =
"";}
else if (res == DialogResult.Cancel) return; else{
t1.SaveFile(fileName,
RichTextBoxStreamType.RichText);tabControl1.SelectedTab.Text = System.IO.
Path.GetFileName(fileName);}
}
Please Help me...!!!

Dr SpackPosted Dec 9, 2007, 2:03 PM
...and as I have stated above, your error seams to be in your if ... else if ...else ... construction.
Maybe you should debug your save-method!?
But to save you some time and trouble I explain it to you.
if (res == DialogResult.OK)
// write Textfile
else if (res == DialogResult.Cancel)
// do nothing
else // Here is the error. The following lines will never be executed!
// write rtf??????
The Result is either OK or Cancel! The last else will never be executed.
Don't you agree?
To determine whether to save rtf or plain text you should check the extension of the returned filename.
And I have something else. This will shorten your source code a bit.
To save Rtf: t1.SaveFile( filename, RichTextBoxStreamType.RichText );
To load Rtf: t1.SaveFile( filename, RichTextBoxStreamType.RichText );
To save PlainText: t1.LoadFile( filename, RichTextBoxStreamType.PlainText );
To load PlainText: t1.LoadFile( filename, RichTextBoxStreamType.PlainText );
And if this is homework and you have to save the text/rtf manually, then try the properties
Rtf and Text of the RichTextBox.
I hope this will help you?!
Chitkaran SinghPosted Dec 8, 2007, 6:37 AM
Here is the method that I am using to open the File...
open.Filter = "Text Documents (*.txt)|*.txt|Rich Text Documents (*.rtf)|*.rtf";
DialogResult result = open.ShowDialog();
if (File.Exists(open.FileName))
{
fileName = open.FileName;
tabControl1.SelectedTab.Name = System.IO.Path.GetFileName(fileName);
tabControl1.SelectedTab.Refresh();
}
if (result == DialogResult.OK)
{
RichTextBox rtb = (RichTextBox)tabControl1.SelectedTab.Controls[0];
tabControl1.SelectedTab.Text = System.IO.Path.GetFileName(fileName);
this.tabControl1.SelectedTab.Name = fileName;
//open the file in the Rich Text Box
string str; StreamReader sRead = new StreamReader(open.FileName);sRead.BaseStream.Seek(0,
SeekOrigin.Begin);str = sRead.ReadToEnd();
// active tab of child
RichTextBox temprtb = (RichTextBox)tabControl1.SelectedTab.Controls[0];temprtb.Text = str;
rtb.Multiline =
true;rtb.Dock =
DockStyle.Fill;rtb.ScrollBars =
RichTextBoxScrollBars.Both;rtb.WordWrap =
true;Also, I think the problem is with the Save method because when I saved the file, I opened it in the MSWORD and it was simple file without any formatting....
PLEASE Help Me guys... My Project is Hanging in balance..
Dr SpackPosted Dec 6, 2007, 8:58 AM
I think your problem is at the last else statement. When do you want to hit it? The DialogResult is Ok or Cancel. And the third option?
Ryan TurneyPosted Dec 6, 2007, 8:05 AM
Alright, your code above looks like it should work fine, I am curious though, how are you loading the file? You may be losing the formatting when you load it and not when you save it.