1

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

我假设这是使用 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));

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

4

2 回答 2

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 块来检查用户是否选择了确定/取消。

OpenFileDialog 和 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);
    }
}
于 2013-04-03T01:04:59.187 回答
1

您可以将其包装在这样的函数中:

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);
于 2013-04-03T00:55:09.457 回答