none
Loading RTF file into RichTextBox on load RRS feed

  • Question

  • Hello.  I am new to C#.  I may be particularly dense, because I am stuck on something that I feel should be (and probably is) simple.

    All I want to do, is create a brand new C# project (which I have done), add a RichTextBox control, and display the contents of an RTF file in the box.  It will always be the same file, so I don't need an open file dialog or anything like that.  I simply want to load an existing RTF file into the RichTextBox.

    I did some searching through MSDN and found the LoadFile method.  I think part of my problem is that I don't know where to put the code.

    So I humbly ask that someone please walk me through this.  I have named the RTF file "myfile.rtf" and it resides in the same directory as my project.  I have created a brand new project and added a RichTextBox control to the form.  Can someone please show me where and how to add the code to load and display the RTF file in the text box? 

    Thank you very much!

    Thursday, October 26, 2006 3:04 AM

Answers

  • sure. It's very straight forward. Firstly, a word of warning or rather caution!

  • RTB (RichTextBox) only supports loading plain text/RTF documents - no other. Otherwise you will get an exception. So you can't load an MS Word document into this but only if you save it as an RTF file.

     

    now, on to the solution:

    Simply say, on a button click (drag a button on the form, then double click it to take you to code view) simply do this:

     

    this.theRichTextBoxControl.LoadFile(Application.StartupPath + "\\myfile.rtf", RichTextBoxStreamType.RichText);

     

    and thats it!

    be sure to rename the "theRichTextBoxControl" control name to the name of your rich text box control.

    This will load the file from the current application startup path into the rich textbox, specifying the type of file it is.

    does this work/make it understand better?

Thursday, October 26, 2006 3:33 AM
Moderator
  • no, itll be on the form_load event

    a constructor should never and is not designed for doing other operations but constructing the object itself, any further operations should be done at the appropriate time, in this case, the form_load event

    Thursday, October 26, 2006 5:47 PM
    Moderator
  • All replies

    • sure. It's very straight forward. Firstly, a word of warning or rather caution!

    • RTB (RichTextBox) only supports loading plain text/RTF documents - no other. Otherwise you will get an exception. So you can't load an MS Word document into this but only if you save it as an RTF file.

       

      now, on to the solution:

      Simply say, on a button click (drag a button on the form, then double click it to take you to code view) simply do this:

       

      this.theRichTextBoxControl.LoadFile(Application.StartupPath + "\\myfile.rtf", RichTextBoxStreamType.RichText);

       

      and thats it!

      be sure to rename the "theRichTextBoxControl" control name to the name of your rich text box control.

      This will load the file from the current application startup path into the rich textbox, specifying the type of file it is.

      does this work/make it understand better?

    Thursday, October 26, 2006 3:33 AM
    Moderator
  • Put this code into your Form's Constructor like

    publoc Form1()

    {

            InitializeComponents();

           richTextBox.Load("myfile.rtf"); //Here

    }

    I hope this will work!

    Best Regards,

    Thursday, October 26, 2006 6:16 AM
  • you should actually be placing it in the form load event. Mine was just an example however best practice is not to load items into controls for example in a constructor
    Thursday, October 26, 2006 8:59 AM
    Moderator
  • Thanks guys!  This is quite helpful.  I see I could do it either way you told me...

    So the best way then would be to handle it like RizwanSharp suggested... in the constructor method?  Because ideally, I'd like the rtf file to be loaded automatically (without the user having to press any buttons at all).

    Thursday, October 26, 2006 3:07 PM
  • no, itll be on the form_load event

    a constructor should never and is not designed for doing other operations but constructing the object itself, any further operations should be done at the appropriate time, in this case, the form_load event

    Thursday, October 26, 2006 5:47 PM
    Moderator
  • Ok, thank you ahmedilyas.  I appreciate it.
    Thursday, October 26, 2006 5:57 PM