在 C# 中将 .txt 文件打开到 richTextBox 中

发布于 2024-10-06 04:38:06 字数 416 浏览 0 评论 0原文

我希望能够将 .txt 文件打开到 c# 中的 Richtextbox 中,也打开到我制作的名为“notes”的全局变量中,但不知道如何执行此操作。这是我目前拥有的代码:

OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
    richTextBox1.Text = opentext.FileName;
    Globals.notes = opentext.FileName;
}

唯一的问题是它既没有出现在 Richtextbox 中,也没有出现在全局变量中,并且全局允许它以另一种形式在另一个 RichTextBox 中查看。所以请你帮忙,最好将 .txt 文件放入两者中,

谢谢

I want to be able to open a .txt file up into a richtextbox in c# and also into a global variable i have made called 'notes' but don't know how to do this. This is the code i have at the moment:

OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
    richTextBox1.Text = opentext.FileName;
    Globals.notes = opentext.FileName;
}

Only problem is it doesn't appear in neither the richtextbox nor in the global varibale, and the global allows it to be viewed in another richtextbox in another form. So please can you help, with ideally the .txt file going into both,

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

不美如何 2024-10-13 04:38:06

您的意思是要显示文本还是文件名?

richTextBox1.Text = File.ReadAllText(opentext.FileName); 
Globals.notes = richTextBox1.Text;

您可能还想将其更正为:

if (opentext.ShowDialog() == DialogResult.OK)

Do you mean you want to have the text displayed or the filename?

richTextBox1.Text = File.ReadAllText(opentext.FileName); 
Globals.notes = richTextBox1.Text;

You probably also want to correct this to:

if (opentext.ShowDialog() == DialogResult.OK)
昔日梦未散 2024-10-13 04:38:06

在c#中没有全局变量。您可以获得的最接近的方法是将变量设置为“public static”。但更好的解决方案是使其成为您有权访问的对象的实例变量,例如您的主窗口类。

In c# there are not global variables. The closest thing you can get is to make the variable "public static". But a better solution would be to make it an instance variable of an object you have access to, for example your main window class.

诗酒趁年少 2024-10-13 04:38:06
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
    richTextBox1.Text = sr.ReadToEnd();
    sr.Close();
}
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    System.IO.StreamReader sr = new System.IO.StreamReader(openFileDialog1.FileName);
    richTextBox1.Text = sr.ReadToEnd();
    sr.Close();
}
反差帅 2024-10-13 04:38:06

FileName property of OpenFileDialog control just gives the full path of file selected by user. In order to read the content of this file, you will need to use a method like File.ReadAllText.

夜血缘 2024-10-13 04:38:06

尝试使用这个,我将它用于聊天程序,它工作正常,您可以将计时器速率设置为您想要的任何值。您也不必使用计时器,您可以使用按钮来启动富文本框的刷新。

    private void refreshRate_Tick(object sender, EventArgs e)
    {
        richTextBox1.Text = File.ReadAllText(@"path.txt");
    }

希望这有帮助!

Try using this, I used it for a chat program and it works fine, you can set your timer rate to whatever you want. You also don't have to use a timer, you can have a button initiate the refresh of the rich text box.

    private void refreshRate_Tick(object sender, EventArgs e)
    {
        richTextBox1.Text = File.ReadAllText(@"path.txt");
    }

Hope this helps!

寄与心 2024-10-13 04:38:06

我希望,我没有迟到。似乎有类似的内容:

OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
    string selectedFileName = opentext.FileName;
    richTextbox.LoadFile(selectedFileName, RichTextBoxStreamType.UnicodePlainText);
}

参考

I hope, I am not late. It seems like there is something like:

OpenFileDialog opentext = new OpenFileDialog();
if (opentext.ShowDialog() == DialogResult.OK)
{
    string selectedFileName = opentext.FileName;
    richTextbox.LoadFile(selectedFileName, RichTextBoxStreamType.UnicodePlainText);
}

Refs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文