Thread: Help with loading files into rich text box

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    Help with loading files into rich text box

    OK, I followed the Microsoft MSDN and copied their code exactly, and it work for RTF files only, but I want to be able to open text files, html files, php files....basically any files.

    The code is:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
            {
    
            }
    
            public void LoadMyFile()
            {
                // Create an OpenFileDialog to request a file to open.
                OpenFileDialog openFile1 = new OpenFileDialog();
    
                // Initialize the OpenFileDialog to look for RTF files.
    
                openFile1.Filter = "Text Files|*.txt"; 
    
                // Determine whether the user selected a file from the OpenFileDialog.
                if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK &&
                   openFile1.FileName.Length > 0)
                {
                    // Load the contents of the file into the RichTextBox.
                    richTextBox1.LoadFile(openFile1.FileName);
                }
            }
    
            private void openmenu1_Click(object sender, EventArgs e)
            {
                LoadMyFile();
            }
        }
    }

    I get this error when I double click on the file to open:

    "File Format is not valid", and it relates to this code:

    Code:
    richTextBox1.LoadFile(openFile1.FileName);
    .


    I am a beginner in C#, and want to understand how it works,


    Thanks,

    BP

  2. #2
    Registered User Frobozz's Avatar
    Join Date
    Dec 2002
    Posts
    546
    Try

    Code:
    richTextBox1.LoadFile(openFile1.FileName, RichTextBoxStreamType.PlainText)

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    2
    OK, thank you. I will try it when I get home

    BP

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    3
    This is how i open files! A good piece of code.

    Code:
    public void LoadMyFile()
    {
    	// Create an OpenFileDialog to request a file to open.
    	OpenFileDialog openFile1 = new OpenFileDialog();
    	// Initialize the OpenFileDialog to look for RTF files.
    	openFile1.Filter = "Text Files|*.txt"; 	
    	//
    	//Try to open the file
    	try
    	{
    		// Determine whether the user selected a file from the OpenFileDialog.
    		if (openFile1.ShowDialog() == System.Windows.Forms.DialogResult.OK && openFile1.FileName.Length > 0)
    		{
    			//Declare using the StreamReader
    			using(System.IO.StreamReader Reader = new System.IO.StreamReader(openFile1.FileName))
    			{
    				//Declare a buffer in which to hold each line and write it to the richTextBox
    				string buffer = null;
    				//
    				//Writing one and one line from the file to the richTextBox
    				foreach((buffer = Reader.ReadLine())
    					richTextBox1.Text += buffer + System.Environment.NewLine;
    			}
    		}
    	}
    	// Run this is file opening fails. It will fail in case of invalid filename and so on
    	catch(Exception x)
    		MessageBox.Show("Error while opening file:\n\n" + x.Message.ToString());
    }
    I just wrote it in notepad out of the blue now, so it's not debugged. There might be an error here or there in the line of forgetting the ";" and so on, but you'll figure it out ;-)

    Good luck to you!
    Last edited by Dahwan; 10-19-2007 at 01:01 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 0
    Last Post: 07-02-2007, 12:32 AM
  2. Automatically enter text in a dialog box
    By leojose in forum Windows Programming
    Replies: 6
    Last Post: 12-13-2005, 11:59 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM
  5. String from a text box
    By Garfield in forum Windows Programming
    Replies: 4
    Last Post: 09-16-2001, 03:20 PM