在C#中将.txt文件打开到richTextBox中 [英] Open a .txt file into a richTextBox in C#

查看:364
本文介绍了在C#中将.txt文件打开到richTextBox中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在c#中将.txt文件打开成一个richtextbox,并且将其打开成为我称为'笔记'的全局变量,但不知道如何执行此操作。这是我目前的代码:

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

唯一的问题是它不会出现在richtextbox或全局中varibale和全局允许它以另一种形式在另一个richtextbox中查看。所以请你能帮忙,理想的情况是将.txt文件放入这两个文件中,

谢谢

解决方案

  richTextBox1.Text =文件名.ReadAllText(opentext.FileName); 
Globals.notes = richTextBox1.Text;

您可能还想更正此问题:

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


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

解决方案

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)

这篇关于在C#中将.txt文件打开到richTextBox中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆