【问题标题】:How do I show the name of the current open file in the Form title?如何在表单标题中显示当前打开文件的名称?
【发布时间】:2013-04-03 00:39:24
【问题描述】:

我正在制作一个文本编辑器,我想在表单标题中显示当前打开文件的名称(就像记事本一样,它显示“无标题 - 记事本”或““文件 - 记事本”)。

我假设这是使用 SaveFileDialog 和 OpenFileDialog 完成的,所以我将发布我当前的代码。

打开文件:

  private void OpenFile()
        {
            NewFile();
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Title = "Open File";
            ofd.FileName = "";
            ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
            if (ofd.ShowDialog() != DialogResult.OK) return;
            StreamReader sr = null;
            try
            {
                sr = new StreamReader(ofd.FileName);
                this.Text = string.Format("{0} - Basic Word Processor", ofd.FileName);
                richTextBoxPrintCtrl1.Text = ofd.FileName;
                richTextBoxPrintCtrl1.Text = sr.ReadToEnd();
                filepath = ofd.FileName;
                richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);

            }
            catch 
            { 
            }
            finally
            {
                if (sr != null) sr.Close();
            }

保存文件

private void SaveFileAs()
        {
            SaveFileDialog sfdSaveFile = new SaveFileDialog();
            sfdSaveFile.Title = "Save File";
            sfdSaveFile.FileName = "Untitled";
            sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
            if (sfdSaveFile.ShowDialog() == DialogResult.OK)
                try
                {
                    filepath = sfdSaveFile.FileName;
                    SaveFile();
                    this.Text = string.Format("{0} - Basic Word Processor", sfdSaveFile.FileName);
                }
                catch (Exception exc)
                {

                }


void SetWindowTitle(string fileName) {
    this.Text = string.Format("{0} - Basic Text Editor", System.IO.Path.GetFileNameWithoutExtension(OpenFileDialog.Filename));

如何获取文件名并将其放在表单的标题中(就像记事本一样,文件名后跟文本编辑器的名称)。

【问题讨论】:

  • 你试过什么? ..考虑到你所拥有的,设置表单标题实际上是困难的倒退..
  • 我尝试过的唯一方法是在标签中显示文件名,但效果不佳(它只显示 .rtf 文件标题)...
  • 顺便说一句,空的 catch 块是个坏主意 - stackoverflow.com/questions/1234343/…

标签: c# winforms visual-studio-2012 openfiledialog savefiledialog


【解决方案1】:

打开时。 . .

private void OpenFile()
{
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Title = "Open File";
        ofd.FileName = "";
        ofd.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
        ofd.ShowDialog();
        try
        {
            // just one line is added
            this.Text = string.Format("{0} - MyNotepad", Path.GetFileName(ofd.Filename));
            richTextBoxPrintCtrl1.Text = ofd.FileName;
            StreamReader stread = new StreamReader(richTextBoxPrintCtrl1.Text);
            richTextBoxPrintCtrl1.Text = stread.ReadToEnd();
            stread.Close();
            richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);
        }
        catch { } 
    }

在保存时。 . .

private void SaveFileAs()
{
    SaveFileDialog sfdSaveFile = new SaveFileDialog();
    sfdSaveFile.Title = "Save File";
    sfdSaveFile.FileName = "Untitled";
    sfdSaveFile.Filter = "Rich Text Files (*.rtf)|*.rtf|Text Document (*.txt)|*.txt|Microsoft Word Document (*.doc)|*.doc|Hypertext Markup Language Document (*.html)|*.html";
    if (sfdSaveFile.ShowDialog() == DialogResult.OK)
        try
        {
            richTextBoxPrintCtrl1.SaveFile(sfdSaveFile.FileName, RichTextBoxStreamType.RichText);
            filepath = sfdSaveFile.FileName;
            // just one line is added
            this.Text = string.Format("{0} - MyNotepad", Path.GetFileName(sfd.Filename));
        }
        catch (Exception exc)
        {

        }
}

只是更新

托比-The empty catch blocks are needed. If the user cancels the ofd or sfd without the catch block, the program crashes. It keeps the program from crashing

您不需要 catch 块来检查用户是否选择了确定/取消。

OpenFileDialog 和 SaveFileDialog 具有返回 DialogResult 的方法 ShowDialog

DialogResult.OK 的值告诉用户已经选择了要打开/保存的文件并且没有取消操作。

OpenFile 示例

私有 void OpenFile() { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "打开文件"; ofd.FileName = ""; ofd.Filter = "富文本文件 (.rtf)|.rtf|文本文档 (.txt)|.txt|Microsoft Word 文档 (.doc) |.doc|超文本标记语言文档 (.html)|.html";

    if (ofd.ShowDialog() == DialogResult.OK)
    {     
        // just one line is added
        this.Text = string.Format("{0} - MyNotepad", Path.GetFileName(ofd.Filename));
        richTextBoxPrintCtrl1.Text = ofd.FileName;
        StreamReader stread = new StreamReader(richTextBoxPrintCtrl1.Text);
        richTextBoxPrintCtrl1.Text = stread.ReadToEnd();
        stread.Close();
        richTextBoxPrintCtrl1.LoadFile(fileName, RichTextBoxStreamType.RichText);
    }
}

【讨论】:

  • 干杯伙伴。这与西蒙的贡献很好。我现在只需要显示文件名就可以了。
  • @Toby - 修复先生,不知道我到底是怎么错过了 Path.GetFileName!我想和 GF 分手真的会影响心灵!
  • 几个月前我有同样的事情,伙计。我很清楚那是什么感觉。希望你的情况很快会好起来。 :-)
  • 需要空的 catch 块。如果用户在没有 catch 块的情况下取消了 ofd 或 sfd,程序就会崩溃。它可以防止程序崩溃。
  • @Toby - 已更新答案以避免尝试捕获 OK/Cancel 检测。
【解决方案2】:

你可以把它包装成这样的函数:

void SetWindowTitle(string fileName) {
    this.Text = string.Format("{0} - MyEditor", System.IO.Path.GetFileName(fileName));
}

..并传入对话框文件名..

编辑:

你的问题是你没有调用我给你的函数。你有我上面给你的功能..但你没有调用它。

替换这个:

this.Text = string.Format("{0} - Basic Word Processor", sfdSaveFile.FileName);

有了这个:

SetWindowTitle(sfdSaveFile.FileName);

然后替换这个:

this.Text = string.Format("{0} - Basic Word Processor", ofd.FileName);

有了这个:

SetWindowTitle(ofd.FileName);

【讨论】:

  • 想通了。问题:它显示完整的文件路径。我怎样才能让它只显示文件名和文件扩展名?
  • 出于某种原因,它仍然在做同样的事情。我尝试将“WithoutExtension”添加到“GetFileName”的末尾,但没有成功..
  • 你传递给函数的是什么? Dialog.FileName?
  • 我正在使用另一个答案的“this.Text = string.Format("{0} - MyNotepad", ofd.Filename);”。
  • 这是如何实现的?抱歉,这是一个愚蠢的问题。我是编程新手,这都是经验。
猜你喜欢
  • 1970-01-01
  • 2011-05-05
  • 2013-09-16
  • 1970-01-01
  • 2012-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多