在 C# 的 Windows 窗体上显示文本文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10698799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying a text file on a Windows Form for C#
提问by Sarp Kaya
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.
我正在尝试显示 txt 文件的内容。我想我应该为该方法使用 RichTextBox。我所做的就是这个。但是它不起作用。
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();
}
采纳答案by Francesco Baruchelli
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.
我想 RichTextBox1_TextChanged 与您要填充的 RichTextBox 的更改事件相关联。这意味着除非您手动更改 RichTextBox 本身的内容,否则它不会执行。
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.
此外,在该方法中,您正在调用一个方法 (ReadFile),该方法读取您的文件并将内容作为字节 [] 返回,但结果会丢失,因为您无论如何都没有使用它。
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.
然后,即使您读取文件的方式也不正确,因为您正在一次读取所有文件(您指定读取文件中包含的确切字符数),因此不需要 while 循环。
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:
您将需要在表单的 InitializeComponent() 中使用此行:
this.Load += new System.EventHandler(this.Form1_Load);
回答by Dave Lawrence
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.
您正在返回缓冲区但不在任何地方使用它。
回答by Falaque
Do this:
做这个:
Have a button.
On button click, call ReadFile(), convert byte[] received from ReadFile() to string and display in TextBox.
有一个按钮。
单击按钮时,调用 ReadFile(),将从 ReadFile() 接收到的 byte[] 转换为字符串并显示在 TextBox 中。
回答by Thorsten Dittmar
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 bytearray to a string and you're also not assigning it to the rich text box. Simply reading the file won't help.
这会一次性读取给定文件中的所有文本,并将其分配给富文本框。当您阅读方法中的文本时,您不会将结果byte数组转换为字符串,也不会将其分配给富文本框。简单地阅读文件无济于事。
Please remove the TextChangedevent also:The TextChangedevent 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 Textproperty, 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.
也请删除该TextChanged事件:TextChanged每次更改富文本框中的文本时都会调用该事件。为Text属性设置新值时也是如此,这会导致无限递归。此外,当文本最初没有更改时,永远不会调用此事件,因此您必须在富文本框中手动输入文本才能触发此事件。
回答by Asif Mushtaq
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();
}
回答by Omobo Ebibote
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);
}
}

