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!

Loading RTF file into RichTextBox on load
JoshKorn
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
Novelle
sure. It's very straight forward. Firstly, a word of warning or rather caution!
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
SOAC
Andreas Kranister
Dawchiks
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).
prog.gabi
Put this code into your Form's Constructor like
publoc Form1()
{
InitializeComponents();
richTextBox.Load("myfile.rtf"); //Here
}
I hope this will work!
Best Regards,