Bravo659
Hello I need some assistance in the MDI ChildForm to save contents of the multiline textbox in the ChildForm and open the text file. It writes to the text file but not will not close nor open with the open existing file or the open file. Below is the code i have generated.
I should be able to save the contents of the ChildForm and open existing file form the MDI parent form and the open a file to the ChildForm. Can you assist? Thanks.
public partial class TextEditorForm : Form
{
public int ChildOneCounterInteger;
StreamWriter editorStreamWriter;
public TextEditorForm()
{
InitializeComponent();
}
private void NewToolStripMenuItem_Click(object sender, EventArgs e)
{
ChildForm childOneForm = new ChildForm();
childOneForm.MdiParent = this;
childOneForm.Show();
ChildOneCounterInteger++;
childOneForm.Text = "Child One Document " + ChildOneCounterInteger.ToString();
childOneForm.Show();
}
private void openToolStripMenuItem1_Click(object sender, EventArgs e)
{
DialogResult responseDialogResult;
ofd.Filter = "Text Files | *.txt | All Files | *.* | RichText Files | *.rtf";
ofd.InitialDirectory = Directory.GetCurrentDirectory();
responseDialogResult = ofd.ShowDialog();
if (responseDialogResult != DialogResult.Cancel)
{
StreamReader sr;
sr = new StreamReader(ofd.FileName);
sr.Close();
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void tileToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void tileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
private void cascadeToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
private void openExistingToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult responseDialogResult;
string fileTextString = " ";
ofd.InitialDirectory = Directory.GetCurrentDirectory();
responseDialogResult = ofd.ShowDialog();
if (responseDialogResult != DialogResult.Cancel)
{
StreamReader sw = new StreamReader(ofd.FileName);
while (sw.Peek() != 1)
{
fileTextString = (sw.ReadLine());
}
sw.Close();
}
ChildForm childOneForm = new ChildForm();
childOneForm.MdiParent = this;
ChildOneCounterInteger++;
childOneForm.Text = "Child One Document " + ChildOneCounterInteger.ToString();
childOneForm.txtEditor.Text = fileTextString;
childOneForm.Show();
}
private void TextEditorForm_Load(object sender, EventArgs e)
{
ChildForm childOneForm = new ChildForm();
childOneForm.MdiParent = this;
childOneForm.Show();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
sfd.Title = "Save a Text File";
sfd.Filter = "Text File (*.txt)|*.txt|All Files (*.*)|*.*";
DialogResult dr = sfd.ShowDialog();
if (dr == DialogResult.OK)
{
System.IO.StreamWriter sw = new System.IO.StreamWriter(sfd.FileName);
Form activeChildForm = this.ActiveMdiChild;
if (activeChildForm != null)
{
TextBox txtEditor = activeChildForm.ActiveControl as TextBox;
if (txtEditor != null)
{
sw.Write(txtEditor.Text);
}
sw.Close();
}
}
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutBox1 aAboutForm = new AboutBox1();
aAboutForm.ShowDialog();
}
}
I appreciate for your assistance and let me know what I am doing wrong so i can fix the problem.
Please disregard this message is not part of the posting above. Seems its a coding or server error in part of Daniweb. Error message is too short.
With this extra text it allow me to post to this thread.