在 C# 中将 .txt 文件打开到 richTextBox 中
我希望能够将 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您的意思是要显示文本还是文件名?
您可能还想将其更正为:
Do you mean you want to have the text displayed or the filename?
You probably also want to correct this to:
在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.文件名
OpenFileDialog
的属性 控件仅给出用户选择的文件的完整路径。为了读取此文件的内容,您需要使用类似File.ReadAllText
。FileName
property ofOpenFileDialog
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 likeFile.ReadAllText
.尝试使用这个,我将它用于聊天程序,它工作正常,您可以将计时器速率设置为您想要的任何值。您也不必使用计时器,您可以使用按钮来启动富文本框的刷新。
希望这有帮助!
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.
Hope this helps!
我希望,我没有迟到。似乎有类似的内容:
参考
I hope, I am not late. It seems like there is something like:
Refs