如何在表单标题中显示当前打开文件的名称?

时间:2013-04-03 00:39:25

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

我正在创建一个文本编辑器,我想在表单标题中显示当前打开文件的名称(如记事本所说的“无标题 - 记事本”或“”文件 - 记事本“)。< / p>

我假设这已经完成了SaveFileDialog和OpenFileDialog,所以我将发布我当前的代码。

的OpenFile:

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

SAVEFILE

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

如何获取文件名并将其放在表单的标题中(如记事本所示,其中包含文件名称,后跟文本编辑器的名称)。

2 个答案:

答案 0 :(得分:2)

开场时。 。

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块来检查User是否选择了OK / Cancel。

OpenFileDialog&amp; SaveFileDialog的方法ShowDialog返回DialogResult

和DialogResult.OK的值告诉用户已选择要打开/保存的文件,但尚未取消操作。

以OpenFile为例

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

答案 1 :(得分:0)

你可以将它包装在这样的函数中:

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

..并传入对话框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);
相关问题