简体   繁体   中英

Displaying a text file on a Windows Form for C#

I am trying to display the content of a txt file. I thought I should use RichTextBox for that method. What I've done was this. However it does not work.

public static byte[] ReadFile() {

        FileStream fileStream = new FileStream(@"help.txt", FileMode.Open, FileAccess.Read);
        byte[] buffer;
        try {
            int length = (int)fileStream.Length;  // get file length
            buffer = new byte[length];            // create buffer
            int count;                            // actual number of bytes read
            int sum = 0;                          // total number of bytes read

            // read until Read method returns 0 (end of the stream has been reached)
            while ((count = fileStream.Read(buffer, sum, length - sum)) > 0)
                sum += count;  // sum is a buffer offset for next reading
        } finally {
            fileStream.Close();
        }
        return buffer;
    }

    private void richTextBox1_TextChanged(object sender, EventArgs e) {
        ReadFile();
    }

I may be missing something but I don't see where you are appending the result of your read to the textbox!

You are returning buffer but not using it anywhere.

You've got a couple of problems here.

I suppose that richTextBox1_TextChanged is associated with the changed event of the RichTextBox you want to fill. This means that it isn't executed unless you manually change the content of the RichTextBox itself.

Furthermore in the method you are calling a method (ReadFile) which read your file and return the content as a byte[], but the result get lost since you aren't using it anyway.

Then even the way you are reading the file is not correct, since you are reading all the file at once (you are specifying to read the exact number of characters contained in the file), so the while loop isn't needed.

I would attach to the load event of the form and write something like this:

public string FillRichText(string aPath)
{
    string content = File.ReadAllText(aPath);
    richTextBox1.Text = content;
}

private void Form1_Load(object sender, EventArgs e)
{
    FillRichText(@"help.txt");
}

You will need this line in the InitializeComponent() of your form:

this.Load += new System.EventHandler(this.Form1_Load);

Do this:

  1. Have a button.

  2. On button click, call ReadFile(), convert byte[] received from ReadFile() to string and display in TextBox.

Use this:

In the form's constructor, write the following code:

public Form1()
{
    InitializeComponent(); // This should already be there by default

    string content = File.ReadAllText(@"help.txt");
    richTextBox1.Text = content;
}

This reads all the text from the given file in one go and assigns it to the rich text box. While you read the text in your method, you're not converting the resulting byte array to a string and you're also not assigning it to the rich text box. Simply reading the file won't help.

Please remove the TextChanged event also: The TextChanged event is called every time the text in the rich text box is changed. This is also the case when setting a new value to the Text property, which would cause an infinite recursion. Also, this event is never called when the text doesn't change in the first place, so you would have to enter text manually in the rich text box to fire this event.

Change the method to the following and you don't need rich textbox , a simple textbox can serve the purpose.

public void ReadFile() {

    TextReader reder = File.OpenText(@"help.txt");
    textBox1.Text = reder.ReadToEnd();        
}
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (openFileDialog1.ShowDialog() == DialogResult.OK)   
        {
            label1.Text = openFileDialog1.FileName;
            richTextBox1.Text = File.ReadAllText(label1.Text);
        }
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM