SaveFileDialog菜单单击项不起作用。

时间:2011-07-30 13:02:38

标签: c# savefiledialog

如果我设置path = "C:\\MSREAD.txt";并单击SaveAs菜单项,它会保存Filetext,但是如果我不提供String路径并从saveFD.FileName保存它它不起作用。请帮我这个问题。 非常感谢

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);
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

这是你的问题:

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

您首先将richTextBox文本保存到文件,但随后使用SaveMyTextBoxContents覆盖相同的文件,但由于SaveMyTextBoxContents方法,该文件为空只有在某些条件为真的情况下才会保存“未选中的项目且选中了两个复选框”,listBoxItems.Items.Count> 0显然不是这样的