Question

I'm making a text editor and I'd like to display the name of the current open file in the Form title (like Notepad does where it says "Untitled - Notepad" or " "File - Notepad").

I'm assuming this is done working with the SaveFileDialog and OpenFileDialog, so I'll post my current code.

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

How can I get the file name and put it in the Form's title (like Notepad does where it has the name of the file followed by the name of the text editor).

Était-ce utile?

La solution 2

You can wrap it in a function like this:

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

..and pass in the dialog FileName..

EDIT:

Your issue is that you're not calling the function I gave you. You have the function I gave you above.. but you don't call it.

Replace this:

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

With this:

SetWindowTitle(sfdSaveFile.FileName);

And replace this:

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

With this:

SetWindowTitle(ofd.FileName);

Autres conseils

While opening . . .

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 { } 
    }

While saving . . .

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

        }
}

Just an update

Toby- 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

You do not need the catch block to check if User selected OK / Cancel.

OpenFileDialog & SaveFileDialog has method ShowDialog that returns DialogResult

and value of DialogResult.OK tells that user has selected file to open / save and has not cancelled the operation.

And example with OpenFile

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

    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);
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top