保存文件对话框菜单单击项不起作用。

发布于 2024-11-27 03:16:50 字数 1468 浏览 5 评论 0原文

如果我设置 path = "C:\\MSREAD.txt"; 并单击“另存为”菜单项,它会保存文件文本,但如果我不提供字符串路径并从 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);
                }
            }
        }
    }
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

眼波传意 2024-12-04 03:16:50

这是您的问题:

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

您首先将 richTextBox 文本保存到文件中,然后使用 SaveMyTextBoxContents 覆盖相同文件,但是该文件是空的,因为SaveMyTextBoxContents 方法仅在某些条件为真“未选定项目且两个复选框均已选中”时才会保存某些内容,并且 listBoxItems.Items.Count > > 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文