saveFileDialog

Hi, I need your help with that saveFile code that seams to compile but hit with an exception:

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)

{

SaveFileDialog saveFileDialog1 = new SaveFileDialog();

saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*|RTF Files (*.rtf)|*.rtf";

saveFileDialog1.FilterIndex = 3;

saveFileDialog1.RestoreDirectory = true;

if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)

{

childForm childForm = new childForm();

String filename = saveFileDialog1.FileName;

using (StreamWriter writer = new StreamWriter(filename))

{

childForm.MdiParent = this;

childForm.richTextBox1.SaveFile(saveFileDialog1.FileName);

}

}

}



Answer this question

saveFileDialog

  • AnnabelP

    Could you please answer a few questions that are in referenced in the comments (//) below.

    1. Are you creating the childForm on the fly
    2. If question 1 is yes then are you populating the RichTextbox someplace else because I don't see you having anything in it in the code you provided.
    3. It seems that you are not "showing" the childForm so therefore I am not sure if the form is actually intialized correctly.

    Look at the code I provided below (SDK Code) from the RichTextBox.SaveFile Method (String) entry in help. In this it is assumed that the RichTextBox is on the current form which it seems is not the case in your example. Also note that the sample does not require you to have a StreamWriter.

    The code fragement you provided leaves a lot to guess so I am not sure if I am answering your question completely so feel free to follow up.

    private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)

    {

    SaveFileDialog saveFileDialog1 = new SaveFileDialog();

    saveFileDialog1.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*|RTF Files (*.rtf)|*.rtf";

    saveFileDialog1.FilterIndex = 3;

    saveFileDialog1.RestoreDirectory = true;

    if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)

    {

    //reference question 1

    childForm childForm = new childForm();

    String filename = saveFileDialog1.FileName;

    using (StreamWriter writer = new StreamWriter(filename))

    {

    childForm.MdiParent = this;

    //reference question 2 and 3

    childForm.richTextBox1.SaveFile(saveFileDialog1.FileName);

    }

    }

    }

    SDK Code

    public void SaveMyFile()
    {
    // Create a SaveFileDialog to request a path and file name to save to.
    SaveFileDialog saveFile1 = new SaveFileDialog();

    // Initialize the SaveFileDialog to specify the RTF extention for the file.
    saveFile1.DefaultExt = "*.rtf";
    saveFile1.Filter = "RTF Files|*.rtf";

    // Determine whether 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.
    richTextBox1.SaveFile(saveFile1.FileName);
    }
    }




  • Robert Reister

    You might want to try and have your ChildMDI form actually call the RichTextbox save command and have the MDIForm send it the filename to use by maybe setting a property. Another things you can do is put all of the code in the ChildMDI just for a test to see if things work. This would uncomplicate things. You could do something like the sample in the help that I provided earlier. I am thinking you are going to have to make things simple and prove out your steps before you get more complicated. Hope this helps.

  • Tim Wong

    Hi I am sorry for the mass. let me explain. I have MDI Form and ChildForm with richTextBox. That code is located on the mdi form. and the exception is that the file being saved is used by another process, but it is not. it didn't save anything anyway.

    thnks jbattat


  • 52179apb

    Hi, Just question I am using the express C#. Since it's free it have less features then the regular VS 2005 Because the menu strip on the childForm did not merge with the menu strip on the mdi form, rather they one below the other.

    jbattat


  • rivast_2001

    I mean I try use a menuStrip on the childForm to code the save file and I try to merge both menus (on the MDI and on the ChildForm) but the merge did go good enough.The menu strips shown as 2 menus one below the other. I am using the C# Express that provided free from Microsoft. However the code of save file seams to work better from the childForm.
  • SHakeelGhauri77

    What sort of exceptions Where are they occurring in this code

  • jcnewbie

    I am not sure I understand the question. If this is a new question then I would suggest you open a new thread. If not then can you explain a little more.

  • saveFileDialog