1

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

  • What have you tried? .. given what you have, setting the form title is in fact a step backward in difficulty.. – Simon Whitehead Apr 03 '13 at 00:42
  • The only thing I've tried is displaying the file name in a label, but that didn't work so well (it only displayed .rtf file titles)... –  Apr 03 '13 at 00:53
  • 1
    as an aside empty catch blocks are a bad idea - http://stackoverflow.com/questions/1234343/why-are-empty-catch-blocks-a-bad-idea – Jason Apr 03 '13 at 01:04

2 Answers2

2

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);
    }
}
Parimal Raj
  • 20,189
  • 9
  • 73
  • 110
  • Cheers mate. This is working well with Simon's contribution. I just need to display the name of the file only now and it'll be done. –  Apr 03 '13 at 01:25
  • 2
    @Toby - fixed sir, dunno how in the world i missed Path.GetFileName! I guess breakup with GF really affects mind! – Parimal Raj Apr 03 '13 at 01:32
  • I had the same thing a few months ago, man. I know exactly how it feels. Hope things get better for you soon. :-) –  Apr 03 '13 at 01:44
  • 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. –  Apr 03 '13 at 12:08
  • @Toby - answer updated to avoid try catch for OK/Cancel detection. – Parimal Raj Apr 03 '13 at 14:14
1

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);
Simon Whitehead
  • 63,300
  • 9
  • 114
  • 138
  • Figured it out. Question: It displays the full filepath. How could I make it only display the file name and file extension? –  Apr 03 '13 at 01:17
  • For some reason it's still doing the same. I tried adding "WithoutExtension" to the end of "GetFileName", but it didn't work.. –  Apr 03 '13 at 01:23
  • What are you passing into the function? The `Dialog.FileName`? – Simon Whitehead Apr 03 '13 at 01:26
  • I'm using the other answer's "this.Text = string.Format("{0} - MyNotepad", ofd.Filename);". –  Apr 03 '13 at 01:27
  • How is that achieved? Sorry if that's a stupid question.. I'm new to programming and this is all experience. –  Apr 03 '13 at 01:32
  • `this.Text = string.Format("{0} - MyNotepad", System.IO.Path.GetFileName(ofd.Filename));` – Simon Whitehead Apr 03 '13 at 01:32
  • damn, i shouldn't have missed this in my answer! – Parimal Raj Apr 03 '13 at 01:33
  • @SimonWhitehead I've tried that, but it still doesn't work. I will edit my original question to show the current code. –  Apr 03 '13 at 01:36
  • Combining your edit with AppDeveloper's edit did the trick. It's working perfectly now. Thanks for your help tonight, guys! :-) –  Apr 03 '13 at 01:46
  • This answer still works. I'm using VS 2019 with C# here in 2021. Thanks! – CaptainGenesisX Jan 04 '21 at 18:35