RichTextBox 无法显示文件内容

问题描述 投票:0回答:1

我遇到一个问题,我尝试在 C# 应用程序中的 RichTextBox 中显示文件的内容,但即使文本已成功加载到字符串变量中,文本也不会出现在 RichTextBox 中。这是相关的代码片段:

string fileContent = File.ReadAllText(openFileDialog.FileName);
childForm1.richTextBox.Text = fileContent;

我已检查 fileContent 是否包含我期望的文本,但由于某种原因,它没有显示在 RichTextBox 中。我已确保 RichTextBox 可见且具有足够的大小,并且未将其设置为只读。另外,我已经验证文件路径是否正确并且文件存在。 有人可以帮助我确定可能导致此问题的原因以及如何解决它吗?谢谢!

c# winforms
1个回答
0
投票

给这个狂欢吧👍🏼

if (childForm1.richTextBox.InvokeRequired)
{
childForm1.richTextBox.Invoke(new 
MethodInvoker(delegate 
{
    childForm1.richTextBox.Text = 
fileContent;
}));
}
else
{
childForm1.richTextBox.Text = fileContent;
}
© www.soinside.com 2019 - 2024. All rights reserved.