-1

There is a question like this: Open a .txt file into a richTextBox in C#

But I need something a little different, I can open the txt in a richTextBox but I am using a button to make the file open. I want to skip the button and just have it load in the richTextBox.

Here is the button code, how do I move it to the richTextBox?:

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.FileName = "Changes.txt";

            string strfilename = openFileDialog1.FileName;
            string filetext = File.ReadAllText(strfilename);

            richTextBox1.Text = filetext;
        }

        private void richTextBox1_TextChanged(object sender, EventArgs e)
        {

        }

By double clicking the rich I get richTextBox1_TextChanged:

enter image description here

Community
  • 1
  • 1
Ryan Foy
  • 322
  • 6
  • 19

3 Answers3

4

How about the load event of the Form?

private void form_load(object sender, Eventargs e)
{
     OpenFileDialog openFileDialog1 = new OpenFileDialog();
     openFileDialog1.FileName = "Changes.txt";

     string strfilename = openFileDialog1.FileName;
     string filetext = File.ReadAllText(strfilename);

     richTextBox1.Text = filetext;
}

I'm not sure what you want to do in the richTextBox1_TextChanged event, maybe you could provide more information regarding this.

Having seen your edit, your'e still better off using the button to open the dialog and selecting a file that way. More intuitive then selecting the RichTextBox to trigger the OpenFileDialog.

Ric
  • 12,855
  • 3
  • 30
  • 36
  • It is not that I want to even selected it, I want to open when the form opens, I am sorry if I made you misinterpret this. – Ryan Foy Dec 19 '13 at 02:04
0

From what I understand, you are probably searching for the event RichTextBox_Click. In your form.cs design view, click your RichTextBox and press F4 to see its properties and click the thunderbolt icon to see its events. Search for "Click" and then double click it. That will create the click event in your class, and you can add your logic there.

0

modify your code with this. when dialogbox visible select the text file which you named here as "Changes.txt".

    private void button1_Click(object sender, EventArgs e)
    {
        OpenFileDialog openFileDialog1 = new OpenFileDialog();
        openFileDialog1.FileName = "Changes.txt";
        openFileDialog1.ShowDialog();
        string strfilename = openFileDialog1.FileName;
        string filetext = File.ReadAllText(strfilename);

        richTextBox1.Text = filetext;
    }
Chandan Kumar
  • 4,570
  • 4
  • 42
  • 62
  • I will give an answer tomorrow, I am sorry that I can not tell what works, I am not on the computer that has the project will fix this tomorrow, I am also sorry if this was a bad question, I am still learning how to move around areas of this program. – Ryan Foy Dec 19 '13 at 01:53
  • @Ryan Foy : your code is 100% correct. only what you need to do is add openFileDialog1.ShowDialog(); after defining the filename to display the opendialog box and select the text file which you named here as "Changes.txt". That's it. It will read all data available inside the text file and display in richTextBox1. – Chandan Kumar Dec 19 '13 at 05:05
  • I have used `ShowDialog`, but I used it to check if it was working properly, but now I want to completely get rid of the button press and just have the text display on the screen for people to read. – Ryan Foy Dec 19 '13 at 11:46