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:

Code Snippet

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 =new

System.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 =newMDIchild();

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

[4758 byte] By [programmer01] at [2008-1-10]
# 1

i don't see where you ever instantiate your string currentFile.

Why don't you just do this

richTextBox1.Modified = false;

this.Text = "Editor: " + openFileDialog.FileName.ToString();

Ken_L at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2

Becuase this is a MDI, the richtextbox is not in the parent form(parent form is where I am opening from) the textbox is in the CHILD form...totally different form.

programmer01 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 3

So you could pass the content from main form to the childform in constructor for example, then in your childform you can code for displaying the content in its richtextBox.

This is one of the solutions.

Thanks

FigoFei-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 4

Thanks if I am understanding you right I guess it should work, I will try.

Thanks

programmer01 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 5

I dont think what I did was right, by constructor did you mean changing this:

Code Block

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace EZWorkshop

{

public partial class MDIParent1 : Form

{

private int childFormNumber = 0;

public MDIParent1()

{

InitializeComponent();

}

to this:

Code Block

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

namespace EZWorkshop

{

public partial class MDIParent1 : MDIchild

{

private int childFormNumber = 0;

public MDIParent1()

{

InitializeComponent();

}

Becuase I still get the same errors as before and it still does not see the richtextbox

Or...would adding the open option to a menustrip in the CHILD form and taking it out of the parent and then merging the two menus work?

Thanks

programmer01 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 6

Hi

I suppose Figo Fei mean something like this:

Change your MDIParent OpenFile like this

Code Block

private void OpenFile ( object sender, EventArgs e )

{

OpenFileDialog openFileDialog = new OpenFileDialog();

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 && openFileDialog.FileName != String.Empty)

{

Form childForm = new MDIChild(openFileDialog.FileName);

childForm.MdiParent = this;

childForm.Show();

}

else

{

MessageBox.Show("Open File request cancelled by user.", "Cancelled");

}

}

and MDIChild something like this:

Code Block

public MDIChild ()

{

InitializeComponent();

}

public MDIChild ( string fileName )

: this()

{

OpenFile(fileName);

}

private void OpenFile ( string fileName )

{

string strExt;

strExt = System.IO.Path.GetExtension(fileName);

strExt = strExt.ToUpper();

if (strExt == ".RTF")

{

richTextBox1.LoadFile(fileName, RichTextBoxStreamType.RichText);

}

else

{

System.IO.StreamReader txtReader;

txtReader = new

System.IO.StreamReader(fileName);

richTextBox1.Text = txtReader.ReadToEnd();

txtReader.Close();

txtReader = null;

richTextBox1.SelectionStart = 0;

richTextBox1.SelectionLength = 0;

}

richTextBox1.Modified = false;

this.Text = "Editor: " + fileName;

}

I hope this help you.

Markku

MarkkuBehm at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 7

Regard these forms as independent ones.

So your question should be how to get data from another form, for the answer check this thread: http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=1736953&SiteID=1

Thanks

FigoFei-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 8

Thanks Figo and Markku for the help, Figo the link to that other topic was a little confusing to me, but thanks for the help you gave me a better idea of what to do. And thanks Markku that code worked and it opens files with no problem.

Tahnks again,

Programmer

programmer01 at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...