Open File dialog MDI
hello,
In my project I have a MDI form, the child form for this has a rich text box in it, I want it so that when the user opens a file using the Openfiledialog that the file they open will open up a new child form with the document they chose to open. Here is my code:
privatevoid OpenFile(object sender,EventArgs e)
{
MDIchild childForm =newMDIchild();
OpenFileDialog openFileDialog =newOpenFileDialog();
openFileDialog.DefaultExt ="html";
openFileDialog.InitialDirectory =Environment.GetFolderPath(Environment.SpecialFolder.Personal);
openFileDialog.Filter ="Text Files (*.txt)|*.txt|Html Files (*.htm)|*.htm|Rich Text Files (*.rtf)|*.rtf|All Files (*.*)|*.*";
openFileDialog.FilterIndex = 1;
openFileDialog.FileName =string.Empty;
if (openFileDialog.ShowDialog(this) ==DialogResult.OK)
{
if (openFileDialog.FileName =="")
{
return;
}
string strExt;
strExt = System.IO.Path.GetExtension(openFileDialog.FileName);
strExt = strExt.ToUpper();
if (strExt ==".RTF")
{
richTextBox1.LoadFile(openFileDialog.FileName,
RichTextBoxStreamType.RichText);
}
else
{
System.IO.
StreamReader txtReader;txtReader =
newSystem.IO.
StreamReader(openFileDialog.FileName);richTextBox1.Text = txtReader.ReadToEnd();
txtReader.Close();
txtReader =
null;richTextBox1.SelectionStart = 0;
richTextBox1.SelectionLength = 0;
}
currentFile = openFileDialog.FileName;
richTextBox1.Modified =
false;this.Text ="Editor: " + currentFile.ToString();}
else{
MessageBox.Show("Open File request cancelled by user.","Cancelled");}
//string FileName = openFileDialog.FileName;}
Where it says at the beginning:
MDIchild childForm =new
MDIchild();MDIchild is the form I made to be the child form, it contains one rich text box that fills the whole form called Richtextbox1
Throught ALL of the code I showed above these 2 words are underlined as an error:
"richTextBox1"
"currentFile"
I know why the richtextbox1 is an error..becuase it only exists in the MDIchild form, but the error for currentFile is:
does not exist in current context.
Just so you know.
I got this code from:
http://www.codeproject.com/csharp/eRichTextBox.asp
Thanks

