1

So I'm making a chat program but I am having issues creating a new line in the text box instead of overwriting the other message. Here is my code:

        private void refreshRate_Tick(object sender, EventArgs e)
        {
        String ChatPath = @"Path";
        String line;
        try
        {
            StreamReader sr = new StreamReader(@"Path");
            line = sr.ReadLine();
            richTextBox1.Text = null;
            while (line != null)
            {
                richTextBox1.AppendText(Environment.NewLine);
                richTextBox1.Text = line;
                line = sr.ReadLine();
            }
            sr.Close();
        }
        catch (Exception r)
        {
            Console.WriteLine("Exception: " + r.Message);
        }
        finally
        {
        }
    }
4

3 回答 3

5

你不需要StreamReaderEnvironment.NewLine

richTextBox1.Text=File.ReadAllText(path);
于 2013-10-20T18:47:52.650 回答
1

如果您更改 richTextBox1.Text = line为, richTextBox1.AppendText(line);您将丢失最后一行,因此将while块更改为:

while (line != null)
{
    richTextBox1.AppendText(Environment.NewLine);        
    line = sr.ReadLine();
    richTextBox1.AppendText(line??"");
}
于 2013-10-20T18:46:06.480 回答
1

我想你想删除这条线

richTextBox1.Text = line;

并添加

richTextBox1.AppendText(line);

从文件中读取它之后。

于 2013-10-20T18:46:32.320 回答