none
Reading Rich Text Doc to Rich Text Box RRS feed

  • Question

  • I need the Richtextdoc I gave the directory for to load in the RichTextBox1, what have I done that is making it not load in it? Is there a property in the richtextbox I need to set or did I code wrong?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ReadFrom_File
    {
        public partial class Form1 : Form
        {
            //Public variables - i.e. Game names.
            public static string gtaiv ;
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                //Repeat for every game listed an make sure the dir is right.
                gtaiv = File.ReadAllText(@"C:\Users\Judd\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Grand Theft Auto IV.rtf");
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.Text == "Grand Theft Auto IV")
                {
                    richTextBox1.Text = gtaiv ;
                }
            }
        }
    }
     

    Here's the Program.cs if neeed:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new ReadFrom_File.Form1());
            }
        }
    }
    


    • Edited by Judd K Thursday, June 14, 2012 12:59 AM
    Thursday, June 14, 2012 12:41 AM

Answers


  • Instead of reading the text into the strings, I would recommend just calling:

    richTextBox1.LoadFile(theFilename);

    See LoadFile (http://msdn.microsoft.com/en-us/library/3f99sst7.aspx) for details.



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 14, 2012 1:00 AM
    Moderator
  • Hi Judd,

     Do you get any exceptions by using  richTextBox1.LoadFile(@"C:\...") ?

    I think the code sample Reed gave is right. Please make sure the File Format of Grand Theft Auto IV.rtf is right( try  File – Save As –Save as type -Rich Text Format) and then run your project again.

    Furthermore ,you can use uses the OpenFileDialog class to display a dialog to request the file Grand Theft Auto IV.rtf.

    Below is a code sample,I hope it will be helpful :

    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.DefaultExt = "*.rtf";
       openFile1.Filter = "RTF Files|*.rtf";
    
       // 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 comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.Text == "Grand Theft Auto IV")
                {
                   LoadMyFile();
                }
            }

    If you have any questions, feel free to let me know.


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us





    Friday, June 15, 2012 7:55 AM
    Moderator

All replies


  • Instead of reading the text into the strings, I would recommend just calling:

    richTextBox1.LoadFile(theFilename);

    See LoadFile (http://msdn.microsoft.com/en-us/library/3f99sst7.aspx) for details.



    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 14, 2012 1:00 AM
    Moderator
  • What do I remove and where do I add it in my code? I'm only learning and don't know a whole lot.
    Thursday, June 14, 2012 1:06 AM
  • What do I remove and where do I add it in my code? I'm only learning and don't know a whole lot.

    Try this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ReadFrom_File
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.Text == "Grand Theft Auto IV")
                {
                    richTextBox1.LoadFile(@"C:\Users\Judd\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Grand Theft Auto IV.rtf");
                }
            }
        }
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Thursday, June 14, 2012 1:30 AM
    Moderator
  • What do I remove and where do I add it in my code? I'm only learning and don't know a whole lot.

    Try this:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace ReadFrom_File
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
            }
    
            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.Text == "Grand Theft Auto IV")
                {
                    richTextBox1.LoadFile(@"C:\Users\Judd\documents\visual studio 2010\Projects\WindowsFormsApplication1\WindowsFormsApplication1\bin\Debug\Grand Theft Auto IV.rtf");
                }
            }
        }
    }


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".

    Still doing the same as before, not loading the text into the richTextBox1. It shows the dropdownlist then I select the game 'Grand Theft Auto' an nothing shows. The directory is correct so I don't know what's causing it?
    Thursday, June 14, 2012 2:27 AM
  • I guess the comboBox1.SelectedIndexChanged is not connected to the comboBox1_SelectedIndexChanged method. In your form's designer, select the combobox, click on the little bolt icon in the properties window, scroll to the SelectedIndexChanged and check if the method is there. If it's not, click on the empty box to the right of the event name and select your method.

    Thursday, June 14, 2012 9:13 AM
  • Hi Judd,

     Do you get any exceptions by using  richTextBox1.LoadFile(@"C:\...") ?

    I think the code sample Reed gave is right. Please make sure the File Format of Grand Theft Auto IV.rtf is right( try  File – Save As –Save as type -Rich Text Format) and then run your project again.

    Furthermore ,you can use uses the OpenFileDialog class to display a dialog to request the file Grand Theft Auto IV.rtf.

    Below is a code sample,I hope it will be helpful :

    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.DefaultExt = "*.rtf";
       openFile1.Filter = "RTF Files|*.rtf";
    
       // 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 comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                if (comboBox1.Text == "Grand Theft Auto IV")
                {
                   LoadMyFile();
                }
            }

    If you have any questions, feel free to let me know.


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us





    Friday, June 15, 2012 7:55 AM
    Moderator