-1

If I set path = "C:\\MSREAD.txt"; and Click on SaveAs Menu Item ,it saves Filetext,But If I dont give the String path and save it from saveFD.FileName it doesnt work.Please help me with this issue. Thanks a lot

public void SaveToFile()
{
    String SavedFile = "";
    saveFD.InitialDirectory = @"C:";
    saveFD.Title = "Save a Text File";
    saveFD.FileName = "";
    RichTextBox richTextBox1 = new RichTextBox();

    saveFD.Filter = "Text Files|*.txt|All Files|*.*";
    try
    {
        if (saveFD.ShowDialog() != DialogResult.Cancel)
        {
            SavedFile = saveFD.FileName;
            path = SavedFile.ToString();
            //path = "C:\\MSREAD.txt";                   
            MessageBox.Show(path);
            richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
            SaveMyTextBoxContents(path);
        }
    }
    catch(Exception e)
    {
     MessageBox.Show(e.ToString());
    }
}

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveToFile();
}

public void SaveMyTextBoxContents(string path)
{
    if (listBoxItems.SelectedIndex == -1)
    {
        if (rdBtnSlow.Checked && rdBtnNo.Checked)
        {
            using (StreamWriter outputFile = new StreamWriter(path))
            {
                foreach (string item in listBoxItems.Items)
                {
                    saveAllText = slowNo + " " + item;
                    outputFile.WriteLine(saveAllText);
                }
            }
        }
    }
}
Jalal Said
  • 15,906
  • 7
  • 45
  • 68
krikk
  • 25
  • 4
  • 1
    Please define "doesn't work." I can't help but notice that you've written this code to ignore any and all exceptions. This is a bad idea. If the code isn't working for a specific reason, the exception message will indicate this. Exceptions are good things, they contain useful information. – David Jul 30 '11 at 13:10
  • Thanks for the Reply,, I updated my question..But I am sorry,If it is not quite understandable – krikk Jul 30 '11 at 13:26
  • You're still going to have to do some debugging. When you step through the code, at what point does it behave differently than expected? If it's generating an error, what line generates the error? What is the error? What are the states of the objects when the error is generated? – David Jul 30 '11 at 13:29

1 Answers1

0

Here is your problem:

richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);
SaveMyTextBoxContents(path);

You first save the richTextBox text to file, but then override the same file with SaveMyTextBoxContents, However the file is empty because of SaveMyTextBoxContents method will only save something if some conditions are true "not selected item and both check boxes are checked", and the listBoxItems.Items.Count > 0 which apparently not the case

Jalal Said
  • 15,906
  • 7
  • 45
  • 68
  • Hii Jalal, Thanks a lot and sorry for the Late Reply.I tried to save the richTextBox text to file,..But I still donot get the SaveMyTextBoxContents(path) function works out for MenuClick..But could you tell me any other way to pass the saveFD.FileName exact (path) to a string.. Thanksss – krikk Jul 30 '11 at 14:18
  • the `richTextBox1.SaveFile(path, RichTextBoxStreamType.PlainText);` will save the contents of the `richTextBox` into a file. so if you want also to save the contents of the `listBox` into another file, then use a separate `SaveFileDialog` and another menu item to save it in another file. – Jalal Said Jul 30 '11 at 14:38
  • Thanks a lot Jaleel after,, I used Seperate SaveFileDialog for ListBox items ,it is working. – krikk Jul 30 '11 at 14:53