Load and Display Contents of RTF File in a RichTextBox in C#

In this tutorial, I will teach you how to create a program that will load and display the contents of an RTF File in a RichTextBox using VB.NET. RTF means Rich Text Format and is a text file format used by Microsoft products. RTF files support text style formatting, as well as images within the text. Now, let's start this tutorial! 1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio 2010: Go to File, click New Project, and choose Windows Application. 2. Next, add two Buttons named Button1 and labeled it as "Browse.." for opening an RTF file only and Button2 and labeled it as "Load" to load and display the contents of the selected file in a RichTextBox. Insert also OpenFileDialog named OpenFileDialog1, TextBox named TextBox1 to display the filename and path of the rtf file, and RichTextBox named RichTextBox1 for displaying the contents of this rtf file. You must design your interface like this: design 3. For Button1_Click that is used to open an RTF file, put this code below.
  1. private void Button1_Click(System.Object sender, System.EventArgs e)
  2. {
  3. OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
  4.  
  5. OpenFileDialog1.Filter = "All Files|*.*|RTF Files (*)|*.rtf";
  6. if (OpenFileDialog1.ShowDialog == Windows.Forms.DialogResult.OK)
  7. {
  8. TextBox1.Text = OpenFileDialog1.FileName;
  9. }
  10.  
  11. }
The code above filters to open the kind of file that is an .rtf extension file only. After choosing and selecting the rtf file and clicking ok in the openfiledialog, the file path and filename will be loaded in the TextBox. 4. For Button2_Click that is used to load and display the content of the selected rtf file, put this code below.
  1. private void Button2_Click(System.Object sender, System.EventArgs e)
  2. {
  3. RichTextBox1.LoadFile(TextBox1.Text);
  4. }
The code above is so simple. We used the RichTextBox control with its LoadFile Method to open and display the chosen rtf file. This will loads the contents of a File into the RichTextBox control. Output: output For more inquiries and need programmer for your thesis systems in any kind of programming languages, just contact my number below. Best Regards, Engr. Lyndon Bermoy IT Instructor/System Developer/Android Developer/Freelance Programmer If you have some queries, feel free to contact the number or e-mail below. Mobile: 09488225971 Landline: 826-9296 E-mail:[email protected] Add and Follow me on Facebook: https://www.facebook.com/donzzsky Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

Add new comment